/** * Creates the output workspace for this algorithm * @param inputWorkspace A parent workspace to initialize from. * @return A pointer to the output workspace. */ API::MatrixWorkspace_sptr Transpose::createOutputWorkspace( API::MatrixWorkspace_const_sptr inputWorkspace) { Mantid::API::Axis *yAxis = getVerticalAxis(inputWorkspace); const size_t oldNhist = inputWorkspace->getNumberHistograms(); const auto &inX = inputWorkspace->x(0); const size_t oldYlength = inputWorkspace->blocksize(); const size_t oldVerticalAxislength = yAxis->length(); // The input Y axis may be binned so the new X data should be too size_t newNhist(oldYlength), newXsize(oldVerticalAxislength), newYsize(oldNhist); MatrixWorkspace_sptr outputWorkspace = inputWorkspace->cloneEmpty(); outputWorkspace->initialize(newNhist, newXsize, newYsize); outputWorkspace->setTitle(inputWorkspace->getTitle()); outputWorkspace->setComment(inputWorkspace->getComment()); outputWorkspace->copyExperimentInfoFrom(inputWorkspace.get()); outputWorkspace->setYUnit(inputWorkspace->YUnit()); outputWorkspace->setYUnitLabel(inputWorkspace->YUnitLabel()); outputWorkspace->setDistribution(inputWorkspace->isDistribution()); // Create a new numeric axis for Y the same length as the old X array // Values come from input X API::NumericAxis *newYAxis(nullptr); if (inputWorkspace->isHistogramData()) { newYAxis = new API::BinEdgeAxis(inX.rawData()); } else { newYAxis = new API::NumericAxis(inX.rawData()); } newYAxis->unit() = inputWorkspace->getAxis(0)->unit(); outputWorkspace->getAxis(0)->unit() = inputWorkspace->getAxis(1)->unit(); outputWorkspace->replaceAxis(1, newYAxis); setProperty("OutputWorkspace", outputWorkspace); return outputWorkspace; }
/** * Return true if the units and distribution-type of the workspaces make them * compatible * @param lhs :: first workspace to check for compatibility * @param rhs :: second workspace to check for compatibility * @return workspace unit compatibility flag */ bool Plus::checkUnitCompatibility( const API::MatrixWorkspace_const_sptr lhs, const API::MatrixWorkspace_const_sptr rhs) const { if (lhs->size() > 1 && rhs->size() > 1) { if (lhs->YUnit() != rhs->YUnit()) { g_log.error("The two workspaces are not compatible because they have " "different units for the data (Y)."); return false; } if (lhs->isDistribution() != rhs->isDistribution()) { g_log.error("The two workspaces are not compatible because one is " "flagged as a distribution."); return false; } } return true; }
/** Initialise the member variables * @param inputWS The input workspace */ void ConvertUnits::setupMemberVariables(const API::MatrixWorkspace_const_sptr inputWS) { m_numberOfSpectra = inputWS->getNumberHistograms(); // In the context of this algorithm, we treat things as a distribution if the flag is set // AND the data are not dimensionless m_distribution = inputWS->isDistribution() && !inputWS->YUnit().empty(); //Check if its an event workspace m_inputEvents = ( boost::dynamic_pointer_cast<const EventWorkspace>(inputWS) != NULL ); m_inputUnit = inputWS->getAxis(0)->unit(); const std::string targetUnit = getPropertyValue("Target"); m_outputUnit = UnitFactory::Instance().create(targetUnit); }
void Divide::setOutputUnits(const API::MatrixWorkspace_const_sptr lhs,const API::MatrixWorkspace_const_sptr rhs,API::MatrixWorkspace_sptr out) { if ( rhs->YUnit().empty() || !WorkspaceHelpers::matchingBins(lhs,rhs,true) ) { // Do nothing } // If the Y units match, then the output will be a distribution and will be dimensionless else if ( lhs->YUnit() == rhs->YUnit() && rhs->blocksize() > 1 ) { out->setYUnit(""); out->isDistribution(true); } // Else we need to set the unit that results from the division else { if ( ! lhs->YUnit().empty() ) out->setYUnit(lhs->YUnit() + "/" + rhs->YUnit()); else out->setYUnit("1/" + rhs->YUnit()); } }
/** Write out a MatrixWorkspace's data as a 2D matrix. * Use writeNexusProcessedDataEvent if writing an EventWorkspace. */ int NexusFileIO::writeNexusProcessedData2D( const API::MatrixWorkspace_const_sptr& localworkspace, const bool& uniformSpectra, const std::vector<int>& spec, const char * group_name, bool write2Ddata) const { NXstatus status; //write data entry status=NXmakegroup(fileID,group_name,"NXdata"); if(status==NX_ERROR) return(2); NXopengroup(fileID,group_name,"NXdata"); // write workspace data const size_t nHist=localworkspace->getNumberHistograms(); if(nHist<1) return(2); const size_t nSpectBins=localworkspace->readY(0).size(); const size_t nSpect=spec.size(); int dims_array[2] = { static_cast<int>(nSpect),static_cast<int>(nSpectBins) }; // Set the axis labels and values Mantid::API::Axis *xAxis=localworkspace->getAxis(0); Mantid::API::Axis *sAxis=localworkspace->getAxis(1); std::string xLabel,sLabel; if ( xAxis->isSpectra() ) xLabel = "spectraNumber"; else { if ( xAxis->unit() ) xLabel = xAxis->unit()->unitID(); else xLabel = "unknown"; } if ( sAxis->isSpectra() ) sLabel = "spectraNumber"; else { if ( sAxis->unit() ) sLabel = sAxis->unit()->unitID(); else sLabel = "unknown"; } // Get the values on the vertical axis std::vector<double> axis2; if (nSpect < nHist) for (size_t i=0;i<nSpect;i++) axis2.push_back((*sAxis)(spec[i])); else for (size_t i=0;i<sAxis->length();i++) axis2.push_back((*sAxis)(i)); int start[2]={0,0}; int asize[2]={1,dims_array[1]}; // -------------- Actually write the 2D data ---------------------------- if (write2Ddata) { std::string name="values"; NXcompmakedata(fileID, name.c_str(), NX_FLOAT64, 2, dims_array,m_nexuscompression,asize); NXopendata(fileID, name.c_str()); for(size_t i=0;i<nSpect;i++) { int s = spec[i]; NXputslab(fileID, reinterpret_cast<void*>(const_cast<double*>(&(localworkspace->readY(s)[0]))),start,asize); start[0]++; } if(m_progress != 0) m_progress->reportIncrement(1, "Writing data"); int signal=1; NXputattr (fileID, "signal", &signal, 1, NX_INT32); // More properties const std::string axesNames="axis2,axis1"; NXputattr (fileID, "axes", reinterpret_cast<void*>(const_cast<char*>(axesNames.c_str())), static_cast<int>(axesNames.size()), NX_CHAR); std::string yUnits=localworkspace->YUnit(); std::string yUnitLabel=localworkspace->YUnitLabel(); NXputattr (fileID, "units", reinterpret_cast<void*>(const_cast<char*>(yUnits.c_str())), static_cast<int>(yUnits.size()), NX_CHAR); NXputattr (fileID, "unit_label", reinterpret_cast<void*>(const_cast<char*>(yUnitLabel.c_str())), static_cast<int>(yUnitLabel.size()), NX_CHAR); NXclosedata(fileID); // error name="errors"; NXcompmakedata(fileID, name.c_str(), NX_FLOAT64, 2, dims_array,m_nexuscompression,asize); NXopendata(fileID, name.c_str()); start[0]=0; for(size_t i=0;i<nSpect;i++) { int s = spec[i]; NXputslab(fileID, reinterpret_cast<void*>(const_cast<double*>(&(localworkspace->readE(s)[0]))),start,asize); start[0]++; } if(m_progress != 0) m_progress->reportIncrement(1, "Writing data"); // Fractional area for RebinnedOutput if (localworkspace->id() == "RebinnedOutput") { RebinnedOutput_const_sptr rebin_workspace = boost::dynamic_pointer_cast<const RebinnedOutput>(localworkspace); name="frac_area"; NXcompmakedata(fileID, name.c_str(), NX_FLOAT64, 2, dims_array,m_nexuscompression,asize); NXopendata(fileID, name.c_str()); start[0]=0; for(size_t i=0;i<nSpect;i++) { int s = spec[i]; NXputslab(fileID, reinterpret_cast<void*>(const_cast<double*>(&(rebin_workspace->readF(s)[0]))), start, asize); start[0]++; } if(m_progress != 0) m_progress->reportIncrement(1, "Writing data"); } NXclosedata(fileID); } // write X data, as single array or all values if "ragged" if(uniformSpectra) { dims_array[0]=static_cast<int>(localworkspace->readX(0).size()); NXmakedata(fileID, "axis1", NX_FLOAT64, 1, dims_array); NXopendata(fileID, "axis1"); NXputdata(fileID, reinterpret_cast<void*>(const_cast<double*>(&(localworkspace->readX(0)[0])))); } else { dims_array[0]=static_cast<int>(nSpect); dims_array[1]=static_cast<int>(localworkspace->readX(0).size()); NXmakedata(fileID, "axis1", NX_FLOAT64, 2, dims_array); NXopendata(fileID, "axis1"); start[0]=0; asize[1]=dims_array[1]; for(size_t i=0;i<nSpect;i++) { NXputslab(fileID, reinterpret_cast<void*>(const_cast<double*>(&(localworkspace->readX(i)[0]))),start,asize); start[0]++; } } std::string dist=(localworkspace->isDistribution()) ? "1" : "0"; NXputattr(fileID, "distribution", reinterpret_cast<void*>(const_cast<char*>(dist.c_str())), 2, NX_CHAR); NXputattr (fileID, "units", reinterpret_cast<void*>(const_cast<char*>(xLabel.c_str())), static_cast<int>(xLabel.size()), NX_CHAR); auto label = boost::dynamic_pointer_cast<Mantid::Kernel::Units::Label>(xAxis->unit()); if(label) { NXputattr (fileID, "caption", reinterpret_cast<void*>(const_cast<char*>(label->caption().c_str())), static_cast<int>(label->caption().size()), NX_CHAR); auto unitLbl = label->label(); NXputattr (fileID, "label", reinterpret_cast<void*>(const_cast<char*>(unitLbl.ascii().c_str())), static_cast<int>(unitLbl.ascii().size()), NX_CHAR); } NXclosedata(fileID); if ( ! sAxis->isText() ) { // write axis2, maybe just spectra number dims_array[0]=static_cast<int>(axis2.size()); NXmakedata(fileID, "axis2", NX_FLOAT64, 1, dims_array); NXopendata(fileID, "axis2"); NXputdata(fileID, (void*)&(axis2[0])); NXputattr (fileID, "units", reinterpret_cast<void*>(const_cast<char*>(sLabel.c_str())), static_cast<int>(sLabel.size()), NX_CHAR); auto label = boost::dynamic_pointer_cast<Mantid::Kernel::Units::Label>(sAxis->unit()); if(label) { NXputattr (fileID, "caption", reinterpret_cast<void*>(const_cast<char*>(label->caption().c_str())), static_cast<int>(label->caption().size()), NX_CHAR); auto unitLbl = label->label(); NXputattr (fileID, "label", reinterpret_cast<void*>(const_cast<char*>(unitLbl.ascii().c_str())), static_cast<int>(unitLbl.ascii().size()), NX_CHAR); } NXclosedata(fileID); } else { std::string textAxis; for ( size_t i = 0; i < sAxis->length(); i ++ ) { std::string label = sAxis->label(i); textAxis += label + "\n"; } dims_array[0] = static_cast<int>(textAxis.size()); NXmakedata(fileID, "axis2", NX_CHAR, 2, dims_array); NXopendata(fileID, "axis2"); NXputdata(fileID, reinterpret_cast<void*>(const_cast<char*>(textAxis.c_str()))); NXputattr (fileID, "units", reinterpret_cast<void*>(const_cast<char*>("TextAxis")), 8, NX_CHAR); auto label = boost::dynamic_pointer_cast<Mantid::Kernel::Units::Label>(sAxis->unit()); if(label) { NXputattr (fileID, "caption", reinterpret_cast<void*>(const_cast<char*>(label->caption().c_str())), static_cast<int>(label->caption().size()), NX_CHAR); auto unitLbl = label->label(); NXputattr (fileID, "label", reinterpret_cast<void*>(const_cast<char*>(unitLbl.ascii().c_str())), static_cast<int>(unitLbl.ascii().size()), NX_CHAR); } NXclosedata(fileID); } writeNexusBinMasking(localworkspace); status=NXclosegroup(fileID); return((status==NX_ERROR)?3:0); }