Skip to content
Snippets Groups Projects
Commit 7ba58152 authored by Dan Nixon's avatar Dan Nixon
Browse files

Merge pull request #13069 from mantidproject/13056_Fix_X_label_after_FFT

Fix X label after FFT transformation
parents b3b7d547 ddba7bd1
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
//---------------------------------------------------------------------- //----------------------------------------------------------------------
#include "MantidAlgorithms/FFT.h" #include "MantidAlgorithms/FFT.h"
#include "MantidKernel/UnitFactory.h" #include "MantidKernel/UnitFactory.h"
#include "MantidKernel/UnitLabelTypes.h"
#include "MantidAPI/TextAxis.h" #include "MantidAPI/TextAxis.h"
#include <boost/shared_array.hpp> #include <boost/shared_array.hpp>
...@@ -127,25 +128,41 @@ void FFT::exec() { ...@@ -127,25 +128,41 @@ void FFT::exec() {
MatrixWorkspace_sptr outWS = MatrixWorkspace_sptr outWS =
WorkspaceFactory::Instance().create(inWS, nOut, xSize, ySize); WorkspaceFactory::Instance().create(inWS, nOut, xSize, ySize);
bool isEnergyMeV = false; double df = 1.0 / (dx * ySize);
if (inWS->getAxis(0)->unit() &&
(inWS->getAxis(0)->unit()->caption() == "Energy" || // Output label
inWS->getAxis(0)->unit()->caption() == "Energy transfer") && outWS->getAxis(0)->unit() = UnitFactory::Instance().create("Label");
inWS->getAxis(0)->unit()->label() == "meV") {
auto inputUnit = inWS->getAxis(0)->unit();
if (inputUnit) {
boost::shared_ptr<Kernel::Units::Label> lblUnit = boost::shared_ptr<Kernel::Units::Label> lblUnit =
boost::dynamic_pointer_cast<Kernel::Units::Label>( boost::dynamic_pointer_cast<Kernel::Units::Label>(
UnitFactory::Instance().create("Label")); UnitFactory::Instance().create("Label"));
if (lblUnit) { if (lblUnit) {
lblUnit->setLabel("Time", "ns");
if ((inputUnit->caption() == "Energy" ||
inputUnit->caption() == "Energy transfer") &&
inputUnit->label() == "meV") {
lblUnit->setLabel("Time", "ns");
df /= 2.418e2;
} else if (inputUnit->caption() == "Time" && inputUnit->label() == "s") {
lblUnit->setLabel("Frequency", "Hz");
} else if (inputUnit->caption() == "Frequency" && inputUnit->label() == "Hz") {
lblUnit->setLabel("Time", "s");
} else if (inputUnit->caption() == "Time" && inputUnit->label() == "microsecond") {
lblUnit->setLabel("Frequency", "MHz");
} else if (inputUnit->caption() == "Frequency" && inputUnit->label() == "MHz") {
lblUnit->setLabel("Time", Units::Symbol::Microsecond);
} else if (inputUnit->caption() == "d-Spacing" && inputUnit->label() == "Angstrom") {
lblUnit->setLabel("q", Units::Symbol::InverseAngstrom);
} else if (inputUnit->caption() == "q" && inputUnit->label() == "Angstrom^-1") {
lblUnit->setLabel("d-Spacing", Units::Symbol::Angstrom);
}
outWS->getAxis(0)->unit() = lblUnit; outWS->getAxis(0)->unit() = lblUnit;
} }
isEnergyMeV = true; }
} else
outWS->getAxis(0)->unit() = UnitFactory::Instance().create("Label");
double df = 1.0 / (dx * ySize);
if (isEnergyMeV)
df /= 2.418e2;
// centerShift == true means that the zero on the x axis is assumed to be in // centerShift == true means that the zero on the x axis is assumed to be in
// the data centre // the data centre
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "MantidAPI/FrameworkManager.h" #include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/MatrixWorkspace.h" #include "MantidAPI/MatrixWorkspace.h"
#include "MantidDataObjects/Workspace2D.h" #include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/UnitFactory.h"
using namespace Mantid; using namespace Mantid;
using namespace Mantid::API; using namespace Mantid::API;
...@@ -382,6 +383,10 @@ public: ...@@ -382,6 +383,10 @@ public:
MatrixWorkspace_sptr fWS = boost::dynamic_pointer_cast<MatrixWorkspace> MatrixWorkspace_sptr fWS = boost::dynamic_pointer_cast<MatrixWorkspace>
(AnalysisDataService::Instance().retrieve("FFT_out")); (AnalysisDataService::Instance().retrieve("FFT_out"));
// Test the output label
TS_ASSERT_EQUALS(fWS->getAxis(0)->unit()->caption(), "Quantity");
TS_ASSERT_EQUALS(fWS->getAxis(0)->unit()->label(), "");
const MantidVec& X = fWS->readX(0); const MantidVec& X = fWS->readX(0);
const MantidVec& Yr = fWS->readY(0); const MantidVec& Yr = fWS->readY(0);
const MantidVec& Yi = fWS->readY(1); const MantidVec& Yi = fWS->readY(1);
...@@ -408,6 +413,49 @@ public: ...@@ -408,6 +413,49 @@ public:
FrameworkManager::Instance().deleteWorkspace("FFT_out"); FrameworkManager::Instance().deleteWorkspace("FFT_out");
} }
void testUnitsEnergy() {
const int N = 100;
const double XX = dX * N;
double dx = 1/(XX);
MatrixWorkspace_sptr WS = createWS(N,1,"even_hist");
// Set a label
WS->getAxis(0)->unit() = Mantid::Kernel::UnitFactory::Instance().create("Energy");
IAlgorithm* fft = Mantid::API::FrameworkManager::Instance().createAlgorithm("FFT");
fft->initialize();
fft->setPropertyValue("InputWorkspace","FFT_WS_even_hist");
fft->setPropertyValue("OutputWorkspace","FFT_out");
fft->setPropertyValue("Real","0");
fft->execute();
MatrixWorkspace_sptr fWS = boost::dynamic_pointer_cast<MatrixWorkspace>
(AnalysisDataService::Instance().retrieve("FFT_out"));
// Test X values
// When the input unit is 'Energy' in 'meV'
// there is a factor of 1/2.418e2 in X
const MantidVec& X = fWS->readX(0);
const MantidVec::const_iterator it = std::find(X.begin(),X.end(),0.);
int i0 = static_cast<int>(it - X.begin());
for (int i = 0; i < N / 4; i++) {
int j = i0 + i;
double x = X[j];
TS_ASSERT_DELTA(x, dx * i /2.418e2, 0.00001);
}
// Test the output label
TS_ASSERT_EQUALS(fWS->getAxis(0)->unit()->caption(),"Time");
TS_ASSERT_EQUALS(fWS->getAxis(0)->unit()->label(),"ns");
FrameworkManager::Instance().deleteWorkspace("FFT_WS_even_hist");
FrameworkManager::Instance().deleteWorkspace("FFT_out");
}
private: private:
MatrixWorkspace_sptr createWS(int n,int dn,const std::string& name) MatrixWorkspace_sptr createWS(int n,int dn,const std::string& name)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment