This article details several technical challenges (and their solutions) encountered during the development of NiCE plugins that utilize HDF5 and HDF5 Java libraries.
The example below lists the proper way to write an Attribute with a String Datatype (or its subclass H5Datatype) to the metadata of a Group (or its subclass H5Group). This method could not be located at the HDF5 website or any associated forums. In this example, a name/value pair is being written to an H5Group.
//Create a custom String data type for the value
H5Datatype datatype = (H5Datatype) h5File.createDatatype(Datatype.CLASS_STRING, value.length(), Datatype.NATIVE,Datatype.NATIVE);
//1D of size 1
long[] dims = { 1 };
//Create a String array of size one to hold the value
String[] values = new String[1];
//Assign the value to the first array index
values[0] = value;
//Create a byte array from values using the stringToByte method
byte[] bvalue = Dataset.stringToByte(values, value.length());
//Create an attribute object
Attribute attribute = new Attribute(name, datatype, dims);
//Set the value of the attribute to bvalue
attribute.setValue(bvalue);
//Write the attribute to the group's metadata
h5Group.writeMetadata(attribute);
The example below lists the proper way to read the value of an Attribute with a String Datatype (or its subclass H5Datatype) from the metadata of a Group (or its subclass H5Group). This method could not be located at the HDF5 website or any associated forums.
When exploring an H5File using the HDFView Java program, all Datasets with a String Datatype (or its subclass H5Datatype) will be presented using the TextView perspective. For small Datasets, only the first column of Strings are shown. For larger Datasets, only lines are shown. This leads to a false positive belief that a bug has been encountered. By using the h5dump utility program, one can see that the String Datasets have been written successfully.
When writing data to a Compound Dataset, the init() method of the Dataset object must be called immediately before calling the write(Object data) method. If the init() method is not called, then all values within the Dataset will be zeroes.