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_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; }
void save_string_attr(H5::Group &g, const char *name, const char *val) { H5::DataSpace string_space(H5S_SCALAR); H5::StrType strdatatype(H5::PredType::C_S1, strlen(val)+1); H5::Attribute attr = g.createAttribute(name, strdatatype, string_space); attr.write(strdatatype, val); }
inline typename boost::enable_if<boost::mpl::and_< is_vector<T> , boost::is_same<typename T::value_type, std::string> >, void>::type write_attribute(H5::H5Object const& object, std::string const& name, T const& value) { size_t size = value.size(); // size of longest string size_t str_len = 0; for (size_t i = 0; i < size; ++i) { str_len = std::max(str_len, value[i].size()); } // remove attribute if it exists and re-create with proper String datatype // and simple dataspace if (exists_attribute(object, name)) { object.removeAttr(name); } hsize_t dim[1] = { size }; H5::DataSpace ds(1, dim); H5::StrType tid(H5::PredType::C_S1, str_len); H5::Attribute attr = object.createAttribute(name, tid, ds); // copy strings to contiguous buffer std::vector<char> buffer(size * str_len); for (size_t i = 0; i < size; ++i) { value[i].copy(buffer.data() + i * str_len, str_len); } attr.write(tid, &*buffer.begin()); }
inline typename boost::enable_if<boost::mpl::and_<is_array<T>, boost::is_same<typename T::value_type, char const*> >, void>::type write_attribute(H5::H5Object const& object, std::string const& name, T const& value) { enum { size = T::static_size }; hsize_t dim[1] = { size }; H5::DataSpace ds(1, dim); size_t max_len = 0; for (size_t i = 0; i < size; ++i) { max_len = std::max(max_len, strlen(value[i])); } H5::StrType tid(H5::PredType::C_S1, max_len); // remove attribute if it exists try { H5XX_NO_AUTO_PRINT(H5::AttributeIException); object.removeAttr(name); } catch (H5::AttributeIException const&) {} H5::Attribute attr = object.createAttribute(name, tid, ds); std::vector<char> data(max_len * size); for (size_t i = 0; i < size; ++i) { strncpy(&*data.begin() + i * max_len, value[i], max_len); } attr.write(tid, &*data.begin()); }
inline typename boost::enable_if<boost::is_same<T, 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; } if (!has_scalar_space(attr)) { throw H5::AttributeIException("H5::attribute::as", "incompatible dataspace"); } H5::DataType tid = attr.getDataType(); std::string value; if (!tid.isVariableStr()) { // read fixed-size string, allocate space in advance and let the HDF5 // library take care about NULLTERM and NULLPAD strings value.resize(tid.getSize(), std::string::value_type()); attr.read(tid, &*value.begin()); } else { // read variable-length string, memory will be allocated by HDF5 C // library and must be freed by us char *c_str; if (H5Aread(attr.getId(), tid.getId(), &c_str) < 0) { throw H5::AttributeIException("Attribute::read", "H5Aread failed"); } value = c_str; // copy '\0'-terminated string free(c_str); } return value; }
/** * @param attribute_id * @return String representing the name of the attribute specified by attribute_id */ std::string HDF5FileReader::getVariableAttributeName(long attribute_id) { H5::DataSet dataset = this->variableGroup->openDataSet(this->variableGroup->getObjnameByIdx(0)); H5::Attribute attribute = dataset.openAttribute(attribute_id); std::string buffer = attribute.getName(); cout << "Attribute Name: '" << buffer << "'" << endl; return buffer; }
void Datafile::add_attribute(const char* name, int value) { try { H5::Attribute attr = root_group.createAttribute(name, int_type, scalar_space); attr.write(int_type, &value); } catch (H5::Exception& e) { e.printError(); throw; } }
void Datafile::add_attribute(const char* name, unsigned long int value) { try { H5::Attribute attr = root_group.createAttribute(name, H5::PredType::NATIVE_ULONG, scalar_space); attr.write(H5::PredType::NATIVE_ULONG, &value); } catch (H5::Exception& e) { e.printError(); throw; } }
/** *@param attribute_id *@return */ std::string HDF5FileReader::getGlobalAttributeName(long attribute_id) { // CDFgetAttrName((void *) current_file_id, attribute_id, buffer); H5::Attribute attribute = this->rootGroup->openAttribute(attribute_id); std::string buffer_string = attribute.getName(); //cout << "Attribute Name: '" << buffer_string << "'" << endl; return buffer_string; }
void Bundle2::storeParameters(H5::H5File& file) const { H5::Group root = file.openGroup("/"); H5::DataSpace scalar; H5::Attribute attr = root.createAttribute("version", H5::PredType::STD_U32LE, scalar); attr.write(H5::PredType::NATIVE_UINT, &version_); attr.close(); unsigned char r2 = parameters_.reduce2?1:0; attr = root.createAttribute("reduce2", H5::PredType::STD_U8LE, scalar); attr.write(H5::PredType::NATIVE_UCHAR, &r2); attr.close(); attr = root.createAttribute("xROI", H5::PredType::STD_U32LE, scalar); attr.write(H5::PredType::NATIVE_UINT, ¶meters_.xROI); attr.close(); attr = root.createAttribute("yROI", H5::PredType::STD_U32LE, scalar); attr.write(H5::PredType::NATIVE_UINT, ¶meters_.yROI); attr.close(); scalar.close(); root.close(); }
void ReadAttrib(const std::string& name, T& value, const H5::DataType& dType){ //attributes are clunky in HDF5++ implementation - this is a workaround //template is required to pass the proper value type //access the built-in root group and create a new attribute for it. H5::Group rootGroup = file_->openGroup("/"); H5::Attribute attrib = rootGroup.openAttribute(name); //write the value to the attribute and close attrib.read(dType,reinterpret_cast<void*>(&value)); attrib.close(); }
std::string read_string_attr(H5::H5File &f, H5::Group &group, const char *name) { std::string strreadbuf (""); H5::Attribute attr = group.openAttribute(name); H5::StrType strdatatype(H5::PredType::C_S1, attr.getStorageSize()); attr.read(strdatatype, strreadbuf); return strreadbuf; }
void WriteTAttrib(const std::string& name, const T& value, const H5::DataType& dType, const H5::DataSpace& dSpace){ //attributes are clunky in HDF5++ implementation - this is a workaround //template is required to pass the proper value type //create a new data set attribute H5::Attribute attrib = dSet_.createAttribute(name,dType,dSpace); //write the value to the attribute and close attrib.write(dType,&value); attrib.close(); }
void Attribute::write(H5::H5File f, const cpath & dataset_root) { //FIXME we should have a path? cpath fullpath = dataset_root / name; cpath grouppath = fullpath.parent_path(); if (_link.size()) { if (!h5_obj_exists(f, grouppath)) h5_create_path_groups(f, grouppath.c_str()); H5::Group g = f.openGroup(grouppath.generic_string().c_str()); if (h5_obj_exists(f, fullpath)) g.unlink(name.filename().generic_string().c_str()); g.link(H5G_LINK_SOFT, (dataset_root/_link).generic_string().c_str(), name.filename().generic_string().c_str()); } else if (_m.total() == 0) { //FIXME remove this (legacy) case hsize_t *dim = new hsize_t[size.size()+1]; for(uint i=0;i<size.size();i++) dim[i] = size[i]; H5::DataSpace space(size.size(), dim); H5::Attribute attr; H5::Group g; delete[] dim; if (!h5_obj_exists(f, grouppath)) h5_create_path_groups(f, grouppath); g = f.openGroup(grouppath.generic_string().c_str()); uint min, max; H5Pget_attr_phase_change(H5Gget_create_plist(g.getId()), &max, &min); if (min || max) printf("WARNING: could not set dense storage on group, may not be able to write large attributes\n"); //FIXME relative to what? if (H5Aexists(g.getId(), name.filename().generic_string().c_str())) g.removeAttr(name.filename().generic_string().c_str()); attr = g.createAttribute(name.filename().generic_string().c_str(), toH5DataType(type), space); attr.write(toH5NativeDataType(type), data); } else Mat_H5AttrWrite(_m, f, fullpath); }
inline typename boost::enable_if<boost::is_same<T, char const*>, void>::type write_attribute(H5::H5Object const& object, std::string const& name, T value) { H5::StrType tid(H5::PredType::C_S1, strlen(value)); // remove attribute if it exists try { H5XX_NO_AUTO_PRINT(H5::AttributeIException); object.removeAttr(name); } catch (H5::AttributeIException const&) {} H5::Attribute attr = object.createAttribute(name, tid, H5S_SCALAR); attr.write(tid, value); }
/** * @return */ std::vector<std::string> HDF5FileReader::getVariableAttributeNames() { std::vector<std::string> attributeNames; int numAttributes = this->getNumberOfVariableAttributes(); H5::DataSet dataset = this->variableGroup->openDataSet(this->variableGroup->getObjnameByIdx(0)); for (int i = 0; i < numAttributes; i++) { std::string value = ""; H5::Attribute attribute = dataset.openAttribute(i); attributeNames.push_back(attribute.getName()); } return attributeNames; }
void WriteAttrib(const std::string& name, const T& value, const H5::DataType& dType, const H5::DataSpace& dSpace){ //attributes are clunky in HDF5++ implementation - this is a workaround //template is required to pass the proper value type //access the built-in root group and create a new attribute for it. H5::Group rootGroup = file_->openGroup("/"); H5::Attribute attrib = rootGroup.createAttribute(name,dType,dSpace); //write the value to the attribute and close attrib.write(dType,&value); attrib.close(); }
void Datafile::add_attribute(const char* name, std::string const& value) { const size_t len = value.length(); try { H5::AtomType string_type = H5::AtomType(H5::PredType::C_S1); // setSize does not like zero sizes if (len != 0) string_type.setSize(len); H5::Attribute attr = root_group.createAttribute(name, string_type, scalar_space); attr.write(string_type, value.c_str()); } catch (H5::Exception& e) { e.printError(); throw; } }
void DataSetIO::SaveDataSet(const DataSet& dataSet_, const std::string& filename_){ // Get the relevant params hsize_t nEntries = dataSet_.GetNEntries(); hsize_t nObs = dataSet_.GetNObservables(); hsize_t nData = nObs * nEntries; // create colon separated string from list of observables std::vector<std::string> observableNames = dataSet_.GetObservableNames(); if (observableNames.size() != nObs) throw HdfIOError("SaveDataSet::Require one name and one name only for each observable"); // Set up files H5::H5File file(filename_, H5F_ACC_TRUNC); // Flatten data into 1D array // HDF5 likes c arrays. Here use a vector and pass pointer to first element // memory guaranteed to be contiguous std::vector<double> flattenedData; std::vector<double> eventData; flattenedData.reserve(nData); for(size_t i = 0; i < nEntries; i++){ eventData = dataSet_.GetEntry(i).GetData(); flattenedData.insert(flattenedData.end(), eventData.begin(), eventData.end()); } // Set up the data set // 1D, ndata long, called "observations". Saved as native doubles on this computer H5::DataSpace dataSpace(1, &nData); H5::DataSet theData(file.createDataSet("observations", H5::PredType::NATIVE_DOUBLE, dataSpace)); // Set up the attributes - the number of obs per event and the names of the observables // 64 chars max in str to save space H5::StrType strType(H5::PredType::C_S1, 64); H5::DataSpace attSpace(H5S_SCALAR); H5::Attribute obsListAtt = theData.createAttribute("observed_quantities", strType, attSpace); obsListAtt.write(strType, FlattenStringVector(observableNames, fDelimiter)); H5::Attribute countAtt = theData.createAttribute("n_observables", H5::PredType::NATIVE_INT, attSpace); countAtt.write(H5::PredType::NATIVE_INT, &nObs); // Write the data theData.write(&flattenedData.at(0), H5::PredType::NATIVE_DOUBLE); }
void WriteTStrAttrib(const std::string& name, const std::string& value){ std::string str = value; str.resize( STRING_ATTRIB_SIZE ); //define the data type as a string with the value's length H5::StrType strType(0, STRING_ATTRIB_SIZE ); //open the data set and create a new attribute of type string H5::Attribute attrib = dSet_.createAttribute(name, strType, H5::DataSpace(H5S_SCALAR)); //write value to the attribute and close attrib.write(strType, str ); attrib.close(); }
void ReadTAttrib(const int& tableNum, const std::string& name, T& value, const H5::DataType& dType){ //attributes are clunky in HDF5++ implementation - this is a workaround //template is required to pass the proper value type //std::cout << "attribute read name = " << name << std::endl; std::string tNum = Num2Table(tableNum); //open data set and read attribute "name" dSet_ = file_->openDataSet(tNum); H5::Attribute attrib = dSet_.openAttribute(name); //write the value to the attribute and close attrib.read(dType,reinterpret_cast<void*>(&value)); attrib.close(); }
void Create(H5::H5Location &object, const std::string & name, const std::vector<std::string> &vect) { hsize_t length = vect.size(); H5::StrType strType(0,H5T_VARIABLE); H5::ArrayType arrayDataType(strType, 1, &length); attribute = object.createAttribute(name.c_str(), strType, H5::DataSpace(1, &length)); attribute.write(strType, &((vect)[0])); }
void Bundle2::closeSaveStream() { H5::DataSpace scalar; // Saving remaining POI information H5::Group poiGroup = streamFile_->openGroup("/POI"); H5::Attribute attr = poiGroup.createAttribute("count", H5::PredType::STD_U64LE, scalar); hsize_t count = poiFirstFrame_; attr.write(H5::PredType::NATIVE_HSIZE, &count); attr.close(); poiGroup.close(); scalar.close(); // Closing HDF5 file streamFile_->close(); delete streamFile_; streamFile_ = NULL; }
size_t H5Signal::clock_size(void) /*-----------------------------*/ { try { H5::Attribute attr = dataset.openAttribute("clock") ; hobj_ref_t ref ; attr.read(H5::PredType::STD_REF_OBJ, &ref) ; attr.close() ; H5::DataSet clk(H5Rdereference(H5Iget_file_id(dataset.getId()), H5R_OBJECT, &ref)) ; H5::DataSpace cspace = clk.getSpace() ; int cdims = cspace.getSimpleExtentNdims() ; hsize_t cshape[cdims] ; cspace.getSimpleExtentDims(cshape) ; return cshape[0] ; } catch (H5::AttributeIException e) { } return -1 ; }
inline typename boost::enable_if<boost::is_fundamental<T>, 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; } if (!has_scalar_space(attr)) { throw H5::AttributeIException("H5::attribute::as", "incompatible dataspace"); } T value; attr.read(ctype<T>::hid(), &value); return value; }
inline typename boost::enable_if<boost::is_fundamental<T>, void>::type write_attribute(H5::H5Object const& object, std::string const& name, T const& value) { H5::Attribute attr; try { H5XX_NO_AUTO_PRINT(H5::AttributeIException); attr = object.openAttribute(name); if (!has_type<T>(attr) || !has_scalar_space(attr)) { // recreate attribute with proper type object.removeAttr(name); throw H5::AttributeIException(); } } catch (H5::AttributeIException const&) { attr = object.createAttribute(name, ctype<T>::hid(), H5S_SCALAR); } attr.write(ctype<T>::hid(), &value); }
const std::string ReadStrAttrib(const std::string& name){ //attributes are clunky in HDF5++ implementation - this is a workaround //template is required to pass the proper value type std::string value; value.resize( STRING_ATTRIB_SIZE ); H5::StrType strType(0, STRING_ATTRIB_SIZE ); //access the built-in root group and create a new attribute for it. H5::Group rootGroup = file_->openGroup("/"); H5::Attribute attrib = rootGroup.openAttribute(name); //write the value to the attribute and close attrib.read(strType,value); attrib.close(); 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; }
void WriteStrAttrib(const std::string& name, const std::string& value){ //HDF5 has a mangled way of creating string attributes - //this is a workaround //create a string array of length 1 containing the value to be written //std::string strBuf[1] = {value}; std::string str = value; str.resize( STRING_ATTRIB_SIZE ); //define the data type as a string with the value's length H5::StrType strType(0, STRING_ATTRIB_SIZE ); //open the root group and create a new attribute of type string H5::Group rootGroup = file_->openGroup("/"); H5::Attribute attrib = rootGroup.createAttribute(name, strType,H5::DataSpace()); //write value to the attribute and close attrib.write(strType, str); attrib.close(); }