How to Create Compound Dataset on HDF5 C#


down vote
i need to write some data to a HDF5 file, i have a list of objects of the class “OPC_UA” inside a “BV” class, i want to create a dataset with the name of the BV and inside the dataset i want to have the list of the “OPC_UA” objects, tha OPC_UA Class have 3 attributes, a Long int ,one float value and a Datetime that i will convert to a timestamp, i can’t seem to make it work, the Marshal.SizeOf() doesn’t work with classes but i cant do it as a struct… Here is my code:

public void CriaHDF5Customizado(PackingConfigFile pf)
H5FileId fileId = H5F.create(“pmdfq.h5”, H5F.CreateMode.ACC_TRUNC);

    H5GroupId infoGroupId = H5G.create(fileId, "informations");
    H5GroupId datasetGroupId = H5G.create(fileId, "datasets");

    long[] dims = new long[1];

    foreach(BasicVariable bv in pf.basicVariableList.bvList)
        OPC_UA aux = new OPC_UA();
        var xx = bv.bvData;
        int tamanho = Marshal.SizeOf(typeof(OPC_UA));
        dims[0] = (long)bv.bvData.Count;
        // dims[1] = (long)4;
        H5DataSpaceId spaceId = H5S.create(H5S.H5SClass.SCALAR);
        H5DataTypeId dataTypeId = H5T.create(H5T.CreateClass.COMPOUND, Marshal.SizeOf(typeof(OPC_UA)));

        H5T.insert(dataTypeId, "TimeStamp", 0, new H5DataTypeId(H5T.H5Type.NATIVE_UINT));
        H5T.insert(dataTypeId, "Quality", Marshal.SizeOf(H5T.H5Type.NATIVE_UINT), new H5DataTypeId(H5T.H5Type.NATIVE_UINT));
        H5T.insert(dataTypeId, "Value", 2* Marshal.SizeOf(H5T.H5Type.NATIVE_UINT), new H5DataTypeId(H5T.H5Type.NATIVE_INT));

        H5DataSetId dataSetId = H5D.create(datasetGroupId, bv.bvTag, dataTypeId, spaceId);

        //H5D.write(dataSetId, new H5DataTypeId(H5T.H5Type.STD_REF_OBJ), new H5Array<OPC_UA>(bv.bvData.ToArray()));
        H5D.writeScalar(dataSetId, dataTypeId, ref xx);


And this is the OPC UA Class

public class OPC_UA
public DateTime timeStamp { get; set; }
public string data { get; set; }
public Int64 quality { get; set; }

public OPC_UA(DateTime? ts = null ,string dt = "",Int64 qlt = -99)
    if (!ts.HasValue)
        timeStamp = DateTime.Now;
    data = dt;
    quality = qlt;


Hello Arthuro,

You may find some C# examples among the HDF-forum archives. I found this one that looks like it could be helpful:

Compound Dataset from C# Struct to HDF File



I have already seen this post and i didn’t want to use a struct as i already have a complete implemented class into my project… Thank you for the answer, i’m having problem on creating the dataset, setting the Type and Space, i can’t seem to understand what that thing does, if it is allocating the space for 1 just row or allocating the entire list