You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C++中读取HDF5文件数据集的指定帧分块数据

HDF5单帧分块数据读取问题(C++ h5xx库)

我需要从HDF5文件的单个数据集中读取对应单帧的分块数据,整个数据集维度为(10, 11214, 3),共10帧,目标单帧分块维度为(11214, 3)。目前我已经可以正常打印预定义数组,但不清楚如何从HDF5文件中读取指定的分块数据。我使用的是HDF5库的模板化C++封装库h5xx以及boost库,目标数据集存储路径为particles/lipids/box/positions,其中的value数据集存储了所有帧数据。

现有代码如下:

#include <h5xx/h5xx.hpp>
#include <boost/multi_array.hpp>
#include <iostream>
#include <vector>
#include <cstdio>

typedef boost::multi_array<int, 2> array_2d_t;

const int NI=10;
const int NJ=NI;

void print_array(array_2d_t const& array)
{
    for (unsigned int j = 0; j < array.shape()[1]; j++)
    {
        for (unsigned int i = 0; i < array.shape()[0]; i++)
        {
            printf("%2d ", array[j][i]);
        }
        printf("\n");
    }
}
void write_int_data(std::string const& filename, array_2d_t const& array)
{
    h5xx::file file(filename, h5xx::file::trunc);
    std::string name;

    {
        // --- 创建数据集并写入默认数组数据(正值)
        name = "integer array";
        h5xx::create_dataset(file, name, array);
        h5xx::write_dataset(file, name, array);

        // --- 创建切片对象(即 hyperslab),指定数据集中需要覆盖写入的位置
        std::vector<int> offset; int offset_raw[2] = {4,4}; offset.assign(offset_raw, offset_raw + 2);
        std::vector<int> count;  int count_raw[2] = {2,2}; count.assign(count_raw, count_raw + 2);
        h5xx::slice slice(offset, count);
    }
}
void read_int_data(std::string const& filename)
{
    h5xx::file file(filename, h5xx::file::in);
    std::string name = "integer array";

    // 读取并打印完整数据集
    {
        array_2d_t array;
        // --- 将完整数据集读取到array中,array会在内部自动重设大小并覆写
        h5xx::read_dataset(file, name, array);
        printf("从文件读取的原始整数数组,负数区域是通过切片写入的\n");
        print_array(array);
        printf("\n");
    }
}
int main(int argc, char** argv)
{
    std::string filename = argv[0];
    filename.append(".h5");

    // --- 几组整数类型的演示/测试
    {
        array_2d_t array(boost::extents[NJ][NI]);
        {
            const int nelem = NI*NJ;
            int data[nelem];
            for (int i = 0; i < nelem; i++)
                data[i] = i;
            array.assign(data, data + nelem);
        }

        write_int_data(filename, array);

        read_int_data(filename);
    }

    return 0;
}

内容的提问来源于stack exchange,提问作者Mahesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 13:15:04