使用C#中的HDF5获取实时数据

时间:2015-11-03 06:01:36

标签: c# hdf5

我可以使用.net版本的HDF5存储HDF5数据。问题是,数据是在一个数组中,然后我把它写到HDF5文件。

有没有办法从C#实时连续向HDF5添加数据?从我在互联网上看到的,这是由数据包表处理的,但我不知道它是dotnet端口的一部分?

2 个答案:

答案 0 :(得分:1)

在hdf5中,您可以将数据集附加到现有数据集。使用Hdf.Pinvoke库的低级方法实现它需要做很多工作。我创建了一个库,在这些方法周围添加了一个层,以便更容易地读取和写入hdf5文件。 HDF5DotNetTools。它仍然是早期版本,但您可以将数据附加到文件。 有一个示例将数据附加到自述文件中的现有数据以及项目的单元测试中。

以下是代码的一部分:

int fileId = Hdf5.CreateFile(filename);    
// Create a dataset and append two more datasets to it. 
// The dsets list are three 10 by 5 matrices
using (var chunkedDset = new ChunkedDataset<double>("/test", fileId, dsets.First()))
{
  foreach (var ds in dsets.Skip(1))
    chunkedDset.AppendDataset(ds);
}
Hdf5.CloseFile(fileId);

答案 1 :(得分:0)

要连续不断地向HDF5中实时添加数据(并且不知道先验先验存在多少行数据),您需要使用可扩展数据集。

假设您未绑定到特定的API,请查看HDFql,因为它可以极大地减轻您对HDF5的低级详细了解。在C#中使用HDFql,您的用例可以按以下方式解决(这只是一个示例-我们假设您有一个名为acquire的函数,该函数用时间戳(UNIX纪元时间)填充变量values,并且获取的值):

using System.Runtime.InteropServices;
using AS.HDFql;

// declare structures
[StructLayout(LayoutKind.Sequential, Pack = 0)]
struct Data
{
    public int timestamp;
    public float reading;
}

public class Example
{
    public static void Main(string []args)
    {
        // declare variables
        Data values;
        int number;

        // create an HDF5 file named 'data.h5'
        HDFql.Execute("CREATE FILE data.h5");

        // use (i.e. open) HDF5 file 'data.h5'
        HDFql.Execute("USE FILE data.h5");

        // create a dataset named 'dset' of data type compound composed of two members named 'timestamp' (of data type int containing a UNIX Epoch Time)
        // and 'reading' (of data type float containing an acquired value). The dataset starts with 0 rows and can grow (i.e. be extended) in an unlimited fashion
        HDFql.Execute("CREATE DATASET dset AS COMPOUND(timestamp AS INT, reading AS FLOAT)(0 TO UNLIMITED)");

        // register variable 'values' for subsequent use (by HDFql)
        number = HDFql.VariableRegister(values);

        // call hypothetical function 'acquire' that populates variable 'values' with a timestamp and an acquired value
        while(acquire(values))
        {
            // alter (i.e. change) dimension of dataset 'dset' to +1 (i.e. add a new row at the end of 'dset')
            HDFql.Execute("ALTER DIMENSION dset TO +1");

            // insert (i.e. write) data from variable 'values' into the last row of dataset 'dset' (thanks to a point selection)
            HDFql.Execute("INSERT INTO dset(-1) VALUES FROM MEMORY " + number);
        }
    }
}

请检查HDFql reference manual,以获取有关如何使用此库处理可扩展数据集的更多信息。