inline typename boost::enable_if<boost::mpl::and_< is_vector<T>, boost::is_fundamental<typename T::value_type> >, T>::type read_attribute(H5::H5Object const& object, std::string const& name) { typedef typename T::value_type value_type; H5::Attribute attr; try { H5XX_NO_AUTO_PRINT(H5::AttributeIException); attr = object.openAttribute(name); } catch (H5::AttributeIException const&) { throw; } H5::DataSpace ds(attr.getSpace()); if (!ds.isSimple()) { throw H5::AttributeIException("H5::attribute::as", "incompatible dataspace"); } size_t size = ds.getSimpleExtentNpoints(); std::vector<value_type> value(size); attr.read(ctype<value_type>::hid(), &*value.begin()); return value; }
inline typename boost::enable_if<is_multi_array<T>, T>::type read_attribute(H5::H5Object const& object, std::string const& name) { typedef typename T::element value_type; enum { rank = T::dimensionality }; H5::Attribute attr; try { H5XX_NO_AUTO_PRINT(H5::AttributeIException); attr = object.openAttribute(name); } catch (H5::AttributeIException const&) { throw; } H5::DataSpace ds(attr.getSpace()); if (!has_rank<rank>(attr)) { throw H5::AttributeIException("H5::attribute::as", "incompatible dataspace"); } hsize_t dim[rank]; ds.getSimpleExtentDims(dim); boost::array<size_t, rank> shape; std::copy(dim, dim + rank, shape.begin()); boost::multi_array<value_type, rank> value(shape); attr.read(ctype<value_type>::hid(), value.origin()); return value; }
inline typename boost::enable_if<boost::mpl::and_< is_vector<T> , boost::is_same<typename T::value_type, std::string> >, T>::type read_attribute(H5::H5Object const& object, std::string const& name) { H5::Attribute attr; try { H5XX_NO_AUTO_PRINT(H5::AttributeIException); attr = object.openAttribute(name); } catch (H5::AttributeIException const&) { throw; } H5::DataSpace ds(attr.getSpace()); if (!ds.isSimple()) { throw H5::AttributeIException("H5::attribute::as", "incompatible dataspace"); } size_t size = ds.getSimpleExtentNpoints(); H5::DataType tid = attr.getDataType(); if (tid.isVariableStr()) { throw error("reading non-scalar attribute of variable-length strings not supported"); } size_t str_len = tid.getSize(); // read to contiguous buffer and copy to std::vector std::vector<char> buffer(str_len * size); attr.read(tid, &*buffer.begin()); T value; value.reserve(size); char const* s = buffer.data(); for (size_t i = 0; i < size; ++i, s += str_len) { size_t len = strnlen(s, str_len); // strings of str_len size are not '\0'-terminated value.push_back(std::string(s, len)); // copy len bytes from buffer } return value; }
/// Return the attribute name of obj, and "" if the attribute does not exist. inline std::string read_string_attribute (H5::H5Object const * obj, std::string name ) { std::string value =""; H5::Attribute attr; if (H5LTfind_attribute(obj -> getId(), name.c_str() )==0) return value;// not present // can not find how to get the size with hl. Using full interface //herr_t err2 = H5LTget_attribute_string(gr.getId(), x.c_str(), name.c_str() , &(buf.front()) ) ; //value.append( &(buf.front()) ); try { attr= obj->openAttribute(name.c_str());} catch (H5::AttributeIException) { return value;} try { H5::DataSpace dataspace = attr.getSpace(); int rank = dataspace.getSimpleExtentNdims(); if (rank != 0) TRIQS_RUNTIME_ERROR << "Reading a string attribute and got rank !=0"; size_t size = attr.getStorageSize(); H5::StrType strdatatype(H5::PredType::C_S1, size+1); std::vector<char> buf(size+1, 0x00); attr.read(strdatatype, (void *)(&buf[0])); value.append( &(buf.front()) ); } TRIQS_ARRAYS_H5_CATCH_EXCEPTION; return value; }
void DataSet::readAttr(const H5::Attribute &attr, H5::DataType mem_type, const NDSize &size, std::string *data) { StringWriter writer(size, data); attr.read(mem_type, *writer); writer.finish(); H5::DataSet::vlenReclaim(*writer, mem_type, attr.getSpace()); //recycle space? }