// Read string attribute <attr_name> given by address <path> char AH5_read_str_attr(hid_t loc_id, const char *path, char *attr_name, char **rdata) { hid_t attr_id, filetype, memtype; size_t sdim; char success = AH5_FALSE; if (AH5_path_valid(loc_id, path) || strcmp(path, ".") == 0) if (H5Aexists_by_name(loc_id, path, attr_name, H5P_DEFAULT) > 0) { attr_id = H5Aopen_by_name(loc_id, path, attr_name, H5P_DEFAULT, H5P_DEFAULT); filetype = H5Aget_type(attr_id); sdim = H5Tget_size(filetype); sdim++; // make a space for null terminator *rdata = (char *) malloc(sdim * sizeof(char)); memtype = H5Tcopy(H5T_C_S1); H5Tset_size(memtype, sdim); if (H5Aread(attr_id, memtype, *rdata) >= 0) success = AH5_TRUE; else free(*rdata); H5Tclose(memtype); H5Tclose(filetype); H5Aclose(attr_id); } if (!success) *rdata = NULL; return success; }
static int readIntAttribute_v1(int _iDatasetId, const char *_pstName) { hid_t iAttributeId; herr_t status; hsize_t n = 0; int iVal = -1; if (H5Aiterate(_iDatasetId, H5_INDEX_NAME, H5_ITER_NATIVE, &n, find_attr_by_name_v1, (void *)_pstName) > 0) { iAttributeId = H5Aopen_by_name(_iDatasetId, ".", _pstName, H5P_DEFAULT, H5P_DEFAULT); if (iAttributeId < 0) { return -1; } status = H5Aread(iAttributeId, H5T_NATIVE_INT, &iVal); if (status < 0) { return -1; } status = H5Aclose(iAttributeId); if (status < 0) { return -1; } } return iVal; }
// read a complex float attribute float_complex read_complex_attribute(hid_t loc_id, const char* path, const char* name) { float_complex value; float *buf; hid_t attr_id, real_type_id; ; hsize_t *dims; herr_t status; status = H5Aexists_by_name(loc_id, path, name, H5P_DEFAULT); if (status < 0) { printf("attribut %s does not exist \n", name); } real_type_id = create_real_type_id(); attr_id = H5Aopen_by_name(loc_id, path, name, H5P_DEFAULT, H5P_DEFAULT); status = H5Aread(attr_id, real_type_id, buf); if (status < 0) { printf("Can't read attribute : %s\n", name); } value.re = buf[0]; value.im=buf[1]; return value; }
attribute get_attribute(handle const& loc, const char* obj_name, const char* attr_name) { hid_t id = H5Aopen_by_name(loc.getId(), obj_name, attr_name, H5P_DEFAULT, H5P_DEFAULT); if (id < 0) throw std::runtime_error("H5Aopen_by_name failed"); return attribute(std::move(id)); }
med_err _MEDattributeStringRdByName(med_idt pid, const char * const path, const char * const attname, const med_size attsize, char * const val) { med_err _ret=-1; med_idt _attid=0; int type_hdf=0; if ( (type_hdf = H5Tcopy(H5T_C_S1)) < 0) { MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_HDFTYPE, MED_ERR_NAME_MSG ); SSCRUTE("H5T_C_S1"); goto ERROR; } if ( H5Tset_size(type_hdf,attsize+1) < 0) { MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_HDFTYPE, MED_ERR_NAME_MSG ); SSCRUTE("H5T_C_S1"); goto ERROR; } if ( (_attid=H5Aopen_by_name( pid, path, attname, H5P_DEFAULT, H5P_DEFAULT )) < 0 ) { MED_ERR_(_ret,MED_ERR_OPEN,MED_ERR_ATTRIBUTE, MED_ERR_NAME_MSG ); SSCRUTE(attname); SSCRUTE(path);goto ERROR; } if ( H5Aread(_attid,type_hdf,val) < 0 ) { MED_ERR_(_ret,MED_ERR_READ,MED_ERR_ATTRIBUTE, MED_ERR_NAME_MSG ); SSCRUTE(attname); SSCRUTE(path);goto ERROR; } _ret=0; ERROR: if (type_hdf >0) if ( H5Tclose(type_hdf) < 0) { MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_HDFTYPE, MED_ERR_ID_MSG ); ISCRUTE_int(type_hdf); } if (_attid >0) if ( H5Aclose(_attid) < 0) { MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_ATTRIBUTE, MED_ERR_ID_MSG ); ISCRUTE_id(_attid); } return _ret; }
/* * Class: hdf_hdf5lib_H5 * Method: _H5Aopen_by_name * Signature: (JLjava/lang/String;Ljava/lang/String;JJ)J */ JNIEXPORT jlong JNICALL Java_hdf_hdf5lib_H5__1H5Aopen_1by_1name (JNIEnv *env, jclass clss, jlong loc_id, jstring obj_name, jstring attr_name, jlong aapl_id, jlong lapl_id) { hid_t status = -1; const char *aName; const char *oName; PIN_JAVA_STRING_TWO(obj_name, oName, attr_name, aName); if (oName != NULL && aName != NULL) { status = H5Aopen_by_name((hid_t)loc_id, oName, aName, (hid_t)aapl_id, (hid_t)lapl_id); UNPIN_JAVA_STRING_TWO(obj_name, oName, attr_name, aName); if (status < 0) h5libraryError(env); } return (jlong)status; } /* end Java_hdf_hdf5lib_H5__1H5Aopen_1by_1name */
// Read complex attribute <attr_name> given by address <path> char AH5_read_cpx_attr(hid_t loc_id, const char* path, char* attr_name, AH5_complex_t *rdata) { hid_t attr_id, type_id; float buf[2]; char success = AH5_FALSE; if (AH5_path_valid(loc_id, path)) if (H5Aexists_by_name(loc_id, path, attr_name, H5P_DEFAULT) > 0) { attr_id = H5Aopen_by_name(loc_id, path, attr_name, H5P_DEFAULT, H5P_DEFAULT); type_id = AH5_H5Tcreate_cpx_memtype(); if (H5Aread(attr_id, type_id, buf) >= 0) { *rdata = AH5_set_complex(buf[0], buf[1]); success = AH5_TRUE; } H5Tclose(type_id); H5Aclose(attr_id); } if (!success) *rdata = AH5_set_complex(0, 0); return success; }
/* ** WARNING : this function returns an allocated value that must be freed. */ static char* readAttribute_v1(int _iDatasetId, const char *_pstName) { hid_t iAttributeId; hid_t iFileType, memtype, iSpace; herr_t status; hsize_t dims[1]; hsize_t n = 0; size_t iDim; char *pstValue = NULL; if (H5Aiterate(_iDatasetId, H5_INDEX_NAME, H5_ITER_NATIVE, &n, find_attr_by_name_v1, (void *)_pstName) > 0) { iAttributeId = H5Aopen_by_name(_iDatasetId, ".", _pstName, H5P_DEFAULT, H5P_DEFAULT); if (iAttributeId < 0) { return NULL; } /* * Get the datatype and its size. */ iFileType = H5Aget_type(iAttributeId); iDim = H5Tget_size(iFileType); iDim++; /* Make room for null terminator */ /* * Get dataspace and allocate memory for read buffer. This is a * two dimensional attribute so the dynamic allocation must be done * in steps. */ iSpace = H5Aget_space(iAttributeId); if (iSpace < 0) { return NULL; } status = H5Sget_simple_extent_dims(iSpace, dims, NULL); if (status < 0) { return NULL; } /* * Allocate space for string data. */ pstValue = (char *)MALLOC((size_t) ((dims[0] * iDim + 1) * sizeof(char))); /* * Create the memory datatype. */ memtype = H5Tcopy(H5T_C_S1); status = H5Tset_size(memtype, iDim); if (status < 0) { FREE(pstValue); return NULL; } /* * Read the data. */ status = H5Aread(iAttributeId, memtype, pstValue); if (status < 0) { FREE(pstValue); return NULL; } status = H5Tclose(memtype); if (status < 0) { FREE(pstValue); return NULL; } status = H5Sclose(iSpace); if (status < 0) { FREE(pstValue); return NULL; } status = H5Tclose(iFileType); if (status < 0) { FREE(pstValue); return NULL; } status = H5Aclose(iAttributeId); if (status < 0) { FREE(pstValue); return NULL; } } return pstValue; }
/*------------------------------------------------------------------------- * Function: read_records * * Purpose: For a given dataset, checks to make sure that the stated * and actual sizes are the same. If they are not, then * we have an inconsistent dataset due to a SWMR error. * * Parameters: const char *filename * The SWMR test file's name. * * unsigned verbose * Whether verbose console output is desired. * * unsigned long nrecords * The total number of records to read. * * unsigned poll_time * The amount of time to sleep (s). * * unsigned reopen_count * * * Return: Success: 0 * Failure: -1 * *------------------------------------------------------------------------- */ static int read_records(const char *filename, unsigned verbose, unsigned long nrecords, unsigned poll_time, unsigned reopen_count) { hid_t fid; /* File ID */ hid_t aid; /* Attribute ID */ time_t start_time; /* Starting time */ hid_t mem_sid; /* Memory dataspace ID */ symbol_t record; /* The record to add to the dataset */ unsigned seed; /* Seed for random number generator */ unsigned iter_to_reopen = reopen_count; /* # of iterations until reopen */ unsigned long u; /* Local index variable */ hid_t fapl; HDassert(filename); HDassert(poll_time != 0); /* Create file access property list */ if((fapl = h5_fileaccess()) < 0) return -1; H5Pset_fclose_degree(fapl, H5F_CLOSE_SEMI); /* Emit informational message */ if(verbose) HDfprintf(stderr, "Opening file: %s\n", filename); /* Open the file */ if((fid = H5Fopen(filename, H5F_ACC_RDONLY | H5F_ACC_SWMR_READ, fapl)) < 0) return -1; /* Seed the random number generator with the attribute in the file */ if((aid = H5Aopen(fid, "seed", H5P_DEFAULT)) < 0) return -1; if(H5Aread(aid, H5T_NATIVE_UINT, &seed) < 0) return -1; if(H5Aclose(aid) < 0) return -1; HDsrandom(seed); /* Reset the record */ /* (record's 'info' field might need to change for each record written, also) */ HDmemset(&record, 0, sizeof(record)); /* Create a dataspace for the record to read */ if((mem_sid = H5Screate(H5S_SCALAR)) < 0) return -1; /* Emit informational message */ if(verbose) HDfprintf(stderr, "Reading records\n"); /* Get the starting time */ start_time = HDtime(NULL); /* Read records */ for(u = 0; u < nrecords; u++) { symbol_info_t *symbol = NULL; /* Symbol (dataset) */ htri_t attr_exists; /* Whether the sequence number attribute exists */ unsigned long file_u; /* Attribute sequence number (writer's "u") */ /* Get a random dataset, according to the symbol distribution */ symbol = choose_dataset(); /* Fill in "nrecords" field. Note that this depends on the writer * using the same algorithm and "nrecords" */ symbol->nrecords = nrecords / 5; /* Wait until we can read the dataset */ do { /* Check if sequence attribute exists */ if((attr_exists = H5Aexists_by_name(fid, symbol->name, "seq", H5P_DEFAULT)) < 0) return -1; if(attr_exists) { /* Read sequence number attribute */ if((aid = H5Aopen_by_name(fid, symbol->name, "seq", H5P_DEFAULT, H5P_DEFAULT)) < 0) return -1; if(H5Aread(aid, H5T_NATIVE_ULONG, &file_u) < 0) return -1; if(H5Aclose(aid) < 0) return -1; /* Check if sequence number is at least u - if so, this should * guarantee that this record has been written */ if(file_u >= u) break; } /* end if */ /* Check for timeout */ if(HDtime(NULL) >= (time_t)(start_time + (time_t)TIMEOUT)) { HDfprintf(stderr, "Reader timed out\n"); return -1; } /* end if */ /* Pause */ HDsleep(poll_time); /* Retrieve and print the collection of metadata read retries */ if(print_metadata_retries_info(fid) < 0) HDfprintf(stderr, "Warning: could not obtain metadata retries info\n"); /* Reopen the file */ if(H5Fclose(fid) < 0) return -1; if((fid = H5Fopen(filename, H5F_ACC_RDONLY | H5F_ACC_SWMR_READ, fapl)) < 0) return -1; iter_to_reopen = reopen_count; } while(1); /* Emit informational message */ if(verbose) HDfprintf(stderr, "Checking dataset %lu\n", u); /* Check dataset */ if(check_dataset(fid, verbose, symbol, &record, mem_sid) < 0) return -1; HDmemset(&record, 0, sizeof(record)); /* Check for reopen */ iter_to_reopen--; if(iter_to_reopen == 0) { /* Emit informational message */ if(verbose) HDfprintf(stderr, "Reopening file: %s\n", filename); /* Retrieve and print the collection of metadata read retries */ if(print_metadata_retries_info(fid) < 0) HDfprintf(stderr, "Warning: could not obtain metadata retries info\n"); /* Reopen the file */ if(H5Fclose(fid) < 0) return -1; if((fid = H5Fopen(filename, H5F_ACC_RDONLY | H5F_ACC_SWMR_READ, fapl)) < 0) return -1; iter_to_reopen = reopen_count; } /* end if */ } /* end while */ /* Retrieve and print the collection of metadata read retries */ if(print_metadata_retries_info(fid) < 0) HDfprintf(stderr, "Warning: could not obtain metadata retries info\n"); /* Close file */ if(H5Fclose(fid) < 0) return -1; /* Close the memory dataspace */ if(H5Sclose(mem_sid) < 0) return -1; return 0; } /* end read_records() */
med_err _MEDattributeStringWrByName(med_idt pid, const char * const path, const char * const attname, const med_size attsize, const char * const val) { med_access_mode MED_ACCESS_MODE; med_idt _attid=0,aid=0; med_err _ret=-1; int type_hdf=0; med_bool _attmustbecreated= MED_FALSE; hsize_t _attsize=0; med_size _valsize=0; if ( (MED_ACCESS_MODE = _MEDmodeAcces(pid) ) == MED_ACC_UNDEF ) { MED_ERR_(_ret,MED_ERR_INVALID,MED_ERR_ACCESSMODE, "MED_ACC_UNDEF" ); SSCRUTE(attname); SSCRUTE(path);goto ERROR; } _valsize=strlen(val); if (_valsize > attsize) { MED_ERR_(_ret,MED_ERR_INVALID,MED_ERR_ATTRIBUTE, attname ); ISCRUTE_size(_valsize);ISCRUTE_size(attsize);goto ERROR; } if ((aid = H5Screate(H5S_SCALAR)) < 0){ MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_DATASPACE, attname ); ISCRUTE_id(aid); } if ( (type_hdf = H5Tcopy(H5T_C_S1)) < 0) { MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_HDFTYPE, MED_ERR_NAME_MSG ); SSCRUTE("H5T_C_S1"); goto ERROR; } if ( H5Tset_size(type_hdf,_valsize+1) < 0) { MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_HDFTYPE, MED_ERR_NAME_MSG ); SSCRUTE("H5T_C_S1"); goto ERROR; } if ( (_attid=H5Aopen_by_name( pid, path, attname, H5P_DEFAULT, H5P_DEFAULT )) >= 0 ) if ( MED_ACCESS_MODE == MED_ACC_RDEXT ) { MED_ERR_(_ret,MED_ERR_INVALID,MED_ERR_ACCESSMODE, "MED_ACC_RDEXT" ); SSCRUTE(attname); SSCRUTE(path);goto ERROR; } if ( _attid > 0 ) { if ( (_attsize=H5Aget_storage_size(_attid) ) < 0 ) goto ERROR; if ( (_valsize+1) > _attsize ) { if (H5Aclose(_attid) < 0) { MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_ATTRIBUTE,""); ISCRUTE_id(_attid); goto ERROR; } if ( H5Adelete_by_name(pid,path,attname,H5P_DEFAULT) < 0 ) goto ERROR; _attmustbecreated=MED_TRUE; } } if ( (_attid < 0) || _attmustbecreated ) if ( (_attid=H5Acreate_by_name( pid, path, attname, type_hdf, aid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT )) < 0 ) { MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_ATTRIBUTE, attname ); SSCRUTE(path);goto ERROR; } if ( H5Awrite(_attid,type_hdf,val) < 0) { MED_ERR_(_ret,MED_ERR_WRITE,MED_ERR_ATTRIBUTE, attname ); SSCRUTE(path);goto ERROR; } _ret=0; ERROR: if (type_hdf > 0 ) if ( H5Tclose(type_hdf) < 0) { MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_HDFTYPE, MED_ERR_ID_MSG ); ISCRUTE_int(type_hdf); } if (aid > 0 ) if ( H5Sclose(aid) < 0) { MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_DATASPACE, MED_ERR_ID_MSG ); ISCRUTE_id(aid); } if (_attid >0) if ( H5Aclose(_attid) < 0) { MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_ATTRIBUTE, MED_ERR_ID_MSG ); ISCRUTE_id(_attid); } return _ret; }