将boost::multi_array写入hdf5数据集
write a boost::multi_array to hdf5 dataset
是否有任何库或头可以轻松地将c++向量或boost::multi_arrays写入HDF5数据集?
我看过HDF5 C++的例子,它们只使用C++语法来调用C函数,并且只将静态C数组写入数据集(请参见create.cpp)
我是不是没有抓住要点!?
非常感谢,Adam
以下是如何以HDF5格式写入N维multi_array
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);
}
我对此一无所知。HDF5C++包装器不是很好,特别是因为它们不允许与并行HDF5组合。所以,我在大约2个小时内写下了自己的包装纸,效果很好。最终,您只需要直接调用它(如果您选择制作C++绑定,则可以间接调用它)。
幸运的是,向量和multi_array在存储中都是连续的,所以您可以直接将它们的数据传递到HDF5函数调用中。
相关文章:
- 写入向量<向量<bool>>
- 如何将内容数组写入文本文件?
- 写入位置0x0000000C时发生访问冲突
- 使用ios:ate写入到流会覆盖现有文件
- 独立读取-修改-写入顺序
- 为什么在某些情况下不写入此文件?
- 用C++将哈希表写入文件并从文件中恢复
- std::当在256字节边界上写入整数时,流的奇怪行为
- 为什么指针不写入类的地址?
- 如何打开并写入一个名称取自C++中字符串的文件
- 如何在C++中从文件中读取/写入多个对象
- 链表中写入访问冲突的未知原因
- HDF5-写入/附加HDF5复合数据类型一一带有HyperSlab
- HDF5 C++:在复合数据集中写入字符串
- 将boost::multi_array写入hdf5数据集
- HDF5在磁盘上将32位无符号int写入64位,并读取32位无信号int
- 无法写入矢量大于 2^13 的 HDF5 文件
- 隐藏复合数据类型的某些字段,使其不被写入(或从hdf5文件中读取)
- 如何智能地将多个参数写入hdf5文件
- 以创建HDF5文件的相同顺序读取/写入HDF5文件