c++ - 将 boost::multi_array 写入 hdf5 数据集

标签 c++ boost vector hdf5

是否有任何库或 header 可用于将 C++ vector 或 boost::multi_arrays 写入 HDF5 数据集变得容易?

我看过 HDF5 C++ 示例,它们只是使用 c++ 语法调用 c 函数,并且它们只将静态 c 数组写入它们的数据集(请参阅 create.cpp)。

我是不是忽略了重点!?

非常感谢, 亚当

最佳答案

下面是N维multi_array的写法HDF5格式的s

这是一个简短的例子:

#include <boost/multi_array.hpp>
using boost::multi_array;
using boost::extents;


// allocate array
int NX = 5,  NY = 6,  NZ = 7;
multi_array<double, 3>  float_data(extents[NX][NY][NZ]);

// initialise the array
for (int ii = 0; ii != NX; ii++)
    for (int jj = 0; jj != NY; jj++)
        for (int kk = 0; kk != NZ; kk++)
            float_data[ii][jj][kk]  = ii + jj + kk;

// 
// write to HDF5 format
// 
H5::H5File file("SDS.h5", H5F_ACC_TRUNC);
write_hdf5(file, "doubleArray", float_data );

这是 write_hdf5() 的代码.

首先,我们必须将 c++ 类型映射到 HDF5 类型(来自 H5 c++ api)。我已经注释掉了导致重复定义的行,因为一些 <stdint.h>类型(例如 uint8_t )是标准类型(例如 unsigned char )的别名

#include <cstdint>

//!_______________________________________________________________________________________
//!     
//!     map types to HDF5 types
//!         
//!     
//!     \author lg (04 March 2013)
//!_______________________________________________________________________________________ 

template<typename T> struct get_hdf5_data_type
{   static H5::PredType type()  
    {   
        //static_assert(false, "Unknown HDF5 data type"); 
        return H5::PredType::NATIVE_DOUBLE; 
    }
};
template<> struct get_hdf5_data_type<char>                  {   H5::IntType type    {   H5::PredType::NATIVE_CHAR       };  };
//template<> struct get_hdf5_data_type<unsigned char>       {   H5::IntType type    {   H5::PredType::NATIVE_UCHAR      };  };
//template<> struct get_hdf5_data_type<short>               {   H5::IntType type    {   H5::PredType::NATIVE_SHORT      };  };
//template<> struct get_hdf5_data_type<unsigned short>      {   H5::IntType type    {   H5::PredType::NATIVE_USHORT     };  };
//template<> struct get_hdf5_data_type<int>                 {   H5::IntType type    {   H5::PredType::NATIVE_INT        };  };
//template<> struct get_hdf5_data_type<unsigned int>        {   H5::IntType type    {   H5::PredType::NATIVE_UINT       };  };
//template<> struct get_hdf5_data_type<long>                {   H5::IntType type    {   H5::PredType::NATIVE_LONG       };  };
//template<> struct get_hdf5_data_type<unsigned long>       {   H5::IntType type    {   H5::PredType::NATIVE_ULONG      };  };
template<> struct get_hdf5_data_type<long long>             {   H5::IntType type    {   H5::PredType::NATIVE_LLONG      };  };
template<> struct get_hdf5_data_type<unsigned long long>    {   H5::IntType type    {   H5::PredType::NATIVE_ULLONG     };  };
template<> struct get_hdf5_data_type<int8_t>                {   H5::IntType type    {   H5::PredType::NATIVE_INT8       };  };
template<> struct get_hdf5_data_type<uint8_t>               {   H5::IntType type    {   H5::PredType::NATIVE_UINT8      };  };
template<> struct get_hdf5_data_type<int16_t>               {   H5::IntType type    {   H5::PredType::NATIVE_INT16      };  };
template<> struct get_hdf5_data_type<uint16_t>              {   H5::IntType type    {   H5::PredType::NATIVE_UINT16     };  };
template<> struct get_hdf5_data_type<int32_t>               {   H5::IntType type    {   H5::PredType::NATIVE_INT32      };  };
template<> struct get_hdf5_data_type<uint32_t>              {   H5::IntType type    {   H5::PredType::NATIVE_UINT32     };  };
template<> struct get_hdf5_data_type<int64_t>               {   H5::IntType type    {   H5::PredType::NATIVE_INT64      };  };
template<> struct get_hdf5_data_type<uint64_t>              {   H5::IntType type    {   H5::PredType::NATIVE_UINT64     };  };
template<> struct get_hdf5_data_type<float>                 {   H5::FloatType type  {   H5::PredType::NATIVE_FLOAT      };  };
template<> struct get_hdf5_data_type<double>                {   H5::FloatType type  {   H5::PredType::NATIVE_DOUBLE     };  };
template<> struct get_hdf5_data_type<long double>           {   H5::FloatType type  {   H5::PredType::NATIVE_LDOUBLE    };  };

然后我们可以使用一些模板转发魔法来制作一个正确类型的函数来输出我们的数据。由于这是模板代码,如果您要从程序中的多个源文件输出 HDF5 数组,它需要存在于头文件中:

//!_______________________________________________________________________________________
//!     
//!     write_hdf5 multi_array
//!         
//!     \author leo Goodstadt (04 March 2013)
//!     
//!_______________________________________________________________________________________
template<typename T, std::size_t DIMENSIONS, typename hdf5_data_type>
void do_write_hdf5(H5::H5File file, const std::string& data_set_name, const boost::multi_array<T, DIMENSIONS>& data, hdf5_data_type& datatype)
{
    // Little endian for x86
    //FloatType datatype(get_hdf5_data_type<T>::type());
    datatype.setOrder(H5T_ORDER_LE);

    vector<hsize_t> dimensions(data.shape(), data.shape() + DIMENSIONS);
    H5::DataSpace dataspace(DIMENSIONS, dimensions.data());

    H5::DataSet dataset = file.createDataSet(data_set_name, datatype, dataspace);

    dataset.write(data.data(), datatype);
}

template<typename T, std::size_t DIMENSIONS>
void write_hdf5(H5::H5File file, const std::string& data_set_name, const boost::multi_array<T, DIMENSIONS>& data )
{

    get_hdf5_data_type<T> hdf_data_type;
    do_write_hdf5(file, data_set_name, data, hdf_data_type.type);
}

关于c++ - 将 boost::multi_array 写入 hdf5 数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9250237/

相关文章:

c++ - 在 C++11 中使用 auto 和 decltype

c++如何将文件解析为结构 vector

c++ - 如何使用 cin 从控制台读取整数到 vector 中

scala - 将 Scala 迭代器转换为向量

c++ - C11 独立内存屏障 LoadLoad StoreStore LoadStore StoreLoad

c++ - 为什么我不能将 C 样式的数组复制到 std::array?

java - Android NDK 写入 SQLite

c++ - 将对象放入 boost::property_tree 的正确方法是什么?

c++ - boost 日志 : How to prevent the output will be duplicated to all added streams when it uses the add_file_log() function?

c++ - boost 压缩矩阵基础知识