简体   繁体   English

从HDF5文件读取单个元素

[英]Reading a single element from HDF5 file

Could you please provide me with an example of reading a single element from HDF5 file using HDF5DotNet library. 您能否提供一个使用HDF5DotNet库从HDF5文件中读取单个元素的示例。 I know how to read the full array into the memory and select the necessary element by index. 我知道如何将整个数组读入内存并按索引选择必要的元素。 The problem that I don't want to read the full array and would like to use the H5S.selectElements method ( http://hdf5.net/api/M_HDF5DotNet_H5S_selectElements_4_9e6f2387.aspx ). 我不想读取完整的数组,而是想使用H5S.selectElements方法( http://hdf5.net/api/M_HDF5DotNet_H5S_selectElements_4_9e6f2387.aspx )。
So far I've created the following: 到目前为止,我已经创建了以下内容:

H5.Open();
H5FileId fileId = H5F.open(this.filePath, H5F.OpenMode.ACC_RDONLY);
var dSet = H5D.open(fileId, "/Link");
var dSpace = H5D.getSpace(dSet);
var dDims = H5S.getSimpleExtentDims(dSpace);
var dType = H5D.getType(dSet);

H5S.selectElements(dSpace, H5S.SelectOperator.SET, InpPtr numElements,long [] coord);

I can not figure out how to define the numElements parameter and coord of the element (I have 3 dimensional array). 我不知道如何定义numElements参数和元素的坐标(我有3维数组)。

Using the HDF5 support doc 使用HDF5支持文档

Assuming your DataSet is As Follows: (2D for simplicity explaining how the parameters work) 假设您的DataSet如下:(为简单起见,二维显示了参数的工作原理)

1 2 3
4 5 6
7 8 9

And you wanted to select the 6 and only the 6 . 而您想选择66 That means that you want one element located in the second row and the third column. 这意味着您希望一个元素位于第二行和第三列。 (One Indexed). (一个索引)。

That means that you will need a 1 by 2 array of points in the selection array. 这意味着在选择数组中将需要一个1 x 2的点数组。 (Zero Indexed). (零索引)。

1 2

If you wanted to select the 7 also, then the selection array would look like this. 如果还要选择7,则选择数组将如下所示。 (Zero Indexed). (零索引)。

1 2 2 0

The related calls would be: 相关电话为:

H5S.selectElements(dSpace, H5S.SelectOperator.SET, 1, new long [] { 1, 2 });
H5S.selectElements(dSpace, H5S.SelectOperator.SET, 2, new long [] { 1, 2, 2, 0 });

Translating to a 3D dataspace, we just add an extra value to the selection array. 转换为3D数据空间后,我们只需向选择数组添加一个额外的值。

That is: 那是:

1 2 0

Will select the point (2,3,1) 将选择点(2,3,1)

If you wanted to select a second value, then the selection array would look like this. 如果要选择第二个值,则选择数组将如下所示。 (Zero Indexed). (零索引)。

1 2 0 1 2 1

Will select the points (2,3,1), (2,3,2). 将选择点(2,3,1),(2,3,2)。

The related calls would be: 相关电话为:

H5S.selectElements(dSpace, H5S.SelectOperator.SET, 1, new long [] { 1, 2, 0 });
H5S.selectElements(dSpace, H5S.SelectOperator.SET, 2, new long [] { 1, 2, 0, 1, 2, 1 });

Note : That I have never used HDF5, describing call based on documentation, so there may be errors. 注意 :我从未使用过HDF5,而是根据文档描述了调用,因此可能会出现错误。

Using HDFql in C#, reading one particular element of a three dimensional dataset can be done as follows (assume that the dataset is called my_dataset and the element to read is at position 2, 3 and 5 in the first, second and third dimension respectively): 使用C#中的HDFql ,可以按以下步骤读取三维数据集的一个特定元素(假设该数据集称为my_dataset并且要读取的元素分别位于第一维,第二维和第三维的位置2、3和5) :

HDFql.Execute("SELECT FROM my_dataset(2, 3, 5)");

From there, you can retrieve the element by doing the following (assume that my_dataset is of datatype integer): 从那里,您可以通过执行以下操作来检索元素(假设my_dataset的数据类型为整数):

HDFql.CursorFirst();
System.Console.WriteLine("Element is " + HDFql.CursorGetInt());

I've found another approach to solve the problem - using H5S.selectHyperslab method. 我发现了另一种解决问题的方法-使用H5S.selectHyperslab方法。 Maybe it's not so elegant but it seems to work fine. 也许它不是那么优雅,但似乎工作正常。

H5.Open();
H5FileId fileId = H5F.open(this.filePath, H5F.OpenMode.ACC_RDONLY);
var dSet = H5D.open(fileId, "/Link");
var dSpace = H5D.getSpace(dSet);
var dDims = H5S.getSimpleExtentDims(dSpace);
var dType = H5D.getType(dSet);
//E.g. to extract the value with coordinates [0,1,0]:
H5DataSpaceId memspaceid = H5S.create_simple(1, new long[] { 1, 1, 1 });
H5S.selectHyperslab(memspaceid, H5S.SelectOperator.SET, new long[] { 0, 1, 0 }, new long[] { 1, 1, 1 });
H5S.selectHyperslab(dSpace, H5S.SelectOperator.SET, new long[] { 0, 1, 0 }, new long[] { 1, 1, 1 });

//array to read data
double[] readDataBank = new double[1];
H5DataTypeId typeId = new H5DataTypeId(H5T.H5Type.NATIVE_DOUBLE);
H5D.read(dSet, dType, memspaceid, dSpace,new H5PropertyListId(new H5P.Template()), new H5Array<double>(readDataBank));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM