2012-02-12 21 views
6

¿Hay bibliotecas o cabeceras disponibles para hacer C++ vectores de escritura o impulsar :: multi_arrays a HDF5 conjuntos de datos fácil?escribir un impulso :: multi_array a HDF5 conjunto de datos

He examinado los ejemplos HDF5 en C++ y simplemente utilizan la sintaxis C++ para llamar a funciones c, y solo escriben matrices estáticas c en sus conjuntos de datos (vea crear.cpp).

¿Me falta el punto?

Muchas gracias de antemano, Adam

+1

sí sí sí, la API HDF5 C++ es horrible ** ** . Está muy cerca de la API C subyacente sin ningún intento de proporcionar una interfaz C++ útil. – Walter

Respuesta

1

estoy al tanto de ninguna. Las envolturas HDF5 C++ no son tan buenas, particularmente porque no permiten la combinación con HDF5 paralelo. Entonces, escribí mis propias envolturas en aproximadamente 2 horas y funciona bien. En última instancia, solo tendrá que llamarlo directamente (o indirectamente si elige hacer enlaces C++).

Afortunadamente, tanto los vectores y multi_arrays son contiguos en el almacenamiento, por lo que sólo pueden pasar los datos de ellos directamente en las llamadas a funciones HDF5.

+0

Hola, de acuerdo, gracias por avisarme. ¡Tendré que continuar con eso! ;-) – AdamC

5

Aquí es cómo escribir dimensión N multi_array s en formato HDF5

Aquí está un ejemplo corto:

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

Aquí es código para write_hdf5().

En primer lugar, debemos asignar tipos de C++ a tipos de HDF5 (desde la API de H5). He comentado las líneas que conducen a duplicar las definiciones porque algunos de los <stdint.h> tipos (por ejemplo uint8_t) son los alias de tipo estándar (por ejemplo 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 }; }; 

entonces podemos usar un poco de reenvío de plantillas mágica para hacer una función del tipo correcto para dar salida a nuestros datos. Dado que este es el código de plantilla, tiene que vivir en un fichero de cabecera si se va a matrices HDF5 salida de varios archivos de origen en su programa:

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

Quizás quiso decir usar 'plantilla <> struct {get_hdf5_data_type estática tipo H5 :: IntType() {return H5 :: :: PredType NATIVE_CHAR; }}; '? –

Cuestiones relacionadas