【问题标题】:Get the dimensions of a HDF5 dataset获取 HDF5 数据集的维度
【发布时间】:2013-03-25 01:50:00
【问题描述】:

我在我的 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,但这不适合我的情况。

有人知道怎么做吗?

【问题讨论】:

    标签: c++ c hdf5


    【解决方案1】:

    为此,您需要使用以 H5S 为前缀的 dataspace 函数。

    HDF5 reference manual 使用这些前缀进行组织,因此有助于理解这一点。

    如何获取数据集的维度

    首先你需要使用H5Dget_space从你的数据集中获取数据空间:

    hid_t dspace = H5Dget_space(dset);
    

    如果你的数据空间是simple(即不是null或scalar),那么你可以使用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。

    【讨论】:

      【解决方案2】:

      或者,可以这样完成(如果是简单的数据空间,请参阅 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++ reference manual 的examples page (readdata.cpp)。

      使用h5c++ 编译所有内容,它应该可以工作。

      【讨论】:

        猜你喜欢
        • 2017-09-06
        • 2017-12-01
        • 2018-03-25
        • 1970-01-01
        • 1970-01-01
        • 2018-10-28
        • 2020-06-21
        • 2022-10-12
        • 2017-09-17
        相关资源
        最近更新 更多