estatus_t e5_read_attr_list_str( hid_t e5_group_id, e5_attr_str* e5_attr_list) { int i; estatus_t status = E5_SUCCESS; hid_t e5_attribute_id; hid_t e5_dataspace_id = H5Screate(H5S_SCALAR); hid_t e5_string_type = H5Tcopy(H5T_C_S1); H5Tset_size(e5_string_type, E5_MAX_ATTR_STRING_LENGTH); for(i = 0; e5_attr_list && e5_attr_list[i].name != 0; i++) { e5_attr_str *attr = &e5_attr_list[i]; if(attr->name == 0 || strlen(attr->name) < 1) continue; if(H5Aexists(e5_group_id, attr->name) <= 0) { status = E5_INVALID_ATTRIBUTE; e5_error(e5_group_id, status, "Specified attribute '%s' does not exist\n", attr->name); continue; } e5_attribute_id = H5Aopen(e5_group_id, attr->name, H5P_DEFAULT); H5Aread(e5_attribute_id, e5_string_type, &(attr->value)); H5Aclose(e5_attribute_id); e5_info(e5_group_id, "Read attribute [type='str', name='%s', value='%s']\n", attr->name, attr->value); } H5Sclose(e5_dataspace_id); return status; }
int e5_write_attr_list_double( hid_t e5_group_id, e5_attr_double* e5_attr_list) { int i; hid_t e5_attribute_id; hid_t e5_dataspace_id = H5Screate(H5S_SCALAR); for(i = 0; e5_attr_list && e5_attr_list[i].name != 0; i++) { e5_attr_double *attr = &e5_attr_list[i]; if(attr->name == 0 || strlen(attr->name) < 1) continue; if(H5Aexists(e5_group_id, attr->name) <= 0) e5_attribute_id = H5Acreate(e5_group_id, attr->name, H5T_IEEE_F64LE, e5_dataspace_id, H5P_DEFAULT); else e5_attribute_id = H5Aopen(e5_group_id, attr->name, H5P_DEFAULT); e5_info(e5_group_id, "Adding attribute [type='double', name='%s', value='%f']\n", attr->name, attr->value); H5Awrite(e5_attribute_id, H5T_NATIVE_DOUBLE, &(attr->value)); H5Aclose(e5_attribute_id); } H5Sclose(e5_dataspace_id); return E5_SUCCESS; }
estatus_t e5_write_attr_list( hid_t e5_group_id, e5_attr* e5_attr_list) { int i; eid_t status = E5_SUCCESS; hid_t e5_attribute_id; hid_t e5_dataspace_id = H5Screate(H5S_SCALAR); for(i = 0; e5_attr_list && e5_attr_list[i].name != 0; i++) { e5_attr *attr = &e5_attr_list[i]; if(attr->name == 0 || strlen(attr->name) < 1) continue; if(e5_is_valid_type(e5_attr_list[i].type) != E5_TRUE) { status = E5_INVALID_TYPE; e5_error(e5_group_id, status, "Invalid type requested for attribute [type='%d', name='%s', value='%p']\n", attr->type, attr->name, attr->value); continue; } hid_t hdf_type = e5_convert_type_to_hdf(e5_attr_list[i].type); hid_t hdf_format = e5_convert_format_to_hdf(e5_attr_list[i].type); if(H5Aexists(e5_group_id, attr->name) <= 0) e5_attribute_id = H5Acreate(e5_group_id, attr->name, hdf_type, e5_dataspace_id, H5P_DEFAULT); e5_info(e5_group_id, "Adding attribute [type='integer', name='%s', value='%d']\n", attr->name, attr->value); e5_attribute_id = H5Aopen(e5_group_id, attr->name, H5P_DEFAULT); H5Awrite(e5_attribute_id, hdf_format, &(attr->value)); H5Aclose(e5_attribute_id); } H5Sclose(e5_dataspace_id); return E5_SUCCESS; }
void write_internal(hdf5dataset& hdataset, const std::string& name, hid_t type_id, const void* buffer, bool overwrite) { bool exists = H5Aexists(hdataset.handle(), name.c_str()); hid_t attribute_id = -1; if(exists && overwrite) { attribute_id = H5Aopen(hdataset.handle(), name.c_str(), H5P_DEFAULT); //H5Ldelete(_hfile.handle(), name.c_str(), H5P_DEFAULT); } else if(exists && !overwrite) throw std::runtime_error("Attribute already exists: (" + name + ")"); else { hsize_t dims = 1; hid_t space_id = H5Screate_simple(1, &dims, NULL); attribute_id = H5Acreate(hdataset.handle(), name.c_str(), type_id, space_id, H5P_DEFAULT, H5P_DEFAULT); H5Sclose(space_id); } if(attribute_id < 0) throw std::runtime_error("Error creating or opening attribute () in file (" + name + ")"); H5Awrite(attribute_id, type_id, buffer); H5Tclose(type_id); H5Aclose(attribute_id); }
estatus_t e5_read_attr_list_float( hid_t e5_group_id, e5_attr_float* e5_attr_list) { int i; hid_t e5_attribute_id; estatus_t status = E5_SUCCESS; for(i = 0; e5_attr_list && e5_attr_list[i].name != 0; i++) { e5_attr_float *attr = &e5_attr_list[i]; if(attr->name == 0 || strlen(attr->name) < 1) continue; if(H5Aexists(e5_group_id, attr->name) <= 0) { status = E5_INVALID_ATTRIBUTE; e5_error(e5_group_id, status, "Specified attribute '%s' does not exist\n", attr->name); continue; } e5_attribute_id = H5Aopen(e5_group_id, attr->name, H5P_DEFAULT); H5Aread(e5_attribute_id, H5T_NATIVE_FLOAT, &(attr->value)); H5Aclose(e5_attribute_id); e5_info(e5_group_id, "Read attribute [type='float', name='%s', value='%f']\n", attr->name, attr->value); } return status; }
estatus_t e5_write_attr_list_str( hid_t e5_group_id, e5_attr_str* e5_attr_list) { int i; hid_t e5_attribute_id; hid_t e5_dataspace_id = H5Screate(H5S_SCALAR); hid_t e5_string_type = H5Tcopy(H5T_C_S1); H5Tset_size(e5_string_type, E5_MAX_ATTR_STRING_LENGTH); for(i = 0; e5_attr_list && e5_attr_list[i].name != 0; i++) { e5_attr_str *attr = &e5_attr_list[i]; if(attr->name == 0 || strlen(attr->name) < 1) continue; if(attr->value == 0 || strlen(attr->value) < 1) continue; if(H5Aexists(e5_group_id, attr->name) <= 0) e5_attribute_id = H5Acreate(e5_group_id, attr->name, e5_string_type, e5_dataspace_id, H5P_DEFAULT); else e5_attribute_id = H5Aopen(e5_group_id, attr->name, H5P_DEFAULT); e5_info(e5_group_id, "Adding attribute [type='string', name='%s', value='%s']\n", attr->name, attr->value); H5Awrite(e5_attribute_id, e5_string_type, &(attr->value)); H5Aclose(e5_attribute_id); } H5Sclose(e5_dataspace_id); return E5_SUCCESS; }
char AH5_write_str_root_attr(hid_t loc_id, const char *attr_name, const char *wdata) { char success = AH5_FALSE; hid_t aid = H5Screate(H5S_SCALAR); hid_t atype = H5Tcopy(H5T_C_S1); htri_t attr_exists = H5Aexists(loc_id, attr_name); hid_t attr = -1; H5Tset_size(atype, strlen(wdata)); if (atype >= 0) { if (attr_exists == 0) { attr = H5Acreate(loc_id, attr_name, atype, aid, H5P_DEFAULT, H5P_DEFAULT); } else if (attr_exists > 0) { attr = H5Aopen(loc_id, attr_name, H5P_DEFAULT); } if (attr && H5Awrite(attr, atype, wdata) >= 0 && H5Aclose(attr) >= 0) success = AH5_TRUE; } success &= (H5Tclose(atype) >= 0); success &= (H5Sclose(aid) >= 0); return success; }
void DCAttribute::writeAttribute(const char* name, const hid_t type, hid_t parent, uint32_t ndims, const Dimensions dims, const void* src) throw (DCException) { hid_t attr = -1; if (H5Aexists(parent, name)) attr = H5Aopen(parent, name, H5P_DEFAULT); else { hid_t dsp; if( ndims == 1 && dims.getScalarSize() == 1 ) dsp = H5Screate(H5S_SCALAR); else dsp = H5Screate_simple( ndims, dims.getPointer(), dims.getPointer() ); attr = H5Acreate(parent, name, type, dsp, H5P_DEFAULT, H5P_DEFAULT); H5Sclose(dsp); } if (attr < 0) throw DCException(getExceptionString(name, "Attribute could not be opened or created")); if (H5Awrite(attr, type, src) < 0) { H5Aclose(attr); throw DCException(getExceptionString(name, "Attribute could not be written")); } H5Aclose(attr); }
int e5_is_valid_attr( eid_t e5_group_id, const char* attr_name) { if(H5Aexists(e5_group_id, attr_name) <= 0) return E5_FALSE; return E5_TRUE; }
/** * determine whether attribute exists in file/group/dataset */ inline bool exists_attribute(H5::H5Object const& object, std::string const& name) { htri_t tri = H5Aexists(object.getId(), name.c_str()); if (tri < 0) { throw error("failed to determine whether attribute \"" + name + "\" exists"); } return (tri > 0); }
std::string hdf5dataset::get_attribute_string(const std::string& name, std::string default_value) { if(H5Aexists(_dataset_id, name.c_str())) { hdf5attribute attr(*this, name); return attr.read_as_string(); } else return default_value; }
H5Attribute::H5Attribute(H5Object & _parent, const std::string & _name) : H5Object(_parent, _name) { if (H5Aexists(getParent().getH5Id(), name.c_str()) <= 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot open attribute: %s"), name.c_str()); } attr = H5Aopen(getParent().getH5Id(), name.c_str(), H5P_DEFAULT); if (attr < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot open attribute: %s"), name.c_str()); } }
//-------------------------------------------------------------------------- // Function: H5Object::attrExists ///\brief Checks whether the named attribute exists at this location. ///\param name - IN: Name of the attribute to be queried ///\exception H5::AttributeIException // Programmer Binh-Minh Ribler - 2013 //-------------------------------------------------------------------------- bool H5Object::attrExists(const char* name) const { // Call C routine H5Aexists to determine whether an attribute exists // at this location, which could be specified by a file, group, dataset, // or named datatype. herr_t ret_value = H5Aexists(getId(), name); if (ret_value > 0) return true; else if(ret_value == 0) return false; else // Raise exception when H5Aexists returns a negative value throw AttributeIException(inMemFunc("attrExists"), "H5Aexists failed"); }
//-***************************************************************************** ArImpl::ArImpl( const std::string &iFileName, AbcA::ReadArraySampleCachePtr iCache ) : m_fileName( iFileName ) , m_file( -1 ) , m_readArraySampleCache( iCache ) { // OPEN THE FILE! htri_t exi = H5Fis_hdf5( m_fileName.c_str() ); ABCA_ASSERT( exi == 1, "Nonexistent File: " << m_fileName ); m_file = H5Fopen( m_fileName.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT ); ABCA_ASSERT( m_file >= 0, "Could not open file: " << m_fileName ); // get the version using HDF5 native calls int version = -INT_MAX; if (H5Aexists(m_file, "abc_version")) { H5LTget_attribute_int(m_file, ".", "abc_version", &version); } ABCA_ASSERT(version == ALEMBIC_HDF5_FILE_VERSION, "Unsupported file version detected."); // if it isn't there, it's pre 1.0 int fileVersion = 9999; if (H5Aexists( m_file, "abc_release_version" )) { H5LTget_attribute_int( m_file, ".", "abc_release_version", &fileVersion); } m_archiveVersion = fileVersion; // Read the top object m_top = new TopOrImpl( *this, m_file ); ReadTimeSamples( m_file, m_timeSamples ); }
estatus_t e5_read_attr_list( hid_t e5_group_id, e5_attr* e5_attr_list) { int i; estatus_t status = E5_SUCCESS; hid_t h5_type; hid_t h5_native_type; hid_t h5_space; hid_t e5_attribute_id; for(i = 0; e5_attr_list && e5_attr_list[i].name != 0; i++) { e5_attr *attr = &e5_attr_list[i]; if(attr->name == 0 || strlen(attr->name) < 1) continue; e5_info(e5_group_id, "Reading attribute [name='%s']\n", attr->name); if(H5Aexists(e5_group_id, attr->name) <= 0) { status = E5_INVALID_ATTRIBUTE; e5_error(e5_group_id, status, "Invalid name requested for attribute [type='%d', name='%s', value='%p']\n", attr->type, attr->name, attr->value); continue; } e5_attribute_id = H5Aopen(e5_group_id, attr->name, H5P_DEFAULT); h5_type = H5Aget_type(e5_attribute_id); h5_space = H5Aget_space(e5_attribute_id); h5_native_type = e5_get_native_h5_type(h5_type); attr->type = e5_convert_hdf_type(h5_type); if(e5_is_valid_type(attr->type) != E5_TRUE) { status = E5_INVALID_TYPE; e5_error(e5_group_id, status, "Invalid type requested for attribute [type='%d', name='%s', value='%p']\n", attr->type, attr->name, attr->value); continue; } H5Aread(e5_attribute_id, h5_native_type, attr->value); H5Aclose(e5_attribute_id); e5_info(e5_group_id, "Read attribute [type='%s', name='%s', value='%p']\n", e5_typename(attr->type), attr->name, attr->value); } return status; }
//-***************************************************************************** bool AttrExists( H5Node& iParent, const std::string& iName ) { ABCA_ASSERT( iParent.isValidObject(), "Invalid parent object in ReadMetaData" ); HDF5Hierarchy* h5HPtr = iParent.getH5HPtr(); if ( h5HPtr ) { return h5HPtr->attrExists( iParent.getRef(), iName ); } else { return H5Aexists( iParent.getObject(), iName.c_str() ) > 0; } }
static int NC4_get_propattr(NC_HDF5_FILE_INFO_T* h5) { int ncstat = NC_NOERR; size_t size; H5T_class_t t_class; hid_t grp = -1; hid_t attid = -1; hid_t aspace = -1; hid_t atype = -1; hid_t ntype = -1; herr_t herr = 0; char* text = NULL; /* Get root group */ grp = h5->root_grp->hdf_grpid; /* get root group */ /* Try to extract the NCPROPS attribute */ if(H5Aexists(grp,NCPROPS) > 0) { /* Does exist */ attid = H5Aopen_name(grp, NCPROPS); herr = -1; aspace = H5Aget_space(attid); /* dimensions of attribute data */ atype = H5Aget_type(attid); /* Verify that atype and size */ t_class = H5Tget_class(atype); if(t_class != H5T_STRING) {ncstat = NC_EATTMETA; goto done;} size = H5Tget_size(atype); if(size == 0) goto done; text = (char*)malloc(size+1); if(text == NULL) {ncstat = NC_ENOMEM; goto done;} HCHECK((ntype = H5Tget_native_type(atype, H5T_DIR_ASCEND))); HCHECK((H5Aread(attid, ntype, text))); /* Make sure its null terminated */ text[size] = '\0'; /* Try to parse text */ ncstat = NC4_properties_parse(&h5->fileinfo->propattr,text); herr = 0; } done: if(attid >= 0) HCHECK((H5Aclose(attid))); if(aspace >= 0) HCHECK((H5Sclose(aspace))); if(ntype >= 0) HCHECK((H5Tclose(ntype))); if(atype >= 0) HCHECK((H5Tclose(atype))); if(text != NULL) free(text); return ncstat; }
int NC4_put_propattr(NC_HDF5_FILE_INFO_T* h5) { int ncstat = NC_NOERR; H5T_class_t t_class; size_t size; hid_t grp = -1; hid_t exists = -1; hid_t attid = -1; hid_t aspace = -1; hid_t atype = -1; herr_t herr = 0; char* text = NULL; /* Get root group */ grp = h5->root_grp->hdf_grpid; /* get root group */ /* See if the NCPROPS attribute exists */ if(H5Aexists(grp,NCPROPS) == 0) { /* Does not exist */ ncstat = NC4_buildpropinfo(&h5->fileinfo->propattr,&text); if(text == NULL || ncstat != NC_NOERR) { goto done; } herr = -1; /* Create a datatype to refer to. */ HCHECK((atype = H5Tcopy(H5T_C_S1))); HCHECK((H5Tset_cset(atype, H5T_CSET_ASCII))); HCHECK((H5Tset_size(atype, strlen(text)+1))); /*keep nul term */ HCHECK((aspace = H5Screate(H5S_SCALAR))); HCHECK((attid = H5Acreate(grp, NCPROPS, atype, aspace, H5P_DEFAULT))); HCHECK((H5Awrite(attid, atype, text))); herr = 0; } done: if(ncstat != NC_NOERR) { if(text != NULL) { free(text); text = NULL; } } if(attid >= 0) HCHECK((H5Aclose(attid))); if(aspace >= 0) HCHECK((H5Sclose(aspace))); if(atype >= 0) HCHECK((H5Tclose(atype))); return ncstat; }
//-***************************************************************************** bool ReadMetaData( H5Node & iParent, const std::string &iMetaDataName, AbcA::MetaData &oMetaData ) { ABCA_ASSERT( iParent.isValidObject(), "Invalid parent in ReadMetaData" ); HDF5Hierarchy* H5HPtr = iParent.getH5HPtr(); if ( H5HPtr ) { std::string str; H5HPtr->readMetaDataString( iParent.getRef(), iMetaDataName, str ); if ( str.length() > 0 ) { oMetaData.deserialize( str ); return true; } else { oMetaData = AbcA::MetaData(); return false; } } else { hid_t iParentObject = iParent.getObject(); if ( H5Aexists( iParentObject, iMetaDataName.c_str() ) > 0 ) { std::string str; ReadString( iParentObject, iMetaDataName, str ); oMetaData.deserialize( str ); return true; } else { oMetaData = AbcA::MetaData(); return false; } } }
//-***************************************************************************** HDF5HierarchyReader::HDF5HierarchyReader( hid_t iFile, HDF5Hierarchy& iH5H, const bool iCacheHierarchy ) : m_H5H( iH5H ) { int enabled( 0 ); if (iCacheHierarchy && H5Aexists( iFile, "abc_ref_hierarchy" )) { H5LTget_attribute_int( iFile, ".", "abc_ref_hierarchy", &enabled ); } m_H5H.clear(); m_H5H.setEnabled( enabled != 0 ); if( enabled ) { readHierarchy( iFile ); } }
/* * Class: hdf_hdf5lib_H5 * Method: H5Aexists * Signature: (JLjava/lang/String;)Z */ JNIEXPORT jboolean JNICALL Java_hdf_hdf5lib_H5_H5Aexists (JNIEnv *env, jclass clss, jlong obj_id, jstring attr_name) { htri_t bval = JNI_FALSE; const char *aName; PIN_JAVA_STRING(attr_name, aName); if (aName != NULL) { bval = H5Aexists((hid_t)obj_id, aName); UNPIN_JAVA_STRING(attr_name, aName); if (bval > 0) bval = JNI_TRUE; else if (bval < 0) h5libraryError(env); } return (jboolean)bval; } /* end Java_hdf_hdf5lib_H5_H5Aexists */
hid_t H5Attribute::create(const hid_t loc, const std::string & name, const hid_t type, const hid_t targettype, const hid_t srcspace, const hid_t targetspace, void * data) { herr_t err; if (H5Aexists(loc, name.c_str()) > 0) { throw H5Exception(__LINE__, __FILE__, _("Attribute %s already exists."), name.c_str()); } hid_t attr = H5Acreate(loc, name.c_str(), targettype, targetspace == -1 ? srcspace : targetspace, H5P_DEFAULT, H5P_DEFAULT); if (attr < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot create a new attribute.")); } err = H5Awrite(attr, type, data); if (err < 0) { H5Aclose(attr); throw H5Exception(__LINE__, __FILE__, _("Cannot write data in the attribute.")); } return attr; }
//-***************************************************************************** bool ReadKey( hid_t iParent, const std::string &iAttrName, AbcA::ArraySample::Key &oKey ) { ABCA_ASSERT( iParent >= 0, "Invalid parent in ReadKey" ); if ( H5Aexists( iParent, iAttrName.c_str() ) > 0 ) { size_t numRead; ReadSmallArray( iParent, iAttrName, H5T_STD_U8LE, H5T_NATIVE_UINT8, 16, numRead, ( void * )&oKey.digest ); ABCA_ASSERT( numRead == 16, "Didn't read enough key bits" ); return true; } else { return false; } }
void DCAttribute::writeAttribute(const char* name, const hid_t type, hid_t parent, const void* src) throw (DCException) { hid_t attr = -1; if (H5Aexists(parent, name)) attr = H5Aopen(parent, name, H5P_DEFAULT); else { hid_t dsp = H5Screate(H5S_SCALAR); attr = H5Acreate(parent, name, type, dsp, H5P_DEFAULT, H5P_DEFAULT); H5Sclose(dsp); } if (attr < 0) throw DCException(getExceptionString(name, "Attribute could not be opened or created")); if (H5Awrite(attr, type, src) < 0) { H5Aclose(attr); throw DCException(getExceptionString(name, "Attribute could not be written")); } H5Aclose(attr); }
estatus_t e5_write_attr_list_int( hid_t e5_group_id, e5_attr_int* e5_attr_list) { int i; hid_t e5_attribute_id; hid_t e5_dataspace_id = H5Screate(H5S_SCALAR); for(i = 0; e5_attr_list && e5_attr_list[i].name != 0; i++) { e5_attr_int *attr = &e5_attr_list[i]; if(attr->name == 0 || strlen(attr->name) < 1) continue; if(H5Aexists(e5_group_id, attr->name) <= 0) e5_attribute_id = H5Acreate(e5_group_id, attr->name, H5T_STD_I32LE, e5_dataspace_id, H5P_DEFAULT); e5_info(e5_group_id, "Adding attribute [type='integer', name='%s', value='%d']\n", attr->name, attr->value); e5_attribute_id = H5Aopen(e5_group_id, attr->name, H5P_DEFAULT); H5Awrite(e5_attribute_id, H5T_NATIVE_INT, &(attr->value)); H5Aclose(e5_attribute_id); } H5Sclose(e5_dataspace_id); return E5_SUCCESS; }
bool LocID::hasAttr(const std::string &name) const { HTri res = H5Aexists(hid, name.c_str()); return res.check("LocID.hasAttr() failed"); }
int NC4_write_ncproperties(NC_FILE_INFO_T* h5) { int retval = NC_NOERR; hid_t hdf5grpid = -1; hid_t attid = -1; hid_t aspace = -1; hid_t atype = -1; char* text = NULL; size_t len = 0; LOG((3, "%s", __func__)); /* If the file is read-only, return an error. */ if (h5->no_write) {retval = NC_EPERM; goto done;} hdf5grpid = ((NC_HDF5_GRP_INFO_T *)(h5->root_grp->format_grp_info))->hdf_grpid; if(H5Aexists(hdf5grpid,NCPROPS) > 0) /* Already exists, no overwrite */ goto done; /* Build the attribute string */ if((retval = NC4_buildpropinfo(&h5->provenance->propattr,&text))) goto done; /* Build the HDF5 string type */ if ((atype = H5Tcopy(H5T_C_S1)) < 0) {retval = NC_EHDFERR; goto done;} if (H5Tset_strpad(atype, H5T_STR_NULLTERM) < 0) {retval = NC_EHDFERR; goto done;} if(H5Tset_cset(atype, H5T_CSET_ASCII) < 0) {retval = NC_EHDFERR; goto done;} len = strlen(text); if(H5Tset_size(atype, len) < 0) {retval = NC_EFILEMETA; goto done;} /* Create NCPROPS attribute */ if((aspace = H5Screate(H5S_SCALAR)) < 0) {retval = NC_EFILEMETA; goto done;} if ((attid = H5Acreate(hdf5grpid, NCPROPS, atype, aspace, H5P_DEFAULT)) < 0) {retval = NC_EFILEMETA; goto done;} if (H5Awrite(attid, atype, text) < 0) {retval = NC_EFILEMETA; goto done;} /* Verify */ #if 0 { hid_t spacev, typev; hsize_t dsize, tsize; typev = H5Aget_type(attid); spacev = H5Aget_space(attid); dsize = H5Aget_storage_size(attid); tsize = H5Tget_size(typev); fprintf(stderr,"dsize=%lu tsize=%lu\n",(unsigned long)dsize,(unsigned long)tsize); } #endif done: if(text != NULL) free(text); /* Close out the HDF5 objects */ if(attid > 0 && H5Aclose(attid) < 0) retval = NC_EHDFERR; if(aspace > 0 && H5Sclose(aspace) < 0) retval = NC_EHDFERR; if(atype > 0 && H5Tclose(atype) < 0) retval = NC_EHDFERR; /* For certain errors, actually fail, else log that attribute was invalid and ignore */ switch (retval) { case NC_ENOMEM: case NC_EHDFERR: case NC_EPERM: case NC_EFILEMETA: case NC_NOERR: break; default: LOG((0,"Invalid _NCProperties attribute")); retval = NC_NOERR; break; } return retval; }
/* HDF5 Specific attribute read/write of _NCProperties */ int NC4_read_ncproperties(NC_FILE_INFO_T* h5) { int retval = NC_NOERR; hid_t hdf5grpid = -1; hid_t attid = -1; hid_t aspace = -1; hid_t atype = -1; hid_t ntype = -1; char* text = NULL; H5T_class_t t_class; hsize_t size; LOG((3, "%s", __func__)); hdf5grpid = ((NC_HDF5_GRP_INFO_T *)(h5->root_grp->format_grp_info))->hdf_grpid; if(H5Aexists(hdf5grpid,NCPROPS) <= 0) { /* Does not exist */ /* File did not contain a _NCProperties attribute */ retval=NC4_get_provenance(h5,NULL,&globalpropinfo); goto done; } /* NCPROPS Attribute exists, make sure it is legitimate */ attid = H5Aopen_name(hdf5grpid, NCPROPS); assert(attid > 0); aspace = H5Aget_space(attid); atype = H5Aget_type(attid); /* Verify atype and size */ t_class = H5Tget_class(atype); if(t_class != H5T_STRING) {retval = NC_EINVAL; goto done;} size = H5Tget_size(atype); if(size == 0) {retval = NC_EINVAL; goto done;} text = (char*)malloc(1+(size_t)size); if(text == NULL) {retval = NC_ENOMEM; goto done;} if((ntype = H5Tget_native_type(atype, H5T_DIR_DEFAULT)) < 0) {retval = NC_EHDFERR; goto done;} if((H5Aread(attid, ntype, text)) < 0) {retval = NC_EHDFERR; goto done;} /* Make sure its null terminated */ text[(size_t)size] = '\0'; /* Process the _NCProperties value */ if((retval = NC4_get_provenance(h5, text, &globalpropinfo))) goto done; done: if(text != NULL) free(text); /* Close out the HDF5 objects */ if(attid > 0 && H5Aclose(attid) < 0) retval = NC_EHDFERR; if(aspace > 0 && H5Sclose(aspace) < 0) retval = NC_EHDFERR; if(atype > 0 && H5Tclose(atype) < 0) retval = NC_EHDFERR; if(ntype > 0 && H5Tclose(ntype) < 0) retval = NC_EHDFERR; /* For certain errors, actually fail, else log that attribute was invalid and ignore */ if(retval != NC_NOERR) { if(retval != NC_ENOMEM && retval != NC_EHDFERR) { LOG((0,"Invalid _NCProperties attribute: ignored")); retval = NC_NOERR; } } return retval; }
inline bool exists_attribute(h5xxObject const& object, std::string const& name) { // return false also if existence can not be queried return H5Aexists(object.hid(), name.c_str()) > 0; }
std::string fast5_get_string_attribute(fast5_file& fh, const std::string& group_name, const std::string& attribute_name) { hid_t group, attribute, attribute_type, native_type; std::string out; // according to http://hdf-forum.184993.n3.nabble.com/check-if-dataset-exists-td194725.html // we should use H5Lexists to check for the existence of a group/dataset using an arbitrary path // HDF5 1.8 returns 0 on the root group, so we explicitly check for it int ret = group_name == "/" ? 1 : H5Lexists(fh.hdf5_file, group_name.c_str(), H5P_DEFAULT); if(ret <= 0) { return ""; } // Open the group containing the attribute we want group = H5Gopen(fh.hdf5_file, group_name.c_str(), H5P_DEFAULT); if(group < 0) { #ifdef DEBUG_FAST5_IO fprintf(stderr, "could not open group %s\n", group_name.c_str()); #endif goto close_group; } // Ensure attribute exists ret = H5Aexists(group, attribute_name.c_str()); if(ret <= 0) { goto close_group; } // Open the attribute attribute = H5Aopen(group, attribute_name.c_str(), H5P_DEFAULT); if(attribute < 0) { #ifdef DEBUG_FAST5_IO fprintf(stderr, "could not open attribute: %s\n", attribute_name.c_str()); #endif goto close_attr; } // Get data type and check it is a fixed-length string attribute_type = H5Aget_type(attribute); if(attribute_type < 0) { #ifdef DEBUG_FAST5_IO fprintf(stderr, "failed to get attribute type %s\n", attribute_name.c_str()); #endif goto close_type; } if(H5Tget_class(attribute_type) != H5T_STRING) { #ifdef DEBUG_FAST5_IO fprintf(stderr, "attribute %s is not a string\n", attribute_name.c_str()); #endif goto close_type; } native_type = H5Tget_native_type(attribute_type, H5T_DIR_ASCEND); if(native_type < 0) { #ifdef DEBUG_FAST5_IO fprintf(stderr, "failed to get native type for %s\n", attribute_name.c_str()); #endif goto close_native_type; } if(H5Tis_variable_str(attribute_type) > 0) { // variable length string char* buffer; ret = H5Aread(attribute, native_type, &buffer); if(ret < 0) { fprintf(stderr, "error reading attribute %s\n", attribute_name.c_str()); exit(EXIT_FAILURE); } out = buffer; free(buffer); buffer = NULL; } else { // fixed length string size_t storage_size; char* buffer; // Get the storage size and allocate storage_size = H5Aget_storage_size(attribute); buffer = (char*)calloc(storage_size + 1, sizeof(char)); // finally read the attribute ret = H5Aread(attribute, attribute_type, buffer); if(ret >= 0) { out = buffer; } // clean up free(buffer); } close_native_type: H5Tclose(native_type); close_type: H5Tclose(attribute_type); close_attr: H5Aclose(attribute); close_group: H5Gclose(group); return out; }