Commit 170e4f0f authored by Federico Montesino Pouzols's avatar Federico Montesino Pouzols
Browse files

Merge pull request #15894 from mantidproject/15888_SaveAscii_fails_with_non_spectrum_axis

SaveAscii2 fails with non spectrum axis
parents 888d5dba 77912def
......@@ -78,6 +78,9 @@ private:
void populateSpectrumNumberMetaData();
void populateAngleMetaData();
void populateAllMetaData();
bool
findElementInUnorderedStringVector(const std::vector<std::string> &vector,
const std::string &toFind);
/// Map the separator options to their string equivalents
std::map<std::string, std::string> m_separatorIndex;
......
......@@ -123,7 +123,6 @@ void SaveAscii2::init() {
void SaveAscii2::exec() {
// Get the workspace
m_ws = getProperty("InputWorkspace");
m_specToIndexMap = m_ws->getSpectrumToWorkspaceIndexMap();
int nSpectra = static_cast<int>(m_ws->getNumberHistograms());
m_nBins = static_cast<int>(m_ws->blocksize());
m_isHistogram = m_ws->isHistogramData();
......@@ -132,15 +131,21 @@ void SaveAscii2::exec() {
std::string metaDataString = getPropertyValue("SpectrumMetaData");
if (metaDataString.size() != 0) {
m_metaData = stringListToVector(metaDataString);
}
if (m_writeID) {
auto containsSpectrumNumber = false;
for (auto iter = m_metaData.begin(); iter != m_metaData.end(); ++iter) {
const auto metaDataType = *iter;
if (metaDataType.compare("spectrumnumber") == 0) {
containsSpectrumNumber = true;
auto containsSpectrumNumber =
findElementInUnorderedStringVector(m_metaData, "spectrumnumber");
if (containsSpectrumNumber) {
try {
m_specToIndexMap = m_ws->getSpectrumToWorkspaceIndexMap();
} catch (std::runtime_error) {
throw std::runtime_error("SpectrumNumber is present in "
"SpectrumMetaData but the workspace does not "
"have a SpectrumAxis.");
}
}
}
if (m_writeID) {
auto containsSpectrumNumber =
findElementInUnorderedStringVector(m_metaData, "spectrumnumber");
if (!containsSpectrumNumber) {
auto firstIter = m_metaData.begin();
m_metaData.insert(firstIter, "spectrumnumber");
......@@ -481,5 +486,17 @@ void SaveAscii2::populateAllMetaData() {
}
}
bool SaveAscii2::findElementInUnorderedStringVector(
const std::vector<std::string> &vector, const std::string &toFind) {
auto containsElement = false;
for (auto iter = vector.begin(); iter != vector.end(); ++iter) {
const auto vectorElement = *iter;
if (vectorElement.compare(toFind) == 0) {
containsElement = true;
}
}
return containsElement;
}
} // namespace DataHandling
} // namespace Mantid
......@@ -4,7 +4,9 @@
#include <cxxtest/TestSuite.h>
#include "MantidDataHandling/SaveAscii2.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/TextAxis.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include <fstream>
......@@ -260,6 +262,41 @@ public:
AnalysisDataService::Instance().remove(m_name);
}
void test_non_spectrum_axisworkspace() {
Mantid::DataObjects::Workspace2D_sptr wsToSave;
writeSampleWS(wsToSave, false);
SaveAscii2 save;
std::string filename = initSaveAscii2(save);
TS_ASSERT_THROWS_NOTHING(save.setProperty("WriteSpectrumID", true));
TS_ASSERT_THROWS_NOTHING(save.execute());
// has the algorithm written a file to disk?
TS_ASSERT(Poco::File(filename).exists());
// Now make some checks on the content of the file
std::ifstream in(filename.c_str());
int specID;
std::string header1, header2, header3, separator, comment;
// Test that the first few column headers, separator and first two bins are
// as expected
in >> comment >> header1 >> separator >> header2 >> separator >> header3 >>
specID;
TS_ASSERT_EQUALS(comment, "#");
TS_ASSERT_EQUALS(separator, ",");
TS_ASSERT_EQUALS(header1, "X");
TS_ASSERT_EQUALS(header2, "Y");
TS_ASSERT_EQUALS(header3, "E");
TS_ASSERT_EQUALS(specID, 1);
in.close();
Poco::File(filename).remove();
AnalysisDataService::Instance().remove(m_name);
}
void testExec_no_header() {
Mantid::DataObjects::Workspace2D_sptr wsToSave;
writeSampleWS(wsToSave);
......@@ -423,6 +460,20 @@ public:
AnalysisDataService::Instance().remove(m_name);
}
void test_fail_spectrum_number_in_meta_data_for_non_spectrum_axis_ws() {
Mantid::DataObjects::Workspace2D_sptr wsToSave;
writeSampleWS(wsToSave, false);
SaveAscii2 save;
std::string filename = initSaveAscii2(save);
TS_ASSERT_THROWS_NOTHING(
save.setProperty("SpectrumMetaData", "SpectrumNumber"));
TS_ASSERT_THROWS_ANYTHING(save.execute());
AnalysisDataService::Instance().remove(m_name);
}
void test_fail_invalid_IndexMin_Max_Overlap() {
Mantid::DataObjects::Workspace2D_sptr wsToSave;
writeSampleWS(wsToSave);
......@@ -687,7 +738,8 @@ public:
}
private:
void writeSampleWS(Mantid::DataObjects::Workspace2D_sptr &wsToSave) {
void writeSampleWS(Mantid::DataObjects::Workspace2D_sptr &wsToSave,
const bool &isSpectra = true) {
wsToSave = boost::dynamic_pointer_cast<Mantid::DataObjects::Workspace2D>(
WorkspaceFactory::Instance().create("Workspace2D", 2, 3, 3));
for (int i = 0; i < 2; i++) {
......@@ -701,6 +753,13 @@ private:
}
}
if (!isSpectra) {
auto textAxis = new TextAxis(2);
textAxis->setLabel(0, "Test Axis 1");
textAxis->setLabel(1, "Test Axis 2");
wsToSave->replaceAxis(1, textAxis);
}
AnalysisDataService::Instance().add(m_name, wsToSave);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment