Allow for weak HDF5 array reading

This commit is contained in:
Guilhem Lavaux 2016-02-23 17:34:53 +01:00
parent 0174cdc8d6
commit 43d77feafe
2 changed files with 52 additions and 7 deletions

View File

@ -87,7 +87,7 @@ int main()
H5::Group g = f.createGroup("test_group"); H5::Group g = f.createGroup("test_group");
array_type A(boost::extents[2][3]); array_type A(boost::extents[2][3]);
array_type B; array_type B, Bprime(boost::extents[1][2]);
array3_type C(boost::extents[2][3][4]); array3_type C(boost::extents[2][3][4]);
arrayc_type D, E; arrayc_type D, E;
array_mys_type F(boost::extents[10]), G; array_mys_type F(boost::extents[10]), G;
@ -130,6 +130,7 @@ int main()
abort(); abort();
} }
std::cout << "Testing C " << std::endl;
try try
{ {
CosmoTool::hdf5_read_array(g, "test_data", C); CosmoTool::hdf5_read_array(g, "test_data", C);
@ -139,6 +140,23 @@ int main()
catch (const CosmoTool::InvalidDimensions&) catch (const CosmoTool::InvalidDimensions&)
{} {}
std::cout << "Testing Bprime " << std::endl;
try
{
CosmoTool::hdf5_read_array(g, "test_data", Bprime, false, true);
for (index i = 0; i != 1; i++)
for (index j = 0; j != 2; j++)
if (B[i][j] != Bprime[i][j]) {
std::cout << "Invalid array content in Bprime" << endl;
abort();
}
}
catch (const CosmoTool::InvalidDimensions&)
{
std::cout << "Bad! Dimensions should be accepted" << std::endl;
abort();
}
D.resize(boost::extents[2][3]); D.resize(boost::extents[2][3]);
D = A; D = A;

View File

@ -283,6 +283,20 @@ namespace CosmoTool {
} }
} }
template<typename ArrayType>
void hdf5_weak_check_array(ArrayType& data, std::vector<hsize_t>& dims) {
for (long i = 0; i < data.num_dimensions(); i++) {
if (data.index_bases()[i] < 0) {
// Negative indexes are not supported right now.
throw InvalidDimensions();
}
if (data.index_bases()[i]+data.shape()[i] > dims[i]) {
throw InvalidDimensions();
}
}
}
template<typename ArrayType> template<typename ArrayType>
typename boost::disable_if< typename boost::disable_if<
array_has_resize<ArrayType> array_has_resize<ArrayType>
@ -296,7 +310,7 @@ namespace CosmoTool {
template<typename ArrayType, typename hdf5_data_type> template<typename ArrayType, typename hdf5_data_type>
void hdf5_read_array_typed(H5::CommonFG& fg, const std::string& data_set_name, void hdf5_read_array_typed(H5::CommonFG& fg, const std::string& data_set_name,
ArrayType& data, ArrayType& data,
const hdf5_data_type& datatype, bool auto_resize = true) const hdf5_data_type& datatype, bool auto_resize = true, bool useBases = false)
{ {
H5::DataSet dataset = fg.openDataSet(data_set_name); H5::DataSet dataset = fg.openDataSet(data_set_name);
H5::DataSpace dataspace = dataset.getSpace(); H5::DataSpace dataspace = dataset.getSpace();
@ -310,18 +324,31 @@ namespace CosmoTool {
dataspace.getSimpleExtentDims(dimensions.data()); dataspace.getSimpleExtentDims(dimensions.data());
if (auto_resize) if (auto_resize)
hdf5_resize_array(data, dimensions); hdf5_resize_array(data, dimensions);
else else {
hdf5_check_array(data, dimensions); if (useBases) {
hdf5_weak_check_array(data, dimensions);
std::vector<hsize_t> memdims(data.shape(), data.shape() + data.num_dimensions());
H5::DataSpace memspace(memdims.size(), memdims.data());
std::vector<hsize_t> offsets(data.index_bases(), data.index_bases() + data.num_dimensions());
dataspace.selectHyperslab(H5S_SELECT_SET, memdims.data(), offsets.data());
dataset.read(data.data(), datatype, memspace, dataspace);
} else {
hdf5_check_array(data, dimensions);
}
}
dataset.read(data.data(), datatype); dataset.read(data.data(), datatype);
} }
template<typename ArrayType> template<typename ArrayType>
void hdf5_read_array(H5::CommonFG& fg, const std::string& data_set_name, ArrayType& data, bool auto_resize = true ) void hdf5_read_array(H5::CommonFG& fg, const std::string& data_set_name, ArrayType& data, bool auto_resize = true,
bool useBases = false )
{ {
typedef typename ArrayType::element T; typedef typename ArrayType::element T;
hdf5_read_array_typed(fg, data_set_name, data, get_hdf5_data_type<T>::type(), auto_resize); hdf5_read_array_typed(fg, data_set_name, data, get_hdf5_data_type<T>::type(), auto_resize, useBases);
} }