bool loadStackHDF5( const char* fileName, Image4DSimple& img ) { #ifdef USE_HDF5 H5::Exception::dontPrint(); H5::H5File file( fileName, H5F_ACC_RDONLY ); for ( size_t i = 0; i < file.getObjCount(); i++ ) { H5std_string name = file.getObjnameByIdx( i ); if ( name == "Channels" ) { H5::Group channels = file.openGroup( name ); // Grab the attributes H5::Attribute attr = channels.openAttribute( "width" ); H5::DataType type = attr.getDataType(); long width, height; attr.read( type, &width ); attr.close(); attr = channels.openAttribute( "height" ); attr.read( type, &height ); attr.close(); int num_channels = 0; // Count the number of channels for ( size_t obj = 0; obj < channels.getNumObjs(); obj++ ) if ( channels.getObjTypeByIdx( obj ) == H5G_DATASET ) num_channels++; int channel_idx = 0; for ( size_t obj = 0; obj < channels.getNumObjs(); obj++ ) { if ( channels.getObjTypeByIdx( obj ) == H5G_DATASET ) { H5std_string ds_name = channels.getObjnameByIdx( obj ); H5::DataSet data = channels.openDataSet( ds_name ); uint8_t* buffer = new uint8_t[ data.getStorageSize() ]; data.read( buffer, data.getDataType() ); QByteArray qbarray( ( const char* )buffer, data.getStorageSize() ); data.close(); if ( !loadIndexedStackFFMpeg( &qbarray, img, channel_idx++, num_channels, width, height ) ) { v3d_msg( "Error happened in HDF file reading. Stop. \n", false ); return false; } delete [] buffer; } } } } #endif return true; }
/** *@return */ int HDF5FileReader::getNumberOfVariables() { long numVars; H5::Group group = this->current_file->openGroup("Variables"); numVars = group.getNumObjs(); return (int)numVars; }
void pyne::Material::_load_comp_protocol0(H5::H5File * db, std::string datapath, int row) { H5::Group matgroup = (*db).openGroup(datapath); H5::DataSet nucset; double nucvalue; hsize_t matG = matgroup.getNumObjs(); // Iterate over datasets in the group. for (int matg = 0; matg < matG; matg++) { std::string nuckey = matgroup.getObjnameByIdx(matg); nucset = matgroup.openDataSet(nuckey); nucvalue = h5wrap::get_array_index<double>(&nucset, row); if (nuckey == "Mass" || nuckey == "MASS" || nuckey == "mass") mass = nucvalue; else comp[pyne::nucname::zzaaam(nuckey)] = nucvalue; nucset.close(); }; // Set meta data name = datapath.substr(datapath.rfind("/")+1, datapath.length()); atoms_per_mol = -1.0; };
bool Bundle2::checkGeometry_(H5::H5File& file) const { bool found = false; H5::Group root = file.openGroup("/"); const hsize_t maxObjs = root.getNumObjs(); for(hsize_t obj = 0; obj < maxObjs; ++obj) { string objName = root.getObjnameByIdx(obj); if(objName == string("Geometry")) found = true; } root.close(); return found; }
static void attributes_append_group(Attributes &attrs, H5::Group &g, cpath basename, cpath group_path) { for(uint i=0;i<g.getNumObjs();i++) { char g_name[1024]; g.getObjnameByIdx(hsize_t(i), g_name, 1024); cpath name = group_path / g_name; H5G_stat_t stat; g.getObjinfo(g_name, false, stat); if (stat.type == H5G_GROUP) { H5::Group sub = g.openGroup(g_name); attributes_append_group(attrs, sub, basename, name); } else if (stat.type == H5G_LINK) { //FIXME we assume soft link! Attribute attr; char attr_name[1024]; char link[1024]; attr.setName(remove_prefix(name, basename)); H5L_info_t info; H5Lget_val(g.getId(), g_name, link, 1024, H5P_DEFAULT); attr.setLink(remove_prefix(link, basename)); attrs.append(attr); } } for(int i=0;i<g.getNumAttrs();i++) { H5::Attribute h5attr = g.openAttribute(i); Attribute attr; BaseType type; char name[1024]; H5Aget_name(h5attr.getId(), 1024, name); read_attr(&attr, g, basename, group_path, name, type); attrs.append(attr); } }
/********************************************************* FunctionName:RecursiveVisitNode FunctionDesc:递归创建Group InputParam: OutputParam: Return: Author:xiaowei.han *********************************************************/ void RecursiveVisitNode(H5::Group& Node, Hdf5_Wrapper::LP_HDF5_DATA pData) { using namespace H5; if (nullptr == pData) { return; } //获取节点名称 pData->strKeyName = Utility::UTF8ToGB2312(Node.getObjName()); //获取属性个数 int nAttrNum = static_cast<int>(Node.getNumAttrs()); for (int i = 0; i < nAttrNum; ++i) { auto childAttr = Node.openAttribute(i); //解析读取属性数据 //获取数据类型 auto AttrDataType = childAttr.getDataType(); //获取数据类型大小 unsigned int nDataByte = (unsigned int)AttrDataType.getSize(); boost::scoped_ptr<CAbstractAttrHanlder> pHandler(CreateAttrHandler(childAttr.getTypeClass(), nDataByte)); if (pHandler) { pHandler->ReadAttr(childAttr, AttrDataType, pData->AttributeArray); } childAttr.close(); } //获取节点对象个数 int nObjNum = static_cast<int>(Node.getNumObjs()); //遍历子节点信息 for (int i = 0; i < nObjNum; ++i) { //NECESSARY_LOG("the node name is [%s],the type is [%d].", Node.getObjnameByIdx(i).c_str(), Node.getObjTypeByIdx(i)); auto SubNodeType = Node.getObjTypeByIdx(i); switch (SubNodeType) { case H5G_GROUP: { Hdf5_Wrapper::LP_HDF5_DATA pSubData = new Hdf5_Wrapper::HDF5_DATA; if (nullptr != pSubData) { //添加进去 pData->SubDataArray.push_back(pSubData); auto ChildGroupNode = Node.openGroup(Node.getObjnameByIdx(i)); RecursiveVisitNode(ChildGroupNode,pSubData); ChildGroupNode.close(); } } break; case H5G_DATASET: { auto childDataset = Node.openDataSet(Node.getObjnameByIdx(i)); Hdf5_Wrapper::LP_HDF5_DATA pSubData = new Hdf5_Wrapper::HDF5_DATA; if (nullptr != pSubData) { pSubData->strKeyName = childDataset.getObjName(); //添加进去 pData->SubDataArray.push_back(pSubData); //获取数据类型 auto ChildDataSetDataType = childDataset.getDataType(); //获取类型占用字节数 unsigned int nDataByte = (unsigned int)ChildDataSetDataType.getSize(); //判断数据类型 boost::scoped_ptr<CAbstractDataTypeHandler> pHandlder(CDataTypeHandlerFactory::CreateDataTypeHandler(childDataset.getTypeClass(),nDataByte)); if (pHandlder) { pHandlder->ReadDataSet(childDataset, pSubData); } } } break; default: break; } } }
//-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- TEST(DISABLED_HDFTests, BasicFileRead) { std::string file_path = "D:/ResInsight/SourSim/PKMUNK_NOV_TEST_SS.sourpre.00001"; try { H5::Exception::dontPrint(); // Turn off auto-printing of failures to handle the errors appropriately H5::H5File file(file_path.c_str(), H5F_ACC_RDONLY); { H5::Group timestep = file.openGroup("Timestep_00001"); H5::Attribute attr = timestep.openAttribute("timestep"); double timestep_value = 0.0; H5::DataType type = attr.getDataType(); attr.read(type, ×tep_value); //std::cout << "Timestep value " << timestep_value << std::endl; EXPECT_NEAR(timestep_value, 1.0, 1e-1); } { // Group size is not an attribute! H5::Group GridFunctions = file.openGroup("Timestep_00001/GridParts/GridPart_00000/GridFunctions"); hsize_t group_size = GridFunctions.getNumObjs(); //std::cout << "GridFunctions group_size " << group_size << std::endl; EXPECT_EQ(size_t(20), group_size); /* for (hsize_t i = 0; i < group_size; i++) { // H5std_string node_name = GridFunctions.getObjnameByIdx(i); // crashes on VS2017 due to lib/heap/runtime differences to HDF5 VS2015 lib std::string node_name; node_name.resize(1024); ssize_t slen = GridFunctions.getObjnameByIdx(i, &node_name[0], 1023); node_name.resize(slen + 1); std::cout << "GridFunctions sub-node name " << node_name << std::endl; } */ std::string first_subnode(1024, '\0'); ssize_t slen = GridFunctions.getObjnameByIdx(0, &first_subnode[0], 1023); first_subnode.resize(slen + 1); EXPECT_TRUE(first_subnode.compare(0, slen, "GridFunction_00002") == 0); } { H5::Group GridFunction_00002 = file.openGroup("Timestep_00001/GridParts/GridPart_00000/GridFunctions/GridFunction_00002"); H5::Attribute attr = GridFunction_00002.openAttribute("limits_max"); double limits_max = 0.0; H5::DataType type = attr.getDataType(); attr.read(type, &limits_max); // std::cout << "limits_max " << limits_max << std::endl; EXPECT_NEAR(limits_max, 0.3970204292629652, 1e-10); } { H5::Group GridFunction_00002 = file.openGroup("Timestep_00001/GridParts/GridPart_00000/GridFunctions/GridFunction_00002"); H5::DataSet dataset = H5::DataSet(GridFunction_00002.openDataSet("values")); hsize_t dims[2]; H5::DataSpace dataspace = dataset.getSpace(); dataspace.getSimpleExtentDims(dims, nullptr); std::vector<double> values; values.resize(dims[0]); dataset.read(values.data(), H5::PredType::NATIVE_DOUBLE); /* for (hsize_t i = 0; i < dims[0]; i++) { std::cout << "value " << i << " " << values[i] << std::endl; } */ EXPECT_NEAR(values[0], 0.32356910366452146, 1e-10); EXPECT_NEAR(values[dims[0] - 1], 0.12200070891582514, 1e-10); } } // end of try block catch (H5::FileIException error) // catch failure caused by the H5File operations { std::cout << error.getCDetailMsg(); } catch (H5::DataSetIException error) // catch failure caused by the DataSet operations { std::cout << error.getCDetailMsg(); } catch (H5::DataSpaceIException error) // catch failure caused by the DataSpace operations { std::cout << error.getCDetailMsg(); } catch (H5::DataTypeIException error) // catch failure caused by the DataSpace operations { std::cout << error.getCDetailMsg(); } }
void ossim_hdf5::iterateGroupForDatasetNames( H5::H5File* file, const std::string& groupName, std::vector<std::string>& datasetNames, ossim_uint32& recursedCount ) { if ( file && groupName.size() ) { ++recursedCount; // std::cout << "iterateGroup: " << groupName << std::endl; H5::Group* group = new H5::Group( file->openGroup(groupName) ); const hsize_t OBJ_COUNT = group->getNumObjs(); for ( hsize_t i = 0; i < OBJ_COUNT; ++i ) { std::string objName = group->getObjnameByIdx(i); if ( objName.size() ) { char separator = '/'; std::string combinedName; combine( groupName, objName, separator, combinedName ); H5G_obj_t objType = group->getObjTypeByIdx(i); #if 0 std::cout << "combinedName: " << combinedName << "\ngetObjnameByIdx[" << i << "]: " << objName << "\ngetObjTypeByIdx[" << i << "]: " << objType << std::endl; #endif if ( objType == H5G_GROUP ) { // Recursive call: if ( recursedCount < ossim_hdf5::MAX_RECURSION_LEVEL ) { ossim_hdf5::iterateGroupForDatasetNames( file, combinedName, datasetNames, recursedCount ); } else { ossimNotify(ossimNotifyLevel_WARN) << "ossim_hdf5::iterateGroupForDatasetNames WARNING!" << "\nMax iterations reached!" << std::endl; } } else if ( objType == H5G_DATASET ) { datasetNames.push_back( combinedName ); } else { ossimNotify(ossimNotifyLevel_WARN) << "ossim_hdf5::iterateGroupForDatasetNames WARNING!" << "\nUnhandled object type: " << objType << std::endl; } } } group->close(); delete group; group = 0; --recursedCount; } // Matches: if ( file ) } // End: void ossim_hdf5::iterateGroupForDatasetNames
void ossim_hdf5::printIterative( H5::H5File* file, const std::string& groupName, const std::string& prefix, ossim_uint32& recursedCount, std::ostream& out ) { if ( file && groupName.size() ) { ++recursedCount; H5::Group* group = new H5::Group( file->openGroup(groupName) ); // Print attributes: const ossim_uint32 ATTRS_COUNT = group->getNumAttrs(); for ( ossim_uint32 aIdx = 0; aIdx < ATTRS_COUNT; ++aIdx ) { H5::Attribute attr( group->openAttribute( aIdx ) ); ossim_hdf5::printAttribute( attr, prefix, out ); attr.close(); } const hsize_t OBJ_COUNT = group->getNumObjs(); for ( hsize_t i = 0; i < OBJ_COUNT; ++i ) { std::string objName = group->getObjnameByIdx(i); if ( objName.size() ) { char separator = '/'; std::string combinedName; combine( groupName, objName, separator, combinedName ); separator = '.'; std::string combinedPrefix; combine( prefix, objName, separator, combinedPrefix ); H5G_obj_t objType = group->getObjTypeByIdx(i); #if 0 std::cout << "combinedName: " << combinedName << "\ncombinedPrefix: " << combinedPrefix << "\ngetObjnameByIdx[" << i << "]: " << objName << "\ngetObjTypeByIdx[" << i << "]: " << objType << std::endl; #endif if ( objType == H5G_GROUP ) { // Recursive call: if ( recursedCount < ossim_hdf5::MAX_RECURSION_LEVEL ) { ossim_hdf5::printIterative( file, combinedName, combinedPrefix, recursedCount, out ); } else { ossimNotify(ossimNotifyLevel_WARN) << "ossim_hdf5::printIterative WARNING!" << "\nMax iterations reached!" << std::endl; } } else if ( objType == H5G_DATASET ) { printObject( file, combinedName, combinedPrefix, out ); } else { ossimNotify(ossimNotifyLevel_WARN) << "ossim_hdf5::printIterative WARNING!" << "\nUnhandled object type: " << objType << std::endl; } } } group->close(); delete group; group = 0; --recursedCount; } // Matches: if ( file ) } // End: void ossim_hdf5::printIterative method.
void Bundle2::loadGeometry_(H5::H5File& file) { H5::Group geometryGroup = file.openGroup("/Geometry"); // Loading poses H5::DataSet posesDataSet = geometryGroup.openDataSet("Poses"); double* posesData = (double*)malloc(frames_.size()*12*sizeof(double)); posesDataSet.read((void*)posesData, H5::PredType::NATIVE_DOUBLE, H5::DataSpace::ALL, H5::DataSpace::ALL); posesDataSet.close(); size_t i = 0; for(deque<Frame*>::iterator it = frames_.begin(); it != frames_.end(); ++it) { Pose* pose = new Pose; pose->sett(core::RealPoint3D<double>(posesData[i*12], posesData[i*12 + 1], posesData[i*12 + 2])); core::Matrix<double> R(3, 3); R[0][0] = posesData[i*12 + 3]; R[1][0] = posesData[i*12 + 4]; R[2][0] = posesData[i*12 + 5]; R[0][1] = posesData[i*12 + 6]; R[1][1] = posesData[i*12 + 7]; R[2][1] = posesData[i*12 + 8]; R[0][2] = posesData[i*12 + 9]; R[1][2] = posesData[i*12 + 10]; R[2][2] = posesData[i*12 + 11]; pose->setR(R); pose->calcEulerAngles(); pose->setorientationSynchronWithAngles(true); pose->setderivationsSynchronWithAngles(false); (*it)->setpose(pose); ++i; } free((void*)posesData); // Loading points H5::DataSet pointsDataSet = geometryGroup.openDataSet("Points"); double* pointsData = (double*)malloc(tracks_.size()*3*sizeof(double)); pointsDataSet.read((void*)pointsData, H5::PredType::NATIVE_DOUBLE, H5::DataSpace::ALL, H5::DataSpace::ALL); pointsDataSet.close(); i = 0; for(deque<Track*>::iterator it = tracks_.begin(); it != tracks_.end(); it++) { Point* point = new Point(core::RealPoint3D<double>(pointsData[i*3], pointsData[i*3 + 1], pointsData[i*3 + 2])); (*it)->setpoint(point); ++i; } free((void*)pointsData); // Loading inlier information H5::DataSet inliersDataSet = geometryGroup.openDataSet("Inliers"); hvl_t* inliersData = (hvl_t*)malloc(frames_.size()*sizeof(hvl_t)); H5::VarLenType memType(&H5::PredType::NATIVE_UCHAR); inliersDataSet.read((void*)inliersData, memType, H5::DataSpace::ALL, H5::DataSpace::ALL); memType.close(); inliersDataSet.close(); i = 0; for(deque<Frame*>::iterator it = frames_.begin(); it != frames_.end(); it++) { unsigned char* inl = (unsigned char*)(inliersData[i].p); size_t k = 0; for(size_t j = 0; j < (*it)->size(); ++j) { View& v = (**it)[j]; for(unsigned int cam = 0; cam < v.numCameras(); ++cam) { if(v.inCamera(cam)) { Ray ray; if(inl[k]) ray.setinlier(true); else ray.setinlier(false); v.addRay(cam, ray); ++k; } } } ++i; } for(size_t j = 0; j < frames_.size(); ++j) free(inliersData[j].p); free((void*)inliersData); // Loading curves if they exists bool curvesFound = false; const hsize_t maxObjs = geometryGroup.getNumObjs(); for(hsize_t obj = 0; obj < maxObjs; ++obj) { string objName = geometryGroup.getObjnameByIdx(obj); if(objName == string("Curves")) curvesFound = true; } if(curvesFound) { H5::DataSet curvesDataSet = geometryGroup.openDataSet("Curves"); hsize_t curvesDim[1]; H5::DataSpace curvesDS = curvesDataSet.getSpace(); curvesDS.getSimpleExtentDims(curvesDim); curvesDS.close(); hvl_t* curvesData = (hvl_t*)malloc(curvesDim[0]*sizeof(hvl_t)); H5::VarLenType memType(&H5::PredType::NATIVE_HSIZE); curvesDataSet.read((void*)curvesData, memType, H5::DataSpace::ALL, H5::DataSpace::ALL); memType.close(); curvesDataSet.close(); for(size_t c = 0; c < curvesDim[0]; ++c) { const size_t cur_c = addCurve(); for(size_t p = 0; p < curvesData[c].len; ++p) { curves_[cur_c].addPoint(((size_t*)(curvesData[c].p))[p]); } } for(size_t i = 0; i < curvesDim[0]; ++i) free(curvesData[i].p); free((void*)curvesData); } geometryGroup.close(); }