[英]Get the dimensions of a HDF5 dataset
我在我的C ++程序中使用了一些HDF5文件,我對H5Dopen
函數有疑問。 是否可以在給定文件中獲取hdf5數據集的維度?
hid_t file, dset;
herr_t status;
file = H5Fopen (filenameField, H5F_ACC_RDONLY, H5P_DEFAULT);
dset = H5Dopen (file, "/xField", H5P_DEFAULT);
在我做下一行之前,我想得到dset
的尺寸。
status = H5Dread (dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &readBuf[0]);
我只找到了H5Dget_storage_size
,但這不適合我的情況。
有誰知道怎么做?
為此,您需要使用以H5 S為前綴的數據空間函數。
HDF5參考手冊使用這些前綴進行組織,因此有助於理解這一點。
首先,您需要使用H5Dget_space
從數據集中獲取數據空間:
hid_t dspace = H5Dget_space(dset);
如果您的數據空間很簡單 (即非空或標量 ),那么您可以使用H5Sget_simple_extent_ndims
獲取維H5Sget_simple_extent_ndims
:
const int ndims = H5Sget_simple_extent_ndims(dspace);
以及使用H5Sget_simple_extent_dims
的每個維度的大小:
hsize_t dims[ndims];
H5Sget_simple_extent_dims(dspace, dims, NULL);
尺寸現在存儲在dims
。
或者,這可以這樣做(如果是簡單的數據空間,請參閱Simons答案,如果需要,請查看bool H5::DataSpace::isSimple() const
):
#include "H5Cpp.h"
using namespace H5;
//[...]
DataSpace dataspace(RANK, dims);
//[...]
/*
* Get the number of dimensions in the dataspace.
*/
const int rank = dataspace.getSimpleExtentNdims();
在大多數情況下,這條線可能是多余的,因為整個任務可以分為兩行:
/*
* Get the dimension size of each dimension in the dataspace and
* store the dimentionality in ndims.
*/
hsize_t dims_out[rank];
const int ndims = dataspace.getSimpleExtentDims( dims_out, NULL);
函數getSimpleExtentNdims()
可以作為H5::DataSpace
實例的成員調用。
這些代碼段來自最近的HDF5 C ++參考手冊的示例頁面 (readdata.cpp)。
用h5c++
編譯所有東西,它應該工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.