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 writeStrAttribute(H5::Group &location, const std::string &name, const std::string &value) { StrType attrType(0, H5T_VARIABLE); DataSpace attrSpace(H5S_SCALAR); auto groupAttr = location.createAttribute(name, attrType, attrSpace); groupAttr.write(attrType, 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); }
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); }
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 File::doAddAttribute(H5::H5Object&loc, std::string name, void*data) { double v; //hsize_t size; H5::Group *d = (H5::Group *)data; H5::Attribute a(loc.openAttribute(name)); //size = loc.getStorageSize(); //FIXME: double a.read( H5::PredType::NATIVE_DOUBLE, &v ); hsize_t dataSize = 1; H5::DataSpace spaceParameters(1,&dataSize,&dataSize); H5::Attribute b(d->createAttribute(name,H5::PredType::NATIVE_DOUBLE, spaceParameters)); b.write( H5::PredType::NATIVE_DOUBLE, &v ); }
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; }
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(); }
void Bundle2::streamPOI(size_t frame) { H5::DataSpace scalar; H5::Group poiGroup = streamFile_->openGroup("/POI"); for(size_t i = poiFirstFrame_; i <= frame; ++i) { const std::string frameGroupName = boost::str(boost::format("Frame %1$04d") % i); H5::Group frameGroup = poiGroup.createGroup(frameGroupName); hsize_t count = poi_[(ptrdiff_t)i - (ptrdiff_t)poiFirstFrame_].size(); H5::Attribute attr = frameGroup.createAttribute("count", H5::PredType::STD_U64LE, scalar); attr.write(H5::PredType::NATIVE_HSIZE, &count); attr.close(); for(size_t camera = 0; camera < poi_[(ptrdiff_t)i - (ptrdiff_t)poiFirstFrame_].size(); ++camera) poi_[(ptrdiff_t)i - (ptrdiff_t)poiFirstFrame_][camera].save(frameGroup, camera); frameGroup.close(); } poiGroup.close(); scalar.close(); poi_.erase(poi_.begin(), poi_.begin() + (ptrdiff_t)frame - (ptrdiff_t)poiFirstFrame_ + 1); poiFirstFrame_ = frame + 1; }
bool saveStackHDF5( const char* fileName, const My4DImage& img, Codec_Mapping* mapping ) { try { #ifdef USE_HDF5 H5::Exception::dontPrint(); H5::H5File file( fileName, H5F_ACC_TRUNC ); H5::Group* group = new H5::Group( file.createGroup( "/Channels" ) ); Image4DProxy<My4DImage> proxy( const_cast<My4DImage*>( &img ) ); long scaledHeight = nearestPowerOfEight( proxy.sy ); long scaledWidth = nearestPowerOfEight( proxy.sx ); // Initialize the upper and lower bounds long pad_right = ( scaledWidth - proxy.sx ) ; long pad_bottom = ( scaledHeight - proxy.sy ); hsize_t dims[1] = { 1 }; H5::DataSpace attr_ds = H5::DataSpace( 1, dims ); H5::Attribute attr = group->createAttribute( "width", H5::PredType::STD_I64LE, attr_ds ); attr.write( H5::PredType::NATIVE_INT, &( proxy.sx ) ); attr = group->createAttribute( "height", H5::PredType::STD_I64LE, attr_ds ); attr.write( H5::PredType::NATIVE_INT, &( proxy.sy ) ); attr = group->createAttribute( "frames", H5::PredType::STD_I64LE, attr_ds ); attr.write( H5::PredType::NATIVE_INT, &( proxy.sz ) ); attr = group->createAttribute( "pad_right", H5::PredType::STD_I64LE, attr_ds ); attr.write( H5::PredType::NATIVE_INT, &( pad_right ) ); attr = group->createAttribute( "pad_bottom", H5::PredType::STD_I64LE, attr_ds ); attr.write( H5::PredType::NATIVE_INT, &( pad_bottom ) ); Codec_Mapping* imap = mapping; if ( !mapping ) { imap = new Codec_Mapping(); generate_codec_mapping( *imap, proxy.sc ); } for ( int c = 0; c < proxy.sc; ++c ) { double default_irange = 1.0; // assumes data range is 0-255.0 if ( proxy.su > 1 ) { default_irange = 1.0 / 16.0; // 0-4096, like our microscope images } std::vector<double> imin( proxy.sc, 0.0 ); std::vector<double> irange2( proxy.sc, default_irange ); // rescale if converting from 16 bit to 8 bit if ( proxy.su > 1 ) { if ( img.p_vmin && img.p_vmax ) proxy.set_minmax( img.p_vmin, img.p_vmax ); if ( proxy.has_minmax() ) { imin[c] = proxy.vmin[c]; irange2[c] = 255.0 / ( proxy.vmax[c] - proxy.vmin[c] ); } } FFMpegEncoder encoder( NULL, scaledWidth, scaledHeight, ( *imap )[c].first, ( *imap )[c].second ); // If the image needs padding, fill the expanded border regions with black for ( int z = 0; z < proxy.sz; ++z ) { for ( int y = 0; y < scaledHeight; ++y ) { for ( int x = 0; x < scaledWidth; ++x ) { // If inside the area with valid data if ( x < proxy.sx && y < proxy.sy ) { int ic = c; double val = proxy.value_at( x, y, z, ic ); val = ( val - imin[ic] ) * irange2[ic]; // rescale to range 0-255 for ( int cc = 0; cc < 3; ++cc ) encoder.setPixelIntensity( x, y, cc, ( int )val ); } else for ( int cc = 0; cc < 3; ++cc ) encoder.setPixelIntensity( x, y, cc, 0 ); } } encoder.write_frame(); } for ( int rem = encoder.encoded_frames(); rem < proxy.sz; rem++ ) encoder.encode(); encoder.close(); hsize_t dims[1]; dims[0] = encoder.buffer_size(); H5::DataSpace dataspace( 1, dims ); std: stringstream name; name << "Channel_" << c; H5::DataSet dataset = group->createDataSet( name.str(), H5::PredType::NATIVE_UINT8, dataspace ); dataset.write( encoder.buffer(), H5::PredType::NATIVE_UINT8 ); dataset.close(); std::cout << "Encoded channel is " << encoder.buffer_size() << " bytes." << std::endl; // Uncomment this if you want to dump the individual movies to a temp file } #endif if ( !mapping ) delete imap; file.close(); return true; } catch ( ... ) {} return false; }
// Bundle management void Bundle2::save(const boost::filesystem::path& fileName) const { // Creating HDF5 file H5::H5File bundleFile(fileName.string(), H5F_ACC_TRUNC); storeParameters(bundleFile); H5::DataSpace scalar; // Saving POI H5::Group poiGroup = bundleFile.createGroup("/POI"); H5::Attribute attr = poiGroup.createAttribute("count", H5::PredType::STD_U64LE, scalar); hsize_t count = poi_.size(); attr.write(H5::PredType::NATIVE_HSIZE, &count); attr.close(); for(size_t frame = 0; frame < poi_.size(); ++frame) { const std::string frameGroupName = boost::str(boost::format("Frame %1$04d") % frame); H5::Group frameGroup = poiGroup.createGroup(frameGroupName); count = poi_[frame].size(); attr = frameGroup.createAttribute("count", H5::PredType::STD_U64LE, scalar); attr.write(H5::PredType::NATIVE_HSIZE, &count); attr.close(); for(size_t camera = 0; camera < poi_[frame].size(); ++camera) poi_[frame][camera].save(frameGroup, camera); frameGroup.close(); } poiGroup.close(); // Saving key frames H5::Group bundleGroup = bundleFile.createGroup("/Bundle"); H5::Group framesGroup = bundleGroup.createGroup("Frames"); count = frames_.size(); attr = framesGroup.createAttribute("count", H5::PredType::STD_U64LE, scalar); attr.write(H5::PredType::NATIVE_HSIZE, &count); attr.close(); for(deque<Frame*>::const_iterator it = frames_.begin(); it != frames_.end(); it++) { (*it)->save(framesGroup); } framesGroup.close(); // Saving tracks const hsize_t chunkDim[] = { 2, 1 }; H5::DSetCreatPropList propList; propList.setLayout(H5D_CHUNKED); propList.setChunk(2, chunkDim); propList.setDeflate(9); H5::VarLenType tracksDatasetType(&H5::PredType::STD_U64LE); hsize_t tracksDim[] = { tracks_.size(), 2 }; hsize_t tracksMaxDim[] = { H5S_UNLIMITED, 2 }; H5::DataSpace tracksDataspace(2, tracksDim, tracksMaxDim); H5::DataSet tracksDataset = bundleGroup.createDataSet("Tracks", tracksDatasetType, tracksDataspace, propList); for(size_t i = 0; i < tracks_.size(); ++i) tracks_[i]->save(tracksDataset, i); tracksDataset.close(); tracksDataspace.close(); tracksDatasetType.close(); propList.close(); bundleGroup.close(); scalar.close(); bundleFile.close(); }
void Bundle2::initFrameStream_() { H5::DataSpace scalar; // Creating datasets for each frame H5::Group bundleGroup = streamFile_->createGroup("/Bundle"); H5::Group framesGroup = bundleGroup.createGroup("Frames"); hsize_t count = frames_.size(); H5::Attribute attr = framesGroup.createAttribute("count", H5::PredType::STD_U64LE, scalar); attr.write(H5::PredType::NATIVE_HSIZE, &count); attr.close(); // Defining frame dataset property hsize_t chunkDim[] = { 1, 2, 10 }; H5::DSetCreatPropList propList; propList.setLayout(H5D_CHUNKED); propList.setChunk(3, chunkDim); propList.setDeflate(9); // Definig dataset dataspace hsize_t max_dim[] = { numCameras_, 2, H5S_UNLIMITED }; hsize_t dim[] = { numCameras_, 2, 0 }; H5::DataSpace ds(3, dim, max_dim); for(deque<Frame*>::const_iterator it = frames_.begin(); it != frames_.end(); it++) { const std::string datasetName = boost::str(boost::format("Frame %1$04d") % (*it)->number()); // Creating dataset H5::DataSet frameData = framesGroup.createDataSet(datasetName, H5::PredType::IEEE_F32LE, ds, propList); // Writing global number attr = frameData.createAttribute("globalNumber", H5::PredType::STD_U64LE, scalar); count = (*it)->globalNumber(); attr.write(H5::PredType::NATIVE_HSIZE, &count); attr.close(); // Clean up! frameData.close(); } // Clean up! ds.close(); propList.close(); // Creating tracks dataset hsize_t chunkDim2[] = { 2, 10 }; propList = H5::DSetCreatPropList(); propList.setLayout(H5D_CHUNKED); propList.setChunk(2, chunkDim); propList.setDeflate(9); H5::VarLenType tracksDatasetType(&H5::PredType::STD_U64LE); hsize_t tracksDim[] = { 0, 2 }; hsize_t tracksMaxDim[] = { H5S_UNLIMITED, 2 }; H5::DataSpace tracksDataspace(2, tracksDim, tracksMaxDim); H5::DataSet tracksDataset = bundleGroup.createDataSet("Tracks", tracksDatasetType, tracksDataspace, propList); tracksDataset.close(); tracksDataspace.close(); tracksDatasetType.close(); propList.close(); framesGroup.close(); bundleGroup.close(); scalar.close(); }