This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Can you add an attribute to a group within an HDF5 File?

0 votes

I'm creating an hdf5 file for output in my python script, for example:

file = HDF5File(mesh.mpi_comm(), out_dir + 'example.h5', 'w')
file.write(mesh, 'mesh')
file.write(att1, 'att1')
file.write(att2, 'att2')
file.write(att3, 'att3')

When I open the hdf5 file each attribute I'm writing (mesh, att1, etc) is saved as a separate group within the file with the relevant datasets. Is it possible to add an additional attribute to these groups? I'd like to add a separate 'citation' attribute where I can insert a citation for the source of the dataset used to create the output. Thanks for the help!

Update:
I should have been more specific, the way it writes each group (att1, etc) is operating the way I desire. Each attribute is derived from a different dataset so I need to add an attribute to the group of the citation source which I currently have as a string in my source code.

i.e. if we take the first group 'att1' I need to assign it an additional attribute 'Citation' that is created as a string earlier in my script named 'cite_att1'

asked Aug 6, 2014 by pTresslar FEniCS Novice (210 points)
edited Aug 7, 2014 by pTresslar

1 Answer

0 votes
 
Best answer

Work through the HDF5File.attributes:

from dolfin import *

mesh = UnitSquareMesh(4,4)
V = FunctionSpace(mesh, "CG", 1)

att1 = Function(V)
cite_att1 = 'Turing, Alan M. "Computing machinery and intelligence." Mind (1950): 433-460.'

f = HDF5File(mesh.mpi_comm(), "example.h5", 'w')
f.write(mesh, "mesh")
f.write(att1, "att1")
attrs = f.attributes("att1")
attrs.set("Citation", cite_att1)
f.close()
answered Aug 7, 2014 by Øyvind Evju FEniCS Expert (17,700 points)
selected Aug 8, 2014 by pTresslar

I should have been more specific, the way it writes each group (att1, etc) is operating the way I desire. Each attribute is derived from a different dataset so I need to add an attribute to the group of the citation source which I currently have as a string in my source code.

i.e. if we take the first group 'att1' I need to assign it an additional attribute 'Citation' that is created as a string earlier in my script named 'cite_att1'

I attempted using your set attribute example but it either prints the string's location in memory or fails if I try to cast the variable to a string

Ok, I misunderstood slightly, but the HDF5File.attributes.set should still work, however, your error sounds strange.

The following stores cite_att1 as the "Citation"-attribute under the "att1"-group, and works fine for me.

from dolfin import *

mesh = UnitSquareMesh(4,4)
V = FunctionSpace(mesh, "CG", 1)

att1 = Function(V)
cite_att1 = 'Turing, Alan M. "Computing machinery and intelligence." Mind (1950): 433-460.'

f = HDF5File(mesh.mpi_comm(), "example.h5", 'w')
f.write(mesh, "mesh")
f.write(att1, "att1")
attrs = f.attributes("att1")
attrs.set("Citation", cite_att1)
f.close()

Is it what you're attempting to do?

When I attempt to add the citation with that method, then pull up the hdf5 file header via:
h5dump -H master.h5

This is what I get:
ATTRIBUTE "Citation" {
DATATYPE H5T_STRING {
STRSIZE 126;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_ASCII;
CTYPE H5T_C_S1;
}
DATASPACE SCALAR
}

So it recognizes that it's a string but it doesn't print the actual text

Isn't that what you'd expect when you print the header only? Try dropping the -H, and look at h5dump -a att1/Citation example.h5.

For the record, I recommend the tool hdfview for inspecting HDF5-files.

Edit: Updated the answer.

Ideally I would like to have it print the citations when I call the header as it does when I create netcdf files (ncdump -h example.nc). Is this not possible for hdf5?

I have no idea, and this is beyond the scope of this Q&A. Try hdf-forum.

Thanks for the help!

...