Using HDF5DotNet, can anyone point me at example code, which will open an hdf5 file, extract the contents of a dataset, and print the contents to standard output?
So
So, your start was awesome. I've created some extensions which should help you out. Using this in your code, you should be able to things that make more sense in an object-oriented language, such as (in your case):
H5.Open();
var h5FileId= H5F.open("example.h5");
double[,] dataArray = h5FileId.Read2DArray("/Timings/aaPCBTimes");
// or more generically...
T[,] dataArray = h5FileId.Read2DArray("/Timings/aaPCBTimes");
Here are the incomplete extensions, I'll look into adding them into the HDF5Net...
public static class HdfExtensions
{
// thank you http://stackoverflow.com/questions/4133377/splitting-a-string-number-every-nth-character-number
public static IEnumerable SplitInParts(this String s, Int32 partLength)
{
if (s == null)
throw new ArgumentNullException("s");
if (partLength <= 0)
throw new ArgumentException("Part length has to be positive.", "partLength");
for (var i = 0; i < s.Length; i += partLength)
yield return s.Substring(i, Math.Min(partLength, s.Length - i));
}
public static T[] Read1DArray(this H5FileId fileId, string dataSetName)
{
var dataset = H5D.open(fileId, dataSetName);
var space = H5D.getSpace(dataset);
var dims = H5S.getSimpleExtentDims(space);
var dataType = H5D.getType(dataset);
if (typeof(T) == typeof(string))
{
int stringLength = H5T.getSize(dataType);
byte[] buffer = new byte[dims[0] * stringLength];
H5D.read(dataset, dataType, new H5Array(buffer));
string stuff = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
return stuff.SplitInParts(stringLength).Select(ss => (T)(object)ss).ToArray();
}
T[] dataArray = new T[dims[0]];
var wrapArray = new H5Array(dataArray);
H5D.read(dataset, dataType, wrapArray);
return dataArray;
}
public static T[,] Read2DArray(this H5FileId fileId, string dataSetName)
{
var dataset = H5D.open(fileId, dataSetName);
var space = H5D.getSpace(dataset);
var dims = H5S.getSimpleExtentDims(space);
var dataType = H5D.getType(dataset);
if (typeof(T) == typeof(string))
{
// this will also need a string hack...
}
T[,] dataArray = new T[dims[0], dims[1]];
var wrapArray = new H5Array(dataArray);
H5D.read(dataset, dataType, wrapArray);
return dataArray;
}
}