写一个boost :: multi_array的到HDF5数据集 [英] write a boost::multi_array to hdf5 dataset

查看:659
本文介绍了写一个boost :: multi_array的到HDF5数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可用,使编写C ++载体或升压任何库或标题:: multi_arrays到HDF5数据集容易?

我已经看过了HDF5 C ++的例子,他们只是使用C ++语法来调用C函数,他们只写静态C阵列的数据集(见create.cpp)。

我缺少的地步!?

提前

非常感谢,
亚当


解决方案

下面是怎么写的N维欧的multi_array 在HDF5格式S

下面是一个简单的例子:

 的#include<升压/ multi_array.hpp>
使用boost :: multi_array的;
使用boost ::程度;
//分配数组
INT NX = 5,NY = 6,NZ = 7;
multi_array的<双,3> float_data(范围[NX] [纽约] [NZ]);//初始化数组
为(中间体二= 0;!二= NX;ⅱ++)
    对于(INT JJ = 0; JJ = NY;!JJ ++)
        对于(INT KK = 0; KK = NZ;!KK ++)
            float_data [II] [林俊杰] [KK] = II +林俊杰+ KK;//
//写入HDF5格式
//
H5 :: H5File文件(SDS.h5,H5F_ACC_TRUNC);
write_hdf5(文件的doubleArray,float_data);

下面是 write_hdf5 code()

