diff --git a/Code/Mantid/Framework/Algorithms/src/FFT.cpp b/Code/Mantid/Framework/Algorithms/src/FFT.cpp index 7b387eae0a38f0607550248450b439e5d406c612..4b1dbebdd4a2fdeb29365824fa71fd38eb8e187c 100644 --- a/Code/Mantid/Framework/Algorithms/src/FFT.cpp +++ b/Code/Mantid/Framework/Algorithms/src/FFT.cpp @@ -3,6 +3,7 @@ //---------------------------------------------------------------------- #include "MantidAlgorithms/FFT.h" #include "MantidKernel/UnitFactory.h" +#include "MantidKernel/UnitLabelTypes.h" #include "MantidAPI/TextAxis.h" #include <boost/shared_array.hpp> @@ -127,25 +128,41 @@ void FFT::exec() { MatrixWorkspace_sptr outWS = WorkspaceFactory::Instance().create(inWS, nOut, xSize, ySize); - bool isEnergyMeV = false; - if (inWS->getAxis(0)->unit() && - (inWS->getAxis(0)->unit()->caption() == "Energy" || - inWS->getAxis(0)->unit()->caption() == "Energy transfer") && - inWS->getAxis(0)->unit()->label() == "meV") { + double df = 1.0 / (dx * ySize); + + // Output label + outWS->getAxis(0)->unit() = UnitFactory::Instance().create("Label"); + + auto inputUnit = inWS->getAxis(0)->unit(); + if (inputUnit) { + boost::shared_ptr<Kernel::Units::Label> lblUnit = boost::dynamic_pointer_cast<Kernel::Units::Label>( UnitFactory::Instance().create("Label")); 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; } - 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 // the data centre diff --git a/Code/Mantid/Framework/Algorithms/test/FFTTest.h b/Code/Mantid/Framework/Algorithms/test/FFTTest.h index 977333897908dae913f4b3802ec872f4b4e05c12..23a600fce244a47b916692730f8e02b07283ac24 100644 --- a/Code/Mantid/Framework/Algorithms/test/FFTTest.h +++ b/Code/Mantid/Framework/Algorithms/test/FFTTest.h @@ -9,6 +9,7 @@ #include "MantidAPI/FrameworkManager.h" #include "MantidAPI/MatrixWorkspace.h" #include "MantidDataObjects/Workspace2D.h" +#include "MantidKernel/UnitFactory.h" using namespace Mantid; using namespace Mantid::API; @@ -382,6 +383,10 @@ public: MatrixWorkspace_sptr fWS = boost::dynamic_pointer_cast<MatrixWorkspace> (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& Yr = fWS->readY(0); const MantidVec& Yi = fWS->readY(1); @@ -408,6 +413,49 @@ public: 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: MatrixWorkspace_sptr createWS(int n,int dn,const std::string& name)