uint64_t File::readiOrder() { uint64_t v; H5::Group group(openGroup( "dark" )); H5::Attribute a(group.openAttribute("iOrder")); a.read( H5::PredType::NATIVE_UINT64, &v ); return v; }
bool XMLBase::isVector(const char* elementName) { openGroup(); // Open dataset and throw if it does not exist xml_node dataset = _groupOpened.node().find_child_by_attribute(_nodeDset.c_str(), _attrName.c_str(), elementName); if (!dataset) { std::ostringstream oss; oss << "Dataset '" << elementName << "' does not exist!"; throw CadetException(oss.str()); } // Read text and attributes size_t rank = dataset.attribute(_attrRank.c_str()).as_int(); std::string dims_str = dataset.attribute(_attrDims.c_str()).value(); // Get dims and compute buffer size std::vector<std::string> dims_vec = split(dims_str, _dimsSeparator.c_str()); int items = 1; for (size_t i = 0; i < rank; ++i) { int j; std::stringstream ss(dims_vec.at(i)); ss >> j; items *= j; } closeGroup(); return items > 1; }
bool XMLBase::exists(const char* elementName) { openGroup(); bool exists = _groupOpened.node().child(elementName); closeGroup(); return exists; }
void File::writeAttribute( const std::string &name, double v ) { H5::Group group(openGroup( "parameters" )); hsize_t dataSize = 1; H5::DataSpace spaceParameters(1,&dataSize,&dataSize); H5::Attribute a(group.createAttribute(name,H5::PredType::NATIVE_DOUBLE, spaceParameters)); a.write( H5::PredType::NATIVE_DOUBLE, &v ); }
void File::readClasses() { hsize_t dims; m_iOrder = readiOrder(); H5::Group group(openGroup( "dark" )); H5::DataSet classes(group.openDataSet("classes")); assert( classes.getSpace().getSimpleExtentNdims() == 1 ); classes.getSpace().getSimpleExtentDims (&dims); m_classes.insert(m_classes.end(),dims,classEntry()); classes.read( &(m_classes[0]), m_classType ); }
uint64_t File::getDarkCount() const { hsize_t dims[2]; H5::Group group(openGroup( "dark" )); H5::DataSet pos(group.openDataSet("position")); assert( pos.getSpace().getSimpleExtentNdims() == 2 ); pos.getSpace().getSimpleExtentDims (dims); assert( dims[1] == 3 ); return dims[0]; }
void File::writeClasses() { H5::Group group(openGroup( "dark" )); H5::DataSet classes(group.openDataSet("classes")); assert( classes.getSpace().getSimpleExtentNdims() == 1 ); hsize_t dataSize = m_classes.size(); hsize_t Zero = 0; H5::DataSpace spaceDisk(1,&dataSize); H5::DataSpace spaceMem(1,&dataSize); classes.extend(&dataSize); spaceDisk.selectHyperslab(H5S_SELECT_SET,&dataSize,&Zero); spaceMem.selectHyperslab(H5S_SELECT_SET,&dataSize,&Zero); classes.write( &(m_classes[0]), m_classType, spaceMem, spaceDisk ); //classes.flush(H5F_SCOPE_LOCAL); }
//-------------------------------------------------------------------------- // Function: CommonFG::openGroup ///\brief This is an overloaded member function, provided for convenience. /// It differs from the above function in that it takes an /// \c H5std_string for \a name. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- Group CommonFG::openGroup( const H5std_string& name ) const { return( openGroup( name.c_str() )); }
/** Goes thoguh a histogram NXS file and counts the number of pixels. * It also determines the name of the data field and axis to load * * @param nexusfilename :: nxs file path * @param entry_name :: name of the entry * @param bankNames :: returns the list of bank names */ void LoadTOFRawNexus::countPixels(const std::string &nexusfilename, const std::string &entry_name, std::vector<std::string> &bankNames) { m_numPixels = 0; m_numBins = 0; m_dataField = ""; m_axisField = ""; bankNames.clear(); // Create the root Nexus class auto file = new ::NeXus::File(nexusfilename); // Open the default data group 'entry' file->openGroup(entry_name, "NXentry"); // Also pop into the instrument file->openGroup("instrument", "NXinstrument"); // Look for all the banks std::map<std::string, std::string> entries = file->getEntries(); std::map<std::string, std::string>::iterator it; for (it = entries.begin(); it != entries.end(); ++it) { std::string name = it->first; if (name.size() > 4) { if (name.substr(0, 4) == "bank") { // OK, this is some bank data file->openGroup(name, it->second); // -------------- Find the data field name ---------------------------- if (m_dataField.empty()) { std::map<std::string, std::string> entries = file->getEntries(); std::map<std::string, std::string>::iterator it; for (it = entries.begin(); it != entries.end(); ++it) { if (it->second == "SDS") { file->openData(it->first); if (file->hasAttr("signal")) { int signal = 0; file->getAttr("signal", signal); if (signal == m_signalNo) { // That's the right signal! m_dataField = it->first; // Find the corresponding X axis std::string axes; m_assumeOldFile = false; if (!file->hasAttr("axes")) { if (1 != m_signalNo) { throw std::runtime_error( "Your chosen signal number, " + Strings::toString(m_signalNo) + ", corresponds to the data field '" + m_dataField + "' has no 'axes' attribute specifying."); } else { m_assumeOldFile = true; axes = "x_pixel_offset,y_pixel_offset,time_of_flight"; } } if (!m_assumeOldFile) { file->getAttr("axes", axes); } std::vector<std::string> allAxes; boost::split(allAxes, axes, boost::algorithm::detail::is_any_ofF<char>(",")); if (allAxes.size() != 3) throw std::runtime_error( "Your chosen signal number, " + Strings::toString(m_signalNo) + ", corresponds to the data field '" + m_dataField + "' which has only " + Strings::toString(allAxes.size()) + " dimension. Expected 3 dimensions."); m_axisField = allAxes.back(); g_log.information() << "Loading signal " << m_signalNo << ", " << m_dataField << " with axis " << m_axisField << std::endl; file->closeData(); break; } // Data has a 'signal' attribute } // Yes, it is a data field file->closeData(); } // each entry in the group } } file->closeGroup(); } // bankX name } } // each entry if (m_dataField.empty()) throw std::runtime_error("Your chosen signal number, " + Strings::toString(m_signalNo) + ", was not found in any of the data fields of any " "'bankX' group. Cannot load file."); for (it = entries.begin(); it != entries.end(); ++it) { std::string name = it->first; if (name.size() > 4) { if (name.substr(0, 4) == "bank") { // OK, this is some bank data file->openGroup(name, it->second); std::map<std::string, std::string> entries = file->getEntries(); if (entries.find("pixel_id") != entries.end()) { bankNames.push_back(name); // Count how many pixels in the bank file->openData("pixel_id"); std::vector<int64_t> dims = file->getInfo().dims; file->closeData(); if (!dims.empty()) { size_t newPixels = 1; for (auto dim : dims) newPixels *= dim; m_numPixels += newPixels; } } else { bankNames.push_back(name); // Get the number of pixels from the offsets arrays file->openData("x_pixel_offset"); std::vector<int64_t> xdim = file->getInfo().dims; file->closeData(); file->openData("y_pixel_offset"); std::vector<int64_t> ydim = file->getInfo().dims; file->closeData(); if (!xdim.empty() && !ydim.empty()) { m_numPixels += (xdim[0] * ydim[0]); } } if (entries.find(m_axisField) != entries.end()) { // Get the size of the X vector file->openData(m_axisField); std::vector<int64_t> dims = file->getInfo().dims; // Find the units, if available if (file->hasAttr("units")) file->getAttr("units", m_xUnits); else m_xUnits = "microsecond"; // use default file->closeData(); if (!dims.empty()) m_numBins = dims[0] - 1; } file->closeGroup(); } // bankX name } } // each entry file->close(); delete file; }
/** Load a single bank into the workspace * * @param nexusfilename :: file to open * @param entry_name :: NXentry name * @param bankName :: NXdata bank name * @param WS :: workspace to modify * @param id_to_wi :: det ID to workspace index mapping */ void LoadTOFRawNexus::loadBank(const std::string &nexusfilename, const std::string &entry_name, const std::string &bankName, API::MatrixWorkspace_sptr WS, const detid2index_map &id_to_wi) { g_log.debug() << "Loading bank " << bankName << std::endl; // To avoid segfaults on RHEL5/6 and Fedora m_fileMutex.lock(); // Navigate to the point in the file auto file = new ::NeXus::File(nexusfilename); file->openGroup(entry_name, "NXentry"); file->openGroup("instrument", "NXinstrument"); file->openGroup(bankName, "NXdetector"); size_t m_numPixels = 0; std::vector<uint32_t> pixel_id; if (!m_assumeOldFile) { // Load the pixel IDs file->readData("pixel_id", pixel_id); m_numPixels = pixel_id.size(); if (m_numPixels == 0) { file->close(); m_fileMutex.unlock(); g_log.warning() << "Invalid pixel_id data in " << bankName << std::endl; return; } } else { // Load the x and y pixel offsets std::vector<float> xoffsets; std::vector<float> yoffsets; file->readData("x_pixel_offset", xoffsets); file->readData("y_pixel_offset", yoffsets); m_numPixels = xoffsets.size() * yoffsets.size(); if (0 == m_numPixels) { file->close(); m_fileMutex.unlock(); g_log.warning() << "Invalid (x,y) offsets in " << bankName << std::endl; return; } size_t bankNum = 0; if (bankName.size() > 4) { if (bankName.substr(0, 4) == "bank") { bankNum = boost::lexical_cast<size_t>(bankName.substr(4)); bankNum--; } else { file->close(); m_fileMutex.unlock(); g_log.warning() << "Invalid bank number for " << bankName << std::endl; return; } } // All good, so construct the pixel ID listing size_t numX = xoffsets.size(); size_t numY = yoffsets.size(); for (size_t i = 0; i < numX; i++) { for (size_t j = 0; j < numY; j++) { pixel_id.push_back( static_cast<uint32_t>(j + numY * (i + numX * bankNum))); } } } size_t iPart = 0; if (m_spec_max != Mantid::EMPTY_INT()) { uint32_t ifirst = pixel_id[0]; range_check out_range(m_spec_min, m_spec_max, id_to_wi); auto newEnd = std::remove_if(pixel_id.begin(), pixel_id.end(), out_range); pixel_id.erase(newEnd, pixel_id.end()); // check if beginning or end of array was erased if (ifirst != pixel_id[0]) iPart = m_numPixels - pixel_id.size(); m_numPixels = pixel_id.size(); if (m_numPixels == 0) { file->close(); m_fileMutex.unlock(); g_log.warning() << "No pixels from " << bankName << std::endl; return; }; } // Load the TOF vector std::vector<float> tof; file->readData(m_axisField, tof); size_t m_numBins = tof.size() - 1; if (tof.size() <= 1) { file->close(); m_fileMutex.unlock(); g_log.warning() << "Invalid " << m_axisField << " data in " << bankName << std::endl; return; } // Make a shared pointer MantidVecPtr Xptr; MantidVec &X = Xptr.access(); X.resize(tof.size(), 0); X.assign(tof.begin(), tof.end()); // Load the data. Coerce ints into double. std::string errorsField = ""; std::vector<double> data; file->openData(m_dataField); file->getDataCoerce(data); if (file->hasAttr("errors")) file->getAttr("errors", errorsField); file->closeData(); // Load the errors bool hasErrors = !errorsField.empty(); std::vector<double> errors; if (hasErrors) { try { file->openData(errorsField); file->getDataCoerce(errors); file->closeData(); } catch (...) { g_log.information() << "Error loading the errors field, '" << errorsField << "' for bank " << bankName << ". Will use sqrt(counts). " << std::endl; hasErrors = false; } } /*if (data.size() != m_numBins * m_numPixels) { file->close(); m_fileMutex.unlock(); g_log.warning() << "Invalid size of '" << m_dataField << "' data in " << bankName << std::endl; return; } if (hasErrors && (errors.size() != m_numBins * m_numPixels)) { file->close(); m_fileMutex.unlock(); g_log.warning() << "Invalid size of '" << errorsField << "' errors in " << bankName << std::endl; return; } */ // Have all the data I need m_fileMutex.unlock(); file->close(); for (size_t i = iPart; i < iPart + m_numPixels; i++) { // Find the workspace index for this detector detid_t pixelID = pixel_id[i - iPart]; size_t wi = id_to_wi.find(pixelID)->second; // Set the basic info of that spectrum ISpectrum *spec = WS->getSpectrum(wi); spec->setSpectrumNo(specid_t(wi + 1)); spec->setDetectorID(pixel_id[i - iPart]); // Set the shared X pointer spec->setX(X); // Extract the Y MantidVec &Y = spec->dataY(); Y.assign(data.begin() + i * m_numBins, data.begin() + (i + 1) * m_numBins); MantidVec &E = spec->dataE(); if (hasErrors) { // Copy the errors from the loaded document E.assign(errors.begin() + i * m_numBins, errors.begin() + (i + 1) * m_numBins); } else { // Now take the sqrt(Y) to give E E = Y; std::transform(E.begin(), E.end(), E.begin(), (double (*)(double))sqrt); } } // Done! }
void File::copyData( const File &src ) { uint64_t N = src.getDarkCount(); uint64_t O = src.m_iOrder; uint64_t i; hsize_t n; hsize_t off[2], Dims[2]; std::cout << "Copying data from " << src.m_name << std::endl; H5::Group dstGroup(openGroup( "dark" )); H5::Group srcGroup(src.openGroup( "dark" )); H5::DataSet dst_pos( dstGroup.openDataSet("position") ); H5::DataSet dst_vel( dstGroup.openDataSet("velocity") ); //H5::DataSet dst_cls( dstGroup.openDataSet("class") ); H5::DataSet src_pos( srcGroup.openDataSet("position") ); H5::DataSet src_vel( srcGroup.openDataSet("velocity") ); //H5::DataSet src_cls( srcGroup.openDataSet("class") ); double *buffer = new double[3*CHUNK_SIZE]; src_pos.getSpace().getSimpleExtentDims(Dims); H5::DataSpace src_spaceDisk(2,Dims); dst_pos.getSpace().getSimpleExtentDims(Dims); H5::DataSpace dst_spaceDisk(2,Dims); for ( i=0; i<N; i+=n ) { n = N-i > CHUNK_SIZE ? CHUNK_SIZE : N-i; Dims[0] = n; Dims[1] = 3; H5::DataSpace spaceMem(2,Dims); Dims[0] = n; Dims[1] = 3; off[0] = off[1] = 0; spaceMem.selectHyperslab(H5S_SELECT_SET,Dims,off); off[0] = i; off[1] = 0; src_spaceDisk.selectHyperslab(H5S_SELECT_SET,Dims,off); src_pos.read( buffer, H5::PredType::NATIVE_DOUBLE, spaceMem, src_spaceDisk ); off[0] = i+O; off[1] = 0; dst_spaceDisk.selectHyperslab(H5S_SELECT_SET,Dims,off); dst_pos.write( buffer, H5::PredType::NATIVE_DOUBLE, spaceMem, dst_spaceDisk ); off[0] = i; off[1] = 0; src_spaceDisk.selectHyperslab(H5S_SELECT_SET,Dims,off); src_vel.read( buffer, H5::PredType::NATIVE_DOUBLE, spaceMem, src_spaceDisk ); off[0] = i+O; off[1] = 0; dst_spaceDisk.selectHyperslab(H5S_SELECT_SET,Dims,off); dst_vel.write( buffer, H5::PredType::NATIVE_DOUBLE, spaceMem, dst_spaceDisk ); } delete [] buffer; }
void File::readAttribute( const std::string &name, double &v ) { H5::Group group(openGroup( "parameters" )); H5::Attribute a(group.openAttribute(name)); a.read( H5::PredType::NATIVE_DOUBLE, &v ); }
void File::copyAttributes( const File *src ) { H5::Group gdst(openGroup( "parameters" )); H5::Group gsrc(src->openGroup( "parameters" )); gsrc.iterateAttrs(doAddAttribute, NULL, (void *)&gdst); }