2

I started to use this https://github.com/HDFGroup/HDF.PInvoke to work with HDF5 files. But documentation is very poor. I want to read dataset. I use this function: H5D.read(...). The last parameter of this function is buf which type is System.IntPtr. And I have problems with that parameter. Could you help me with example of how to read the entire dataset and how to define this parameter? The Dataset I want to read is 3-dimensional array of float. I write in VB.net, but example in C# will be fine too. Thanks a lot.

1 Answers1

0

So, I wrote a solution based on this: https://github.com/HDFGroup/HDF.PInvoke/wiki/Cookbook-:-Strings

In the above link 1D dataset of strings is read.

My solution is universal (except BitConverter part) and can read datasets of various dimensions and datatypes:

    Dim dsID = H5D.open(hdf5fileId, dsname, H5P.DEFAULT)

    Dim spaceID = H5D.get_space(dsID)
    Dim typeID = H5D.get_type(dsID)
    Dim rank = H5S.get_simple_extent_ndims(spaceID)
    Dim dims(rank - 1) As ULong
    Dim maxDims(rank - 1) As ULong
    H5S.get_simple_extent_dims(spaceID, dims, maxDims)
    Dim sizeData = H5T.get_size(typeID)
    Dim size = sizeData.ToInt32()
    Dim bytearray_elements = 1
    For i = 0 To dims.Length - 1
        bytearray_elements *= dims(i)
    Next
    Dim dataBytes(bytearray_elements * CULng(size)) As Byte

    Dim pinnedArray As GCHandle = GCHandle.Alloc(dataBytes, GCHandleType.Pinned)

    H5D.read(dsID, typeID, H5S.ALL, H5S.ALL, H5P.DEFAULT, pinnedArray.AddrOfPinnedObject())
    pinnedArray.Free()

    ' Read all dataset in loop, val by val
    For i = 0 To bytearray_elements-1:
        Dim slice = idlist_byte.Skip(i * size).Take(size).ToArray()
        Dim val = BitConverter.ToSingle(slice, 0)
    Next