Apollo3zehn / PureHDF

A pure .NET library that makes reading and writing of HDF5 files (groups, datasets, attributes, ...) very easy.
MIT License
47 stars 16 forks source link

Reading hdf5 HELP #36

Closed Marco-Pellegrino closed 10 months ago

Marco-Pellegrino commented 11 months ago

Hi @Apollo3zehn

I would like to use your library to read my .hdf5 file but I am having issue in understanding the "group" or "dataset" or "attribute".

Can you provide a quick example that reference my file?

Thanks, Marco

image recorder.zip

Apollo3zehn commented 11 months ago

Here is an overview over HDF View:

grafik

For example to read the dataset COORDINATES (which is of type double) located in group /MODEL_STAGE[1]/MODEL/NODES/ (the folder symbols are groups), you would to the following:

using var root = NativeFile.OpenRead("<your file>.h5");

var dataset = root.Group("/MODEL_STAGE[1]/MODEL/NODES").Dataset("COORDINATES");

var data = dataset
    .Read<double>()
    .ToArray2D(dim0: 264, dim1: -1 /* auto-calculated */);

Here is the result of the sample code above. It shows that PureHDF could read the data of the "COORDINATES" dataset:

grafik

Attributes work similar in that instead of calling .Dataset(<dataset name>) you would call .Attribute(<attribute name>). Attributes are very small datasets that often consists of single values. Conceptually attributes are some kind of metadata. You can also view them with HDFView.

grafik

I hope this helps you to get started.

Marco-Pellegrino commented 11 months ago

thanks a lot @Apollo3zehn !

It is definitely way easier to understand the structure now!

I also found the following way to get access to the dataset.

using var h5File = PureHDF.H5File.OpenRead(@"HDF5\recorder.hdf5");

var dataset = h5File.Dataset("/MODEL_STAGE[1]/RESULTS/ON_NODES/DISPLACEMENT/DATA/STEP_0");
var dimX = (long)dataset.Space.Dimensions[0];
var dimY = (long)dataset.Space.Dimensions[1];

var values = dataset.Read<double>().ToArray2D(dimX, dimY);

Your tool is super apreciated as I can finally read hdf5 file from my M2 mac

Apollo3zehn commented 11 months ago

Glad it helped you! You are right, your way is simpler :D

I am working on a simple writing implementation, too, and then I hopefully can finally release v1.