Hi Jim,
The reason for the failure was that you were trying to write a new attribute
to a dataset where an attribute with the name already exists.
If your want to update the values of an attribute, the correct way is to get
the attribute from the object, change the values, and write the attribute
back to the object.
The following example code creates a new dataset and adds an attribute to
the dataset. Then close the file and update the attribute that is attached to the
dataset.
Hope it helps.
Thanks
--pc
···
=====================
private static void testUpdateAttr(String fname) throws Exception
{
int data[] = {1,2,3,4,5,6};
long[] dims = {data.length};
String dname = "/dset";
// create a new file and a new dataset
H5File file = new H5File(fname, FileFormat.CREATE);
Datatype dtype = file.createDatatype(Datatype.CLASS_INTEGER, 4, Datatype.NATIVE, Datatype.NATIVE);
Dataset dataset = file.createScalarDS (dname, null, dtype, dims, null, null, 0, data);
// create and write an attribute to the dataset
long[] attrDims = {2};
int[] attrValue = {0, 10000};
Attribute attr = new Attribute("range", dtype, attrDims);
attr.setValue(attrValue); // set the attribute value
dataset.writeMetadata(attr);
// close the file
file.close();
// open the file with read and write access
file = new H5File(fname, FileFormat.WRITE);
// retrieve the dataset and attribute
dataset = (Dataset)file.get(dname);
attr = (Attribute)dataset.getMetadata().get(0);
// change the attribute value
if (attr!=null) {
attrValue[0] = 100;
attr.setValue(attrValue);
dataset.writeMetadata(attr);
}
// close file resource
file.close();
}
On 10/28/2010 12:25 PM, James Bullard wrote:
Hi All,
The documentation for writeMetadata (java hdf5 API) states that if the
object exists it overwrites its value, however I am observing that if
the object exists it throws an exception and the same code works fine if
the object doesn't exist.
Am I in err?
thanks, jim
_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org