首先,我们必须映射C ++类型HDF5类型(从 H5 C ++ API)。我注释掉从而导致线路重复定义,因为某些< stdint.h> 类型(如 uint8_t有 )是标准类型的别名(如 unsigned char型

 的#include< cstdint>//!_______________________________________________________________________________________
//!
//!地图类型HDF5类型
//!
//!
//! \\ LG作者(04 2013年3月)
//!_______________________________________________________________________________________模板< typename的T>结构get_hdf5_data_type
{静态H5 :: predType类型()
    {
        // static_assert(假,未知HDF5数据类型);
        返回H5 :: predType :: NATIVE_DOUBLE;
    }
};
模板<>结构get_hdf5_data_type<焦炭> {H5 :: IntType上的类型{H5 :: predType :: NATIVE_CHAR}; };
//模板<>结构get_hdf5_data_type< unsigned char型> {H5 :: IntType上的类型{H5 :: predType :: NATIVE_UCHAR}; };
//模板<>结构get_hdf5_data_type<短> {H5 :: IntType上的类型{H5 :: predType :: NATIVE_SHORT}; };
//模板<>结构get_hdf5_data_type<无符号短> {H5 :: IntType上的类型{H5 :: predType :: NATIVE_USHORT}; };
//模板<>结构get_hdf5_data_type< INT> {H5 :: IntType上的类型{H5 :: predType :: NATIVE_INT}; };
//模板<>结构get_hdf5_data_type< unsigned int类型> {H5 :: IntType上的类型{H5 :: predType :: NATIVE_UINT}; };
//模板<>结构get_hdf5_data_type<长> {H5 :: IntType上的类型{H5 :: predType :: NATIVE_LONG}; };
//模板<>结构get_hdf5_data_type<无符号长> {H5 :: IntType上的类型{H5 :: predType :: NATIVE_ULONG}; };
模板<>结构get_hdf5_data_type<长长的> {H5 :: IntType上的类型{H5 :: predType :: NATIVE_LLONG}; };
模板<>结构get_hdf5_data_type<无符号长长> {H5 :: IntType上的类型{H5 :: predType :: NATIVE_ULLONG}; };
模板<>结构get_hdf5_data_type<&中int8_t GT; {H5 :: IntType上的类型{H5 :: predType :: NATIVE_INT8}; };
模板<>结构get_hdf5_data_type< uint8_t有> {H5 :: IntType上的类型{H5 :: predType :: NATIVE_UINT8}; };
模板<>结构get_hdf5_data_type< int16_t> {H5 :: IntType上的类型{H5 :: predType :: NATIVE_INT16}; };
模板<>结构get_hdf5_data_type< uint16_t> {H5 :: IntType上的类型{H5 :: predType :: NATIVE_UINT16}; };
模板<>结构get_hdf5_data_type< int32_t> {H5 :: IntType上的类型{H5 :: predType :: NATIVE_INT32}; };
模板<>结构get_hdf5_data_type<&uint32_t的GT; {H5 :: IntType上的类型{H5 :: predType :: NATIVE_UINT32}; };
模板<>结构get_hdf5_data_type<&的int64_t GT; {H5 :: IntType上的类型{H5 :: predType :: NATIVE_INT64}; };
模板<>结构get_hdf5_data_type< uint64_t中> {H5 :: IntType上的类型{H5 :: predType :: NATIVE_UINT64}; };
模板<>结构get_hdf5_data_type<浮动> {H5 :: FloatType类型{H5 :: predType :: NATIVE_FLOAT}; };
模板<>结构get_hdf5_data_type<双> {H5 :: FloatType类型{H5 :: predType :: NATIVE_DOUBLE}; };
模板<>结构get_hdf5_data_type<长双> {H5 :: FloatType类型{H5 :: predType :: NATIVE_LDOUBLE}; };

然后我们可以使用一点模板转发魔术做出正确类型的函数输出我们的数据。由于这是模板code,它需要生活在一个头文件,如果你要在你的程序的多个源文件输出HDF5数组:

<$p$p><$c$c>//!_______________________________________________________________________________________
//!
//! write_hdf5的multi_array
//!
//! \\作者利奥Goodstadt(2013年3月4日)
//!
//!_______________________________________________________________________________________
模板&LT; typename的T,性病::为size_t尺寸,类型名hdf5_data_type&GT;
无效do_write_hdf5(H5 :: H5File文件,常量标准::字符串&放大器; data_set_name,常量的boost :: multi_array的&LT; T,尺寸及GT;&放大器;数据hdf5_data_type&安培;数据类型)
{
    //小尾数为86
    // FloatType的数据类型(get_hdf5_data_type&LT; T&GT; ::类型());
    datatype.setOrder(H5T_ORDER_LE);    矢量&lt; hsize_t&GT;尺寸(data.shape(),data.shape()+尺寸);
    H5 ::数据空间数据空间(尺寸,dimensions.data());    H5 ::数据集的数据集= file.createDataSet(data_set_name,数据类型,数据空间);    dataset.write(数据。数据(),数据类型);
}模板&LT; typename的T,性病::为size_t尺寸&GT;
无效write_hdf5(H5 :: H5File文件,常量标准::字符串&放大器; data_set_name,常量的boost :: multi_array的&LT; T,尺寸及GT;&安培;数据)
{    get_hdf5_data_type&LT; T&GT; hdf_data_type;
    do_write_hdf5(文件,data_set_name,数据hdf_data_type.type);
}

Are there any libraries or headers available to make writing c++ vectors or boost::multi_arrays to HDF5 datasets easy?

I have looked at the HDF5 C++ examples and they just use c++ syntax to call c functions, and they only write static c arrays to their datasets (see create.cpp).

Am I missing the point!?

Many thanks in advance, Adam

解决方案

Here is how to write N dimension multi_arrays in HDF5 format

Here is a short example:

#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 );

Here is code for write_hdf5().

First, we must map c++ types to HDF5 types (from the H5 c++ api). I have commented out lines which lead to duplicate definitions because some of the <stdint.h> types (e.g. uint8_t) are aliases of standard types (e.g. 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    };  };

Then we can use a bit of template forwarding magic to make a function of the right type to output our data. Since this is template code, it needs to live in a header file if you are going to output HDF5 arrays from multiple source files in your programme:

//!_______________________________________________________________________________________
//!     
//!     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);
}

这篇关于写一个boost :: multi_array的到HDF5数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