//-***************************************************************************** bool EquivalentDatatypes( hid_t iA, hid_t iB ) { if ( iA >= 0 && iB >= 0 ) { if ( H5Tequal( iA, iB ) > 0 ) { return true; } // If they're not equal, but they are both arrayed and // both have the same super type // and dimensions, they're equivalent if ( H5Tget_class( iA ) == H5T_ARRAY && H5Tget_class( iB ) == H5T_ARRAY ) { hid_t superA = H5Tget_super( iA ); hid_t superB = H5Tget_super( iB ); if ( superA >= 0 && superB >= 0 && H5Tequal( superA, superB ) > 0 ) { Dimensions aDims; getDatatypeArrayDims( iA, aDims ); Dimensions bDims; getDatatypeArrayDims( iB, bDims ); if ( aDims == bDims ) { return true; } } } } return false; }
void ReadStringsT( hid_t iParent, const std::string &iAttrName, size_t iNumStrings, StringT *oStrings ) { ABCA_ASSERT( iParent >= 0, "Invalid parent in ReadStringsT" ); // Open the attribute. hid_t attrId = H5Aopen( iParent, iAttrName.c_str(), H5P_DEFAULT ); ABCA_ASSERT( attrId >= 0, "Couldn't open attribute named: " << iAttrName ); AttrCloser attrCloser( attrId ); // Checking code. { hid_t attrFtype = H5Aget_type( attrId ); DtypeCloser dtypeCloser( attrFtype ); hid_t nativeDtype = GetNativeDtype<CharT>(); ABCA_ASSERT( H5Tget_class( attrFtype ) == H5Tget_class( nativeDtype ) && H5Tget_sign( attrFtype ) == H5Tget_sign( nativeDtype ), "Invalid datatype for stringT" ); } hid_t attrSpace = H5Aget_space( attrId ); ABCA_ASSERT( attrSpace >= 0, "Couldn't get dataspace for attribute: " << iAttrName ); DspaceCloser dspaceCloser( attrSpace ); hssize_t numPoints = H5Sget_simple_extent_npoints( attrSpace ); ABCA_ASSERT( numPoints > 0, "Degenerate string dimensions in ReadStringsT" ); // Create temporary char storage buffer. std::vector<CharT> charStorage( ( size_t )( 1 + numPoints ), ( CharT )0 ); // Read into it. herr_t status = H5Aread( attrId, GetNativeDtype<CharT>(), ( void * )&charStorage.front() ); ABCA_ASSERT( status >= 0, "Couldn't read from attribute: " << iAttrName ); // Extract 'em. ExtractStrings( oStrings, ( const CharT * )&charStorage.front(), 1 + numPoints, iNumStrings ); }
void write(const hid_attribute_adaptor& attrib, const std::string& value) { hid_t attr_type_id = H5CPP_ERR_ON_NEG(H5Aget_type(attrib.id())); if(H5Tget_class(attr_type_id) != H5T_STRING) H5CPP_THROW("Attempted to set string value on non-string attribute '" << object::get_name(attrib.id()) << '\''); const char* cstr = value.c_str(); if(H5Tis_variable_str(attr_type_id)) H5CPP_ERR_ON_NEG(H5Awrite(attrib.id(), attr_type_id, &cstr)); else { std::size_t attriblen = H5Aget_storage_size(attrib.id()); if(attriblen > value.length()) { // avoid reading past end of value into unalloc'd mem std::vector<char> buf(attriblen,'\0'); std::copy(cstr, cstr+value.length(), &buf[0]); H5CPP_ERR_ON_NEG(H5Awrite(attrib.id(), attr_type_id, &buf[0])); } else H5CPP_ERR_ON_NEG(H5Awrite(attrib.id(), attr_type_id, cstr)); } }
/*------------------------------------------------------------------------- * subroutine for test_text_dtype(): test_compounds(). *------------------------------------------------------------------------- */ static int test_compounds(void) { hid_t dtype; int nmembs; char *memb_name = NULL; H5T_class_t memb_class; H5T_class_t type_class; char* dt_str; size_t str_len; TESTING3(" text for compound types"); if((dtype = H5LTtext_to_dtype("H5T_COMPOUND { H5T_STD_I16BE \"one_field\" : 2; H5T_STD_U8LE \"two_field\" : 6; }", H5LT_DDL))<0) goto out; if((type_class = H5Tget_class(dtype))<0) goto out; if(type_class != H5T_COMPOUND) goto out; if((nmembs = H5Tget_nmembers(dtype))<0) goto out; if(nmembs != 2) goto out; if(H5LTdtype_to_text(dtype, NULL, H5LT_DDL, &str_len)<0) goto out; dt_str = (char*)calloc(str_len, sizeof(char)); if(H5LTdtype_to_text(dtype, dt_str, H5LT_DDL, &str_len)<0) goto out; if(strcmp(dt_str, "H5T_COMPOUND {\n H5T_STD_I16BE \"one_field\" : 2;\n H5T_STD_U8LE \"two_field\" : 6;\n }")) { printf("dt=\n%s\n", dt_str); goto out; } free(dt_str); if(H5Tclose(dtype)<0) goto out; if((dtype = H5LTtext_to_dtype("H5T_COMPOUND { H5T_STD_I32BE \"i32_field\"; H5T_STD_I16BE \"i16_field\"; H5T_COMPOUND { H5T_STD_I16BE \"sec_field\"; H5T_COMPOUND { H5T_STD_I32BE \"thd_field\"; } \"grandchild\"; } \"child_compound\"; H5T_STD_I8BE \"i8_field\"; }", H5LT_DDL))<0) goto out; if((memb_name = H5Tget_member_name(dtype, 1)) == NULL) goto out; if(strcmp(memb_name, "i16_field")) goto out; free(memb_name); if((memb_class = H5Tget_member_class(dtype, 2))<0) goto out; if(memb_class != H5T_COMPOUND) goto out; PASSED(); return 0; out: H5_FAILED(); return -1; }
std::string hdf5attribute::read_as_string() { std::string result; if(H5Tequal(_type_id, H5T_NATIVE_INT16)) result = boost::lexical_cast<std::string>(read<short>()); else if(H5Tequal(_type_id, H5T_NATIVE_INT32)) result = boost::lexical_cast<std::string>(read<int>()); else if(H5Tequal(_type_id, H5T_NATIVE_UINT32)) result = boost::lexical_cast<std::string>(read<unsigned int>()); else if(H5Tequal(_type_id, H5T_NATIVE_UINT16)) result = boost::lexical_cast<std::string>(read<unsigned short>()); else if(H5Tequal(_type_id, H5T_NATIVE_UINT8)) result = boost::lexical_cast<std::string>(read<unsigned char>()); else if(H5Tequal(_type_id, H5T_NATIVE_INT8)) result = boost::lexical_cast<std::string>(read<char>()); else if(H5Tequal(_type_id, H5T_NATIVE_FLOAT)) result = boost::lexical_cast<std::string>(read<float>()); else if(H5Tequal(_type_id, H5T_NATIVE_DOUBLE)) result = boost::lexical_cast<std::string>(read<double>()); else if(H5Tget_class(_type_id) == H5T_STRING) result = read<std::string>(); else throw std::runtime_error("unknown type"); return result; }
H5T_class_t HdfDataset::type() const { hid_t tid = H5Dget_type( d->id ); H5T_class_t t_class = H5Tget_class( tid ); H5Tclose( tid ); return t_class; }
/**************************************************************** ** ** getHDF5ClassID(): Returns class ID for loc_id.name. -1 if error. ** ****************************************************************/ H5T_class_t getHDF5ClassID(hid_t loc_id, const char *name, H5D_layout_t *layout, hid_t *type_id, hid_t *dataset_id) { H5T_class_t class_id; hid_t plist; /* Open the dataset. */ if ( (*dataset_id = H5Dopen( loc_id, name, H5P_DEFAULT )) < 0 ) return -1; /* Get an identifier for the datatype. */ *type_id = H5Dget_type( *dataset_id ); /* Get the class. */ class_id = H5Tget_class( *type_id ); /* Get the layout of the datatype */ plist = H5Dget_create_plist(*dataset_id); *layout = H5Pget_layout(plist); H5Pclose(plist); return class_id; }
/* This may be ultimately confused with nested types with 2 components called 'r' and 'i' and being floats, but in that case, the user most probably wanted to keep a complex type, so getting a complex instead of a nested type should not be a big issue (I hope!) :-/ F. Alted 2005-05-23 */ int is_complex(hid_t type_id) { hid_t class_id, base_type_id; hid_t class1, class2; char *colname1, *colname2; int result = 0; hsize_t nfields; class_id = H5Tget_class(type_id); if (class_id == H5T_COMPOUND) { nfields = H5Tget_nmembers(type_id); if (nfields == 2) { colname1 = H5Tget_member_name(type_id, 0); colname2 = H5Tget_member_name(type_id, 1); if ((strcmp(colname1, "r") == 0) && (strcmp(colname2, "i") == 0)) { class1 = H5Tget_member_class(type_id, 0); class2 = H5Tget_member_class(type_id, 1); if (class1 == H5T_FLOAT && class2 == H5T_FLOAT) result = 1; } free(colname1); free(colname2); } } /* Is an Array of Complex? */ else if (class_id == H5T_ARRAY) { /* Get the array base component */ base_type_id = H5Tget_super(type_id); /* Call is_complex again */ result = is_complex(base_type_id); H5Tclose(base_type_id); } return result; }
// JRC: This fat interface may not scale? What about // scalar attributes? herr_t VsH5Attribute::getDoubleVectorValue(std::vector<double>* dvals) { herr_t err = 0; size_t npoints; hid_t atype = H5Aget_type(getId()); H5T_class_t type = H5Tget_class(atype); hid_t aspace = H5Aget_space(getId()); size_t rank = H5Sget_simple_extent_ndims(aspace); if (type != H5T_FLOAT) { VsLog::warningLog() <<"VsH5Attribute::getDoubleVectorValue() - Requested attribute " <<getShortName() <<" is not a floating point vector." <<std::endl; dvals->resize(0); return -1; } if (rank == 0) { dvals->resize(1); double v; err = H5Aread(getId(), H5T_NATIVE_DOUBLE, &v); (*dvals)[0] = v; return err; } // rank>0 npoints = H5Sget_simple_extent_npoints(aspace); double* v = new double[npoints]; err = H5Aread(getId(), H5T_NATIVE_DOUBLE, v); dvals->resize(npoints); for (size_t i = 0; i<npoints; ++i) { (*dvals)[i] = v[i]; } delete [] v; return err; }
static herr_t get_attribute_float(hid_t input, const char *name, float *val) { hid_t attr_id; hid_t type_id; H5T_class_t type_class; size_t type_size; herr_t status; char *strval; attr_id = H5Aopen_name(input, name); type_id = H5Aget_type(attr_id); type_class = H5Tget_class(type_id); type_size = H5Tget_size(type_id); H5Tclose(type_id); H5Aclose(attr_id); switch(type_class) { case H5T_STRING: status = get_attribute_str(input, name, &strval); if (status < 0) return -1; *val = atof(strval); free(strval); return 0; case H5T_FLOAT: status = get_attribute(input, name, H5T_NATIVE_FLOAT, val); if (status < 0) return -1; return 0; } return -1; }
//-------------------------------------------------------------------------- // Function: AbstractDs::getTypeClass ///\brief Returns the class of the datatype that is used by this /// object, which can be a dataset or an attribute. ///\return Datatype class identifier ///\exception H5::DataTypeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- H5T_class_t AbstractDs::getTypeClass() const { // Gets the datatype used by this dataset or attribute. // p_get_type calls either H5Dget_type or H5Aget_type depending on // which object invokes getTypeClass hid_t datatype_id; try { datatype_id = p_get_type(); // returned value is already validated } catch (DataSetIException E) { throw DataTypeIException("DataSet::getTypeClass", E.getDetailMsg()); } catch (AttributeIException E) { throw DataTypeIException("Attribute::getTypeClass", E.getDetailMsg()); } // Gets the class of the datatype and validate it before returning H5T_class_t type_class = H5Tget_class(datatype_id); if( type_class != H5T_NO_CLASS ) return( type_class ); else { if (fromClass() == "DataSet") throw DataTypeIException("DataSet::getTypeClass", "H5Tget_class returns H5T_NO_CLASS"); else if (fromClass() == "Attribute") throw DataTypeIException("Attribute::getTypeClass", "H5Tget_class returns H5T_NO_CLASS"); } }
size_t getNestedSizeType(hid_t type_id) { hid_t member_type_id; H5T_class_t class_id; hsize_t i, nfields; size_t itemsize, offset; nfields = H5Tget_nmembers(type_id); offset = 0; // Iterate thru the members for (i=0; i < nfields; i++) { // Get the member type member_type_id = H5Tget_member_type(type_id, i); // Get the HDF5 class class_id = H5Tget_class(member_type_id); if (class_id == H5T_COMPOUND) { // Get the member size for compound type itemsize = getNestedSizeType(member_type_id); } else { // Get the atomic member size itemsize = H5Tget_size(member_type_id); } // Update the offset offset = offset + itemsize; } return(offset); }
/*-------------------------------------------------------------*/ static void validateFloat3Attribute(pNXVcontext self, hid_t dpField, char *name) { hid_t attID, attType, attSpace; H5T_class_t h5class; hsize_t dims[2], maxDims[2]; char fname[512]; memset(fname,0,sizeof(fname)); H5Iget_name(dpField,fname,sizeof(fname)); if(!H5LTfind_attribute(dpField,name)){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "Missing attribute %s on %s", name, fname); NXVlog(self); self->errCount++; } else { attID = H5Aopen(dpField,name,H5P_DEFAULT); assert(attID >= 0); attType = H5Aget_type(attID); assert(attType >= 0); h5class = H5Tget_class(attType); if(h5class != H5T_FLOAT){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "%s attribute on %s is of wrong type, expected float", name, fname); NXVlog(self); self->errCount++; } else { attSpace = H5Aget_space(attID); if(H5Sget_simple_extent_ndims(attSpace) != 1){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "%s attribute on %s is of wrong rank, expected 1", name, fname); NXVlog(self); self->errCount++; } else { H5Sget_simple_extent_dims(attSpace,dims,maxDims); if(dims[0] != 3){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "%s attribute on %s is of wrong size, expected 3", name, fname); NXVlog(self); self->errCount++; } } H5Sclose(attSpace); } H5Tclose(attType); H5Aclose(attID); } }
/* fetch num_cols and the col for a particular trackname */ void get_cols(chromosome_t *chromosome, char *trackname, hsize_t *num_cols, hsize_t *col) { hid_t attr, root, dataspace, datatype; hsize_t data_size, cell_size, num_cells; char *attr_data; /* Tracknames are stored in the attributes of the root group of each file */ root = H5Gopen(chromosome->h5group, "/", H5P_DEFAULT); assert(root >= 0); attr = H5Aopen_name(root, "tracknames"); assert(attr >= 0); dataspace = H5Aget_space(attr); assert(dataspace >= 0); assert(H5Sget_simple_extent_dims(dataspace, num_cols, NULL) == 1); assert(H5Sclose(dataspace) >= 0); if (trackname && col) { datatype = H5Aget_type(attr); assert(datatype >= 0); assert(H5Tget_class(datatype) == H5T_STRING); cell_size = H5Tget_size(datatype); assert(cell_size > 0); data_size = H5Aget_storage_size(attr); assert(data_size > 0); num_cells = data_size / cell_size; /* allocate room for tracknames */ attr_data = xmalloc(data_size); assert(attr_data); assert(H5Aread(attr, datatype, attr_data) >= 0); *col = 0; for (*col = 0; *col <= num_cells; (*col)++) { if (*col == num_cells) { fprintf(stderr, "can't find trackname: %s\n", trackname); free(attr_data); exit(EXIT_FAILURE); } else { if (!strncmp(attr_data + (*col * cell_size), trackname, cell_size)) { break; } } } /* clean up read tracknames */ free(attr_data); } assert(H5Aclose(attr) >= 0); }
/*-------------------------------------------------------------- This validates the lesser depends_on chain fields like vector, offset and transformation_type ----------------------------------------------------------------*/ static void validateDependsOnAttributes(pNXVcontext self,hid_t dpField) { char fname[512], transData[512]; hid_t attID, attType, attSpace; H5T_class_t h5class; memset(fname,0,sizeof(fname)); memset(transData,0,sizeof(transData)); H5Iget_name(dpField,fname,sizeof(fname)); /* deal with transformation_type */ if(!H5LTfind_attribute(dpField,"transformation_type")){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "Missing attribute transformation_type on %s", fname); NXVlog(self); self->errCount++; } else { attID = H5Aopen(dpField,"transformation_type",H5P_DEFAULT); assert(attID >= 0); attType = H5Aget_type(attID); assert(attType >= 0); h5class = H5Tget_class(attType); if(h5class != H5T_STRING){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "transformation_type on %s is of wrong type, expected string", fname); NXVlog(self); self->errCount++; } else { H5NXget_attribute_string(self->fileID, fname, "transformation_type",transData); if(strcmp(transData,"translation") != 0 && strcmp(transData,"rotation") != 0){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "transformation_type on %s contains bad data: %s", fname, "expected rotation or translation"); NXVlog(self); self->errCount++; } } H5Tclose(attType); H5Aclose(attID); } validateFloat3Attribute(self,dpField,"offset"); validateFloat3Attribute(self,dpField,"vector"); }
DataType ImageBase::datatypeH5(hid_t h5datatype) { H5T_sign_t h5sign = H5Tget_sign(h5datatype); // if (h5sign == H5T_SGN_ERROR) // REPORT_ERROR(ERR_IO, "datatypeHDF5: Integer sign error in dataset."); bool sign = (h5sign > H5T_SGN_NONE); size_t size = H5Tget_size(h5datatype); DataType dt; switch(H5Tget_class(h5datatype)) { case H5T_FLOAT: { switch(size) { case 4: dt = DT_Float; break; case 8: dt = DT_Double; break; default: REPORT_ERROR(ERR_IO_SIZE, "datatypeHDF5: bad datatype size"); } } break; case H5T_INTEGER: { switch(size) { case 1: dt = (sign)? DT_SChar : DT_UChar; break; case 2: dt = (sign)? DT_Short : DT_UShort; break; case 4: dt = (sign)? DT_Int : DT_UInt; break; case 8: dt = (sign)? DT_Long : DT_ULong; break; default: REPORT_ERROR(ERR_IO_SIZE, "datatypeHDF5: bad datatype size"); } } break; case H5T_NO_CLASS: default: dt = DT_Unknown; break; } return dt; }
/*--------------------------------------------------------------*/ static void validateDependsOn(pNXVcontext self, hid_t groupID, hid_t fieldID) { char fname[512], dpData[1024]; hid_t h5type, dpfieldID; H5T_class_t h5class; memset(fname,0,sizeof(fname)); memset(dpData,0,sizeof(dpData)); H5Iget_name(fieldID,fname,sizeof(fname)); NXVsetLog(self,"dataPath",fname); NXVsetLog(self,"sev","debug"); NXVprintLog(self,"message","Validating depends_on chain starting at %s", fname); NXVlog(self); /* test that the depends_on field is of the right type */ h5type = H5Dget_type(fieldID); h5class = H5Tget_class(h5type); H5Tclose(h5type); if(h5class != H5T_STRING){ NXVsetLog(self,"sev","error"); NXVsetLog(self,"message", "depends_on field is of wrong type, expect string"); NXVlog(self); self->errCount++; return; } /* read the field */ H5LTread_dataset_string(groupID,"depends_on",dpData); /* find the field and start iterating through the chain */ dpfieldID = findDependentField(self,fieldID,dpData); if(dpfieldID < 0){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "Cannot even find the starting point of the depends_on chain, %s", dpData); NXVlog(self); self->errCount++; return; } else { validateDependsOnField(self,groupID,dpfieldID); H5Dclose(dpfieldID); } }
static herr_t get_attribute_info(hid_t obj_id, const char *attr_name, hsize_t *dims, H5T_class_t *type_class, size_t *type_size, hid_t *type_id) { hid_t attr_id; hid_t space_id; herr_t status; int rank; /* Open the attribute. */ attr_id = H5Aopen_name(obj_id, attr_name); if (attr_id < 0) return -1; /* Get an identifier for the datatype. */ *type_id = H5Aget_type(attr_id); /* Get the class. */ *type_class = H5Tget_class(*type_id); /* Get the size. */ *type_size = H5Tget_size(*type_id); /* Get the dataspace handle */ space_id = H5Aget_space(attr_id); if (space_id < 0) goto out; /* Get dimensions */ rank = H5Sget_simple_extent_dims(space_id, dims, NULL); if (rank < 0) goto out; /* Terminate access to the dataspace */ status = H5Sclose(space_id); if (status < 0) goto out; /* End access to the attribute */ status = H5Aclose(attr_id); if (status < 0) goto out; return 0; out: H5Tclose(*type_id); H5Aclose(attr_id); return -1; }
/*------------------------------------------------------------------------- * subroutine for test_text_dtype(): test_arrays(). *------------------------------------------------------------------------- */ static int test_arrays(void) { hid_t dtype; int ndims; hsize_t dims[3]; H5T_class_t type_class; char* dt_str; size_t str_len; TESTING3(" text for array types"); if((dtype = H5LTtext_to_dtype("H5T_ARRAY { [5][7][13] H5T_ARRAY { [17][19] H5T_COMPOUND { H5T_STD_I8BE \"arr_compound_1\"; H5T_STD_I32BE \"arr_compound_2\"; } } }", H5LT_DDL))<0) goto out; if((type_class = H5Tget_class(dtype))<0) goto out; if(type_class != H5T_ARRAY) goto out; if((ndims = H5Tget_array_ndims(dtype))<0) goto out; if(ndims != 3) goto out; if(H5Tget_array_dims2(dtype, dims) < 0) goto out; if(dims[0] != 5 || dims[1] != 7 || dims[2] != 13) goto out; if(H5LTdtype_to_text(dtype, NULL, H5LT_DDL, &str_len)<0) goto out; dt_str = (char*)calloc(str_len, sizeof(char)); if(H5LTdtype_to_text(dtype, dt_str, H5LT_DDL, &str_len)<0) goto out; if(strcmp(dt_str, "H5T_ARRAY {\n [5][7][13] H5T_ARRAY {\n [17][19] H5T_COMPOUND {\n H5T_STD_I8BE \"arr_compound_1\" : 0;\n H5T_STD_I32BE \"arr_compound_2\" : 1;\n }\n }\n }")) { printf("dt=\n%s\n", dt_str); goto out; } free(dt_str); if(H5Tclose(dtype)<0) goto out; PASSED(); return 0; out: H5_FAILED(); return -1; }
void PWParameterSet::checkVersion(hid_t h) { if(is_manager()) { hid_t dataset=H5Dopen(h,"version"); hid_t datatype=H5Dget_type(dataset); H5T_class_t classtype = H5Tget_class(datatype); H5Tclose(datatype); H5Dclose(dataset); if(classtype == H5T_INTEGER) { HDFAttribIO<TinyVector<int,2> > hdfver(version); hdfver.read(h,"version"); } else if(classtype == H5T_FLOAT) { TinyVector<double,2> vt; HDFAttribIO<TinyVector<double,2> > hdfver(vt); hdfver.read(h,"version"); version[0]=int(vt[0]); version[1]=int(vt[1]); } //else //{ // APP_ABORT("PWParameterSet::checkVersion The type of version is not integer or double."); //} } myComm->bcast(version); app_log() << "\tWavefunction HDF version: " << version[0] << "." << version[1] << endl; if(version[0] == 0) { if(version[1] == 11) { hasSpin=false; paramTag="parameters_0"; basisTag="basis_1"; pwTag="planewaves"; pwMultTag="multipliers"; eigTag="eigenstates_3"; twistTag="twist_"; bandTag="band_"; } else if(version[1] == 10) { pwMultTag="planewaves"; pwTag="0"; } } }
/*------------------------------------------------------------------------- * Function: h5tools_get_native_type * * Purpose: Wrapper around H5Tget_native_type() to work around * Problems with bitfields. * * Return: Success: datatype ID * * Failure: FAIL * * Programmer: Quincey Koziol * Tuesday, October 5, 2004 * * Modifications: * *------------------------------------------------------------------------- */ hid_t h5tools_get_native_type(hid_t type) { hid_t p_type; H5T_class_t type_class; type_class = H5Tget_class(type); if(type_class==H5T_BITFIELD) p_type=H5Tcopy(type); else p_type = H5Tget_native_type(type,H5T_DIR_DEFAULT); return(p_type); }
static int try_read_string(hid_t loc, char * name, char ** dest){ if(H5Lexists(loc,name,H5P_DEFAULT)){ hid_t ds = H5Dopen(loc,name,H5P_DEFAULT); hid_t t = H5Dget_type(ds); if(H5Tget_class(t) == H5T_STRING){ *dest = malloc(sizeof(char)*H5Tget_size(t)); H5Dread(ds,t,H5S_ALL,H5S_ALL,H5P_DEFAULT,*dest); } H5Tclose(t); H5Dclose(ds); return 1; } return 0; }
/*------------------------------------------------------------------------- * subroutine for test_text_dtype(): test_variables(). *------------------------------------------------------------------------- */ static int test_variables(void) { hid_t dtype; H5T_class_t type_class; char* dt_str; size_t str_len; TESTING3(" text for variable types"); if((dtype = H5LTtext_to_dtype("H5T_VLEN { H5T_NATIVE_CHAR }\n", H5LT_DDL))<0) goto out; if((type_class = H5Tget_class(dtype))<0) goto out; if(type_class != H5T_VLEN) goto out; if(H5Tis_variable_str(dtype)) goto out; if(H5Tclose(dtype)<0) goto out; if((dtype = H5LTtext_to_dtype("H5T_VLEN { H5T_VLEN { H5T_STD_I32BE } }", H5LT_DDL))<0) goto out; if(H5Tis_variable_str(dtype)) goto out; if(H5LTdtype_to_text(dtype, NULL, H5LT_DDL, &str_len)<0) goto out; dt_str = (char*)calloc(str_len, sizeof(char)); if(H5LTdtype_to_text(dtype, dt_str, H5LT_DDL, &str_len)<0) goto out; if(strcmp(dt_str, "H5T_VLEN {\n H5T_VLEN {\n H5T_STD_I32BE\n }\n }")) { printf("dt=\n%s\n", dt_str); goto out; } free(dt_str); if(H5Tclose(dtype)<0) goto out; PASSED(); return 0; out: H5_FAILED(); return -1; }
/* * Class: hdf_hdf5lib_H5 * Method: H5AreadVL * Signature: (JJJJJ[Ljava/lang/String;)I */ JNIEXPORT jint JNICALL Java_hdf_hdf5lib_H5_H5AreadVL (JNIEnv *env, jclass clss, jlong attr_id, jlong mem_type_id, jobjectArray buf) { herr_t status = -1; htri_t isStr = 0; htri_t isVlenStr = 0; htri_t isComplex = 0; if (buf == NULL) { h5nullArgument(env, "H5AreadVL: buf is NULL"); } /* end if */ else { isStr = H5Tdetect_class((hid_t)mem_type_id, H5T_STRING); if (H5Tget_class((hid_t)mem_type_id) == H5T_COMPOUND) { unsigned i; int nm = H5Tget_nmembers(mem_type_id); for(i = 0; i <nm; i++) { hid_t nested_tid = H5Tget_member_type((hid_t)mem_type_id, i); isComplex = H5Tdetect_class((hid_t)nested_tid, H5T_COMPOUND) || H5Tdetect_class((hid_t)nested_tid, H5T_VLEN); H5Tclose(nested_tid); } } else if (H5Tget_class((hid_t)mem_type_id) == H5T_VLEN) { isVlenStr = 1; /* strings created by H5Tvlen_create(H5T_C_S1) */ } if (isStr == 0 || isComplex>0 || isVlenStr) { status = H5AreadVL_asstr(env, (hid_t)attr_id, (hid_t)mem_type_id, buf); } else if (isStr > 0) { status = H5AreadVL_str(env, (hid_t)attr_id, (hid_t)mem_type_id, buf); } } /* end else */ return (jint)status; } /* end Java_hdf_hdf5lib_H5_H5Aread_1VL */
bool GH5_FetchAttribute( hid_t loc_id, const char *pszAttrName, CPLString &osResult, bool bReportError ) { bool retVal = false; hid_t hAttr = H5Aopen_name( loc_id, pszAttrName ); osResult.clear(); if( hAttr < 0 ) { if( bReportError ) CPLError( CE_Failure, CPLE_AppDefined, "Attempt to read attribute %s failed, not found.", pszAttrName ); return false; } hid_t hAttrTypeID = H5Aget_type( hAttr ); hid_t hAttrNativeType = H5Tget_native_type( hAttrTypeID, H5T_DIR_DEFAULT ); if( H5Tget_class( hAttrNativeType ) == H5T_STRING ) { int nAttrSize = H5Tget_size( hAttrTypeID ); char *pachBuffer = (char *) CPLCalloc(nAttrSize+1,1); H5Aread( hAttr, hAttrNativeType, pachBuffer ); osResult = pachBuffer; CPLFree( pachBuffer ); retVal = true; } else { if( bReportError ) CPLError( CE_Failure, CPLE_AppDefined, "Attribute %s of unsupported type for conversion to string.", pszAttrName ); retVal = false; } H5Tclose( hAttrNativeType ); H5Tclose( hAttrTypeID ); H5Aclose( hAttr ); return retVal; }
static int try_read_int(hid_t loc, char * name, int * dest){ if(H5Lexists(loc,name,H5P_DEFAULT)){ hid_t ds = H5Dopen(loc,name,H5P_DEFAULT); hid_t t = H5Dget_type(ds); hid_t s = H5Dget_space(ds); if(H5Tget_class(t) == H5T_INTEGER && is_scalar(ds)){ H5Dread(ds,H5T_NATIVE_INT32,H5S_ALL,H5S_ALL,H5P_DEFAULT,dest); } H5Tclose(t); H5Sclose(s); H5Dclose(ds); return 1; } return 0; }
static int try_read_float_array(hid_t loc, char * name, double * dest, int size){ if(H5Lexists(loc,name,H5P_DEFAULT)){ hid_t ds = H5Dopen(loc,name,H5P_DEFAULT); hid_t t = H5Dget_type(ds); hid_t s = H5Dget_space(ds); if(H5Tget_class(t) == H5T_FLOAT && array_total_size(ds) == size){ H5Dread(ds,H5T_NATIVE_DOUBLE,H5S_ALL,H5S_ALL,H5P_DEFAULT,dest); } H5Tclose(t); H5Sclose(s); H5Dclose(ds); return 1; } return 0; }
std::string compare_and_read_scalar_attribute(hid_t attribute_id, hid_t type_id) { if(H5Tget_class(type_id) == H5T_STRING) { int size = H5Tget_size(type_id); std::string data(size+1, ' '); herr_t status = H5Aread(attribute_id, type_id, &(data[0])); if(status >= 0) { data.resize(size); return data; } } throw std::runtime_error("type mismatch"); }
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; }
/* * - Name : _MEDdatasetRead * - Description : writes a HDF dataset * - Parameters : * - id (IN) : dataset ID * - val (OUT) : datset values * - Result : * - if success : 0 * - if failure : -1 */ hdf_err HDFdatasetRead(hdf_idt id, void *val) { hdf_idt datatype; hdf_err ret; if ((datatype = H5Dget_type(id)) < 0) return -1; //#if defined (PCLINUX) || defined (PCLINUX64) if ((H5Tget_class(datatype) == H5T_INTEGER) && (H5Tget_size(datatype) == 4)) datatype = H5T_NATIVE_INT; //#endif if ((ret = H5Dread(id,datatype,H5S_ALL,H5S_ALL,H5P_DEFAULT, val)) < 0) return -1; return 0; }