Here's some sample code to hopefully help anybody else who might be struggling with this topic. Basically a car object
with a string model description, int year and double array of top speed values is put into memory as HDF5 format, and
subsequenty retrieved.
Since the model and year member variables are small, I used attributes to describe this data in the HDF5 format. And even
though the top speeds only has 4 entries, I assume that this data might be large in practice so used a data set for it.
Since I wanted to just get this up and running, I freely used HDF5Constants.H5P_DEFAULT whenever I was in doubt, but
appreciate it may not be the best thing, particularly since I did not fully understand all implications. If any one sees anything
terribly wrong, please let me know, thanks.
private void storeCarAsHDF5(){
try {
//first let's create an instance of a car -this is the java object we wish to
//store in memory as HDF5. The car object will include 2 attributes: a string
//model description and an integer year. It will also have a dataset of doubles
//called topSpeeds.
double[] topSpeeds = {30.25, 44.0, 65.1, 72.6};
Car car = new Car("Ford Anglia", 1971, topSpeeds);
int errorCode;
int faplId = H5.H5Pcreate(HDF5Constants.H5P_FILE_ACCESS); //create a property list of type file access
int increment = 512; //block size
boolean backingStore = false; //do not write contents to disk (true = write contents to file after file is closed)
//set the core
errorCode = H5.H5Pset_fapl_core(faplId, increment, backingStore);
if (errorCode<0){
System.out.println("Error setting the core driver.");
return;
}
String name = "should_not_exist.txt"; //after all, we don't want to use expensive I/O right?
int flags = HDF5Constants.H5P_DEFAULT; //use default
int createId = HDF5Constants.H5P_DEFAULT;
int fileId = H5.H5Fcreate(name, flags, createId, faplId); //create a file id -this will be pointing to a virtual file
// we now have a valid file id.
int sizeHint = 0; //optional parameter indicating number of bytes to reserve for the names that will appear in the group
int groupId = H5.H5Gcreate(fileId, car.getClass().getSimpleName(), sizeHint);
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Here is the section that will create
// a string model attribute, and attach it
// to our car group, also writing in
// the model description provided by our
// car object.
//
int modelTypeId = HDF5Constants.H5T_NATIVE_CHAR;
int rank = 2;
long[] dims = new long[rank];
dims[0] = 1;
dims[1] = car.getModel().length();
long[] maxDims = dims;
int modelSpaceId = H5.H5Screate_simple(rank, dims, maxDims);
//now add an attribute
int modelAttId = H5.H5Acreate(groupId, "model", modelTypeId, modelSpaceId, 0);
//now write a value
String modelValue = car.getModel();
errorCode = H5.H5Awrite(modelAttId, modelTypeId, modelValue.getBytes());
if (errorCode<0){
System.out.println("Error writing car's model attribute.");
return;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Here is the section that will create
// a year int attribute, and attach it
// to our car group, also writing in
// the year provided by our car object.
//
int yearTypeId = HDF5Constants.H5T_NATIVE_INT32;
rank = 2;
dims = new long[rank];
dims[0] = 1;
dims[1] = 1;
maxDims = dims;
int yearSpaceId = H5.H5Screate_simple(rank, dims, maxDims);
//now add an attribute
int yearAttId = H5.H5Acreate(groupId, "year", yearTypeId, yearSpaceId, 0);
//now write a value
int[] yearValueArray = {car.getYear()};
errorCode = H5.H5Awrite(yearAttId, yearTypeId, yearValueArray);
if (errorCode<0){
System.out.println("Error writing car's year attribute.");
return;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Here is the section that will create
// a dataset and attach it
// to our car group, also writing in
// the top speed values provided by our car object.
//
int topSpeedsTypeId = HDF5Constants.H5T_NATIVE_DOUBLE;
rank = 2;
dims = new long[rank];
dims[0] = 1;
dims[1] = car.getTopSpeeds().length;
maxDims = dims;
int topSpeedsSpaceId = H5.H5Screate_simple(rank, dims, maxDims);
//now create a dataset
int topSpeedsDataSetId = H5.H5Dcreate(groupId, "top speeds", topSpeedsTypeId,
topSpeedsSpaceId, HDF5Constants.H5P_DEFAULT);
//int propertyList = H5.H5Pcreate(HDF5Constants.H5P_DEFAULT);
//now write the values
double[] topSpeedsValueArray = car.getTopSpeeds();
errorCode = H5.H5Dwrite(topSpeedsDataSetId, topSpeedsTypeId,
HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT,
HDF5Constants.H5P_DEFAULT, topSpeedsValueArray);
if (errorCode<0){
System.out.println("Error writing car's top speeds dataset.");
return;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//OK, let's close the file here....
H5.H5Fclose(fileId);
//now let's retrieve it to test to see if we have the
//same data.....
fileId = H5.H5Fopen(name, flags, faplId);
groupId = H5.H5Gopen(fileId, car.getClass().getSimpleName());
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Retrieving the name attribute
//
modelAttId = H5.H5Aopen_idx(groupId, 0); //the index for the name
//attribute should be 0 as
//we added that first
String[] nameMetaNameArray = {""};
long nameMetaNameLength = H5.H5Aget_name(modelAttId, increment, nameMetaNameArray);
if (nameMetaNameLength<1){
System.out.println("Attribute meta name for model can not be found.");
return;
}
System.out.println("The attribute name is: " + nameMetaNameArray[0]);
byte[] modelByteArray = new byte[car.getModel().length()];
errorCode = H5.H5Aread(modelAttId, modelTypeId, modelByteArray);
if (errorCode<0){
System.out.println("Attribute name value can not be read.");
return;
}
System.out.println("The value is: " + new String(modelByteArray).trim());
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Retrieving the age attribute
//
yearAttId = H5.H5Aopen_idx(groupId, 1); //note index is 1; was added
//after name attribute
String[] yearMetaNameArray = {""};
long yearMetaNameLength = H5.H5Aget_name(yearAttId, increment, yearMetaNameArray);
if (yearMetaNameLength<1){
System.out.println("Attribute meta name for year can not be found.");
return;
}
System.out.println("The attribute name is: " + yearMetaNameArray[0]);
int[] yearIntArray = new int[1];
errorCode = H5.H5Aread(yearAttId, yearTypeId, yearIntArray);
if (errorCode<0){
System.out.println("Attribute year value can not be read.");
return;
}
System.out.println("The value is: " + yearIntArray[0]);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Retrieving the dataset
//
topSpeedsDataSetId = H5.H5Dopen(groupId, "top speeds");
double[] topSpeedsDoubleArray = new double[car.getTopSpeeds().length];
errorCode = H5.H5Dread(topSpeedsDataSetId, topSpeedsTypeId,
HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT,
HDF5Constants.H5P_DEFAULT, topSpeedsDoubleArray);
if (errorCode<0){
System.out.println("Dataset top speeds can not be read.");
return;
}
for (int count=0; count<topSpeedsDoubleArray.length; count++){
System.out.println("top speed: " + topSpeedsDoubleArray[count]);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
}catch(HDF5LibraryException e){
e.printStackTrace();
System.out.println("Exception is " + e);
}catch(HDF5Exception e){
e.printStackTrace();
System.out.println("Exception is " + e);
}catch(NullPointerException e){
e.printStackTrace();
System.out.println("Exception is " + e);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception is " + e);
}
}
//here's our car object
public class Car{
private String model;
private int year;
private double[] topSpeeds;
public Car(String model, int year, double[] topSpeeds){
this.model = model;
this.year = year;
this.topSpeeds = topSpeeds;
}
public String getModel(){
return model;
}
public int getYear(){
return year;
}
public double[] getTopSpeeds(){
return topSpeeds;
}
}