/*+ This is CosmoTool (./src/hdf5_array.hpp) -- Copyright (C) Guilhem Lavaux (2007-2014) guilhem.lavaux@gmail.com This software is a computer program whose purpose is to provide a toolbox for cosmological data analysis (e.g. filters, generalized Fourier transforms, power spectra, ...) This software is governed by the CeCILL license under French law and abiding by the rules of distribution of free software. You can use, modify and/ or redistribute the software under the terms of the CeCILL license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info". As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license, users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability. In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software, that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge. Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured and, more generally, to use and operate it in the same conditions as regards security. The fact that you are presently reading this means that you have had knowledge of the CeCILL license and that you accept its terms. +*/ #ifndef __COSMO_HDF5_ARRAY_HPP #define __COSMO_HDF5_ARRAY_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include namespace CosmoTool { //!_______________________________________________________________________________________ //! //! map types to HDF5 types //! //! //! Leo Goodstadt (04 March 2013), improved with enable_if by Guilhem Lavaux (May 2014) //!_______________________________________________________________________________________ template struct get_hdf5_data_type { static H5::DataType type() { BOOST_MPL_ASSERT_MSG(0, Unknown_HDF5_data_type, ()); return H5::PredType::NATIVE_DOUBLE; } }; #define HDF5_TYPE(tl, thdf5) \ template struct get_hdf5_data_type >::type> \ { static H5::DataType type() { return H5::PredType::thdf5; }; } HDF5_TYPE(char , NATIVE_CHAR); HDF5_TYPE(long long , NATIVE_LLONG); HDF5_TYPE(unsigned long long, NATIVE_ULLONG); HDF5_TYPE(int8_t , NATIVE_INT8); HDF5_TYPE(uint8_t , NATIVE_UINT8); HDF5_TYPE(int16_t , NATIVE_INT16); HDF5_TYPE(uint16_t , NATIVE_UINT16); HDF5_TYPE(int32_t , NATIVE_INT32); HDF5_TYPE(uint32_t , NATIVE_UINT32); HDF5_TYPE(int64_t , NATIVE_INT64); HDF5_TYPE(uint64_t , NATIVE_UINT64); HDF5_TYPE(float , NATIVE_FLOAT); HDF5_TYPE(double , NATIVE_DOUBLE); HDF5_TYPE(long double , NATIVE_LDOUBLE); #undef HDF5_TYPE //!_______________________________________________________________________________________ //! //! write_hdf5 multi_array //! //! \author leo Goodstadt (04 March 2013) //! //!_______________________________________________________________________________________ template void hdf5_write_array(H5::CommonFG& fg, const std::string& data_set_name, const boost::multi_array& data, const hdf5_data_type& datatype) { std::vector dimensions(data.shape(), data.shape() + DIMENSIONS); H5::DataSpace dataspace(DIMENSIONS, dimensions.data()); H5::DataSet dataset = fg.createDataSet(data_set_name, datatype, dataspace); dataset.write(data.data(), datatype); } /* HDF5 complex type */ template class hdf5_ComplexType { public: H5::CompType type; hdf5_ComplexType() : type(sizeof(std::complex)) { get_hdf5_data_type hdf_data_type; type.insertMember("r", 0, hdf_data_type.type()); type.insertMember("i", sizeof(T), hdf_data_type.type()); type.pack(); } static const hdf5_ComplexType *ctype() { static hdf5_ComplexType singleton; return &singleton; } }; template void hdf5_write_array(H5::CommonFG& fg, const std::string& data_set_name, const boost::multi_array& data ) { get_hdf5_data_type hdf_data_type; hdf5_write_array(fg, data_set_name, data, hdf_data_type.type()); } template void hdf5_write_array(H5::CommonFG& fg, const std::string& data_set_name, const boost::multi_array, DIMENSIONS>& data ) { hdf5_write_array(fg, data_set_name, data, hdf5_ComplexType::ctype()->type); } // HDF5 array reader // // Author Guilhem Lavaux (May 2014) class InvalidDimensions: virtual std::exception { }; template struct hdf5_extent_gen { typedef typename boost::detail::multi_array::extent_gen type; static inline type build(hsize_t *d) { return (hdf5_extent_gen::build(d))[d[r-1]]; } }; template<> struct hdf5_extent_gen<0> { static inline boost::multi_array_types::extent_gen build(hsize_t *d) { return boost::extents; } }; template void hdf5_read_array(H5::CommonFG& fg, const std::string& data_set_name, boost::multi_array& data, const hdf5_data_type& datatype) { H5::DataSet dataset = fg.openDataSet(data_set_name); H5::DataSpace dataspace = dataset.getSpace(); std::vector dimensions(DIMENSIONS); if (dataspace.getSimpleExtentNdims() != DIMENSIONS) { throw InvalidDimensions(); } dataspace.getSimpleExtentDims(dimensions.data()); data.resize(hdf5_extent_gen::build(dimensions.data())); dataset.read(data.data(), datatype); } template void hdf5_read_array(H5::CommonFG& fg, const std::string& data_set_name, boost::multi_array& data ) { get_hdf5_data_type hdf_data_type; hdf5_read_array(fg, data_set_name, data, hdf_data_type.type()); } template void hdf5_read_array(H5::CommonFG& fg, const std::string& data_set_name, boost::multi_array, DIMENSIONS>& data ) { hdf5_read_array(fg, data_set_name, data, hdf5_ComplexType::ctype()->type); } #define CTOOL_HDF5_NAME(STRUCT) BOOST_PP_CAT(hdf5_,STRUCT) #define CTOOL_HDF5_INSERT_ELEMENT(r, STRUCT, element) \ { \ ::CosmoTool::get_hdf5_data_type t; \ position = HOFFSET(STRUCT, BOOST_PP_TUPLE_ELEM(2, 1, element)); \ const char *field_name = BOOST_PP_STRINGIZE(BOOST_PP_TUPLE_ELEM(2, 1, element)); \ type.insertMember(field_name, position, t.type()); \ } #define CTOOL_STRUCT_TYPE(STRUCT, TNAME, ATTRIBUTES) \ namespace CosmoTool { \ class TNAME { \ public: \ H5::CompType type; \ \ TNAME() : type(sizeof(STRUCT)) \ { \ long position; \ BOOST_PP_SEQ_FOR_EACH(CTOOL_HDF5_INSERT_ELEMENT, STRUCT, ATTRIBUTES) \ } \ \ static const TNAME *ctype() \ { \ static TNAME singleton; \ return &singleton; \ } \ }; \ template<> struct get_hdf5_data_type { \ static H5::DataType type() { return TNAME::ctype()->type; }; \ }; \ }; #define CTOOL_ARRAY_TYPE(ARRAY_TYPE, DIM, TNAME) \ namespace CosmoTool { \ class TNAME { \ public: \ H5::ArrayType *type; \ \ TNAME() \ { \ hsize_t dims[1] = { DIM }; \ type = new H5::ArrayType(get_hdf5_data_type::type(), 1, dims); \ } \ ~TNAME() { delete type; } \ \ static const TNAME *ctype() \ { \ static TNAME singleton; \ return &singleton; \ } \ }; \ \ template<> struct get_hdf5_data_type< ARRAY_TYPE[DIM] > { \ static H5::DataType type() { return *(TNAME::ctype()->type); }; \ }; \ }; }; #endif