From ec89afa1e44d8776c8bbc653af354b74221447ea Mon Sep 17 00:00:00 2001 From: Keith Brown Date: Wed, 19 Feb 2014 08:56:08 +0000 Subject: [PATCH 01/11] Refs #8960 Edited cmake list for new files Cmake list now has entries for the new files in this ticket --- Code/Mantid/Framework/DataHandling/CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Code/Mantid/Framework/DataHandling/CMakeLists.txt b/Code/Mantid/Framework/DataHandling/CMakeLists.txt index b8cd20a8dbaf..8a00699def96 100644 --- a/Code/Mantid/Framework/DataHandling/CMakeLists.txt +++ b/Code/Mantid/Framework/DataHandling/CMakeLists.txt @@ -111,6 +111,7 @@ set ( SRC_FILES src/SaveFullprofResolution.cpp src/SaveGSS.cpp src/SaveISISNexus.cpp + src/SaveILLCosmosAscii.cpp src/SaveIsawDetCal.cpp src/SaveMask.cpp src/SaveNISTDAT.cpp @@ -236,6 +237,7 @@ set ( INC_FILES inc/MantidDataHandling/SaveFullprofResolution.h inc/MantidDataHandling/SaveGSS.h inc/MantidDataHandling/SaveISISNexus.h + inc/MantidDataHandling/SaveILLCosmosAscii.h inc/MantidDataHandling/SaveIsawDetCal.h inc/MantidDataHandling/SaveMask.h inc/MantidDataHandling/SaveNISTDAT.h @@ -356,6 +358,7 @@ set ( TEST_FILES SaveDspacemapTest.h SaveFocussedXYETest.h SaveFullprofResolutionTest.h + SaveILLCosmosAsciiTest.h SaveIsawDetCalTest.h SaveMaskTest.h SaveNISTDATTest.h From e3bd9a8f121c06273fe69dfb1e674e3cd60c1ce2 Mon Sep 17 00:00:00 2001 From: Keith Brown Date: Wed, 19 Feb 2014 08:57:50 +0000 Subject: [PATCH 02/11] Refs #8960 Test skeleton made The test will be pretty much the same as SaveAnstoAscii's so I've copied it over for now ready for modifications to be made. --- .../test/SaveILLCosmosAsciiTest.h | 222 ++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 Code/Mantid/Framework/DataHandling/test/SaveILLCosmosAsciiTest.h diff --git a/Code/Mantid/Framework/DataHandling/test/SaveILLCosmosAsciiTest.h b/Code/Mantid/Framework/DataHandling/test/SaveILLCosmosAsciiTest.h new file mode 100644 index 000000000000..6db9ba1565dc --- /dev/null +++ b/Code/Mantid/Framework/DataHandling/test/SaveILLCosmosAsciiTest.h @@ -0,0 +1,222 @@ +#ifndef SAVEILLCOSMOSASCIITEST_H_ +#define SAVEILLCOSMOSASCIITEST_H_ + +#include +#include "MantidDataHandling/SaveILLCosmosAscii.h" +#include "MantidDataObjects/Workspace2D.h" +#include "MantidAPI/AlgorithmManager.h" +#include "MantidTestHelpers/WorkspaceCreationHelper.h" +#include +#include +#include + +using namespace Mantid::API; +using namespace Mantid::DataHandling; +using namespace Mantid::DataObjects; + +class SaveILLCosmosAsciiTest : public CxxTest::TestSuite +{ + +public: + + static SaveILLCosmosAsciiTest *createSuite() { return new SaveILLCosmosAsciiTest(); } + static void destroySuite(SaveILLCosmosAsciiTest *suite) { delete suite; } + + SaveILLCosmosAsciiTest() + { + m_filename = "SaveILLCosmosAsciiTestFile.txt"; + m_name = "SaveILLCosmosAsciiWS"; + for (int i = 1; i < 11; ++i) + { + //X, Y and E get [1,2,3,4,5,6,7,8,9,10] + //0 gets [0,0,0,0,0,0,0,0,0,0] and is used to make sure there is no problem with divide by zero + m_dataX.push_back(i); + m_dataY.push_back(i); + m_dataE.push_back(i); + m_data0.push_back(0); + } + } + ~SaveILLCosmosAsciiTest() + { + } + + void testExec() + { + //create a new workspace and then delete it later on + createWS(); + + Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveILLCosmosAscii"); + alg->setPropertyValue("InputWorkspace", m_name); + alg->setPropertyValue("Filename", m_filename); + TS_ASSERT_THROWS_NOTHING(alg->execute()); + + if ( ! alg->isExecuted() ) + { + TS_FAIL("Could not run SaveILLCosmosAscii"); + } + m_long_filename= alg->getPropertyValue("Filename"); + // has the algorithm written a file to disk? + TS_ASSERT( Poco::File(m_long_filename).exists() ); + std::ifstream in(m_long_filename.c_str()); + std::string fullline; + getline(in,fullline); + std::vector columns; + boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on); + TS_ASSERT_EQUALS(columns.size(),4); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(0)), 1.5, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(1)), 1, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(2)), 1, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(3)), 0.6, 0.01); + in.close(); + + cleanupafterwards(); + } + void testNoX() + { + //create a new workspace and then delete it later on + createWS(true); + + Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveILLCosmosAscii"); + alg->setPropertyValue("InputWorkspace", m_name); + alg->setPropertyValue("Filename", m_filename); + TS_ASSERT_THROWS_NOTHING(alg->execute()); + + if ( ! alg->isExecuted() ) + { + TS_FAIL("Could not run SaveILLCosmosAscii"); + } + m_long_filename= alg->getPropertyValue("Filename"); + // has the algorithm written a file to disk? + TS_ASSERT( Poco::File(m_long_filename).exists() ); + std::ifstream in(m_long_filename.c_str()); + std::string fullline; + getline(in,fullline); + std::vector columns; + boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on); + TS_ASSERT_EQUALS(columns.size(),4); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(0)), 0, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(1)), 1, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(2)), 1, 0.01); + TS_ASSERT((columns.at(3) == "nan") || (columns.at(3) == "inf")); + in.close(); + + cleanupafterwards(); + } + void testNoY() + { + //create a new workspace and then delete it later on + createWS(false,true); + + Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveILLCosmosAscii"); + alg->setPropertyValue("InputWorkspace", m_name); + alg->setPropertyValue("Filename", m_filename); + TS_ASSERT_THROWS_NOTHING(alg->execute()); + + if ( ! alg->isExecuted() ) + { + TS_FAIL("Could not run SaveILLCosmosAscii"); + } + m_long_filename= alg->getPropertyValue("Filename"); + // has the algorithm written a file to disk? + TS_ASSERT( Poco::File(m_long_filename).exists() ); + std::ifstream in(m_long_filename.c_str()); + std::string fullline; + getline(in,fullline); + std::vector columns; + boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on); + TS_ASSERT_EQUALS(columns.size(),4); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(0)), 1.5, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(1)), 0, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(2)), 1, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(3)), 0.6, 0.01); + in.close(); + + cleanupafterwards(); + } + void testNoE() + { + //create a new workspace and then delete it later on + createWS(false,false,true); + + Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveILLCosmosAscii"); + alg->setPropertyValue("InputWorkspace", m_name); + alg->setPropertyValue("Filename", m_filename); + TS_ASSERT_THROWS_NOTHING(alg->execute()); + + if ( ! alg->isExecuted() ) + { + TS_FAIL("Could not run SaveILLCosmosAscii"); + } + m_long_filename= alg->getPropertyValue("Filename"); + // has the algorithm written a file to disk? + TS_ASSERT( Poco::File(m_long_filename).exists() ); + std::ifstream in(m_long_filename.c_str()); + std::string fullline; + getline(in,fullline); + std::vector columns; + boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on); + TS_ASSERT_EQUALS(columns.size(),4); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(0)), 1.5, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(1)), 1, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(2)), 0, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(3)), 0.6, 0.01); + in.close(); + + cleanupafterwards(); + } + + void test_fail_invalid_workspace() + { + Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveILLCosmosAscii"); + alg->setRethrows(true); + TS_ASSERT(alg->isInitialized()); + TS_ASSERT_THROWS_NOTHING(alg->setPropertyValue("Filename", m_filename)); + m_long_filename= alg->getPropertyValue("Filename"); //Get absolute path + TS_ASSERT_THROWS_ANYTHING(alg->setPropertyValue("InputWorkspace", "NotARealWS")); + TS_ASSERT_THROWS_ANYTHING(alg->execute()); + + // the algorithm shouldn't have written a file to disk + TS_ASSERT( !Poco::File(m_long_filename).exists() ); + } +private: + void createWS(bool zeroX = false, bool zeroY = false, bool zeroE = false) + { + MatrixWorkspace_sptr ws = WorkspaceCreationHelper::Create2DWorkspace(1,10); + AnalysisDataService::Instance().addOrReplace(m_name, ws); + //Check if any of X, Y or E should be zeroed to check for divide by zero or similiar + if (zeroX) + { + ws->dataX(0) = m_data0; + } + else + { + ws->dataX(0) = m_dataX; + } + + if (zeroY) + { + ws->dataY(0) = m_data0; + } + else + { + ws->dataY(0) = m_dataY; + } + + if (zeroE) + { + ws->dataE(0) = m_data0; + } + else + { + ws->dataE(0) = m_dataE; + } + } + void cleanupafterwards() + { + Poco::File(m_long_filename).remove(); + AnalysisDataService::Instance().remove(m_name); + } + std::string m_filename, m_name, m_long_filename; + std::vector m_dataX, m_dataY, m_dataE, m_data0; +}; +#endif /*SAVEILLCOSMOSASCIITEST_H_*/ From f68ed9fe2a3617677f3af01ca8b28b74beb9ede1 Mon Sep 17 00:00:00 2001 From: Keith Brown Date: Wed, 19 Feb 2014 08:58:44 +0000 Subject: [PATCH 03/11] Refs #8960 Algorithm coming along Algorithm is nearly there, only a few tweaks and cleanup to go --- .../MantidDataHandling/SaveILLCosmosAscii.h | 93 +++++++++ .../DataHandling/src/SaveILLCosmosAscii.cpp | 188 ++++++++++++++++++ 2 files changed, 281 insertions(+) create mode 100644 Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h create mode 100644 Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h new file mode 100644 index 000000000000..d66749001997 --- /dev/null +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h @@ -0,0 +1,93 @@ +#ifndef MANTID_DATAHANDLING_SaveILLCosmosAscii_H_ +#define MANTID_DATAHANDLING_SaveILLCosmosAscii_H_ + +//---------------------------------------------------------------------- +// Includes +//---------------------------------------------------------------------- +#include "MantidAPI/Algorithm.h" +#include "MantidAPI/MatrixWorkspace.h" + +namespace Mantid +{ + namespace DataHandling + { + /** @class SaveILLCosmosAscii SaveILLCosmosAscii.h DataHandling/SaveILLCosmosAscii.h + + Saves a workspace or selected spectra in a coma-separated ascii file. Spectra are saved in columns. + Properties: +
    +
  • Filename - the name of the file to write to.
  • +
  • Workspace - the workspace name to be saved.
  • +
  • SpectrumMin - the starting spectrum index to save (optional)
  • +
  • SpectrumMax - the ending spectrum index to save (optional)
  • +
  • SpectrumList - a list of comma-separated spectra indeces to save (optional)
  • +
  • Precision - the numeric precision - the number of significant digits for the saved data (optional)
  • +
+ + + @author Keith Brown, ISIS, Placement student from the University of Derby + @date 10/10/13 + + Copyright © 2007-9 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory + + This file is part of Mantid. + + Mantid is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + Mantid is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + File change history is stored at: . + Code Documentation is available at: + */ + class DLLExport SaveILLCosmosAscii : public API::Algorithm + { + public: + /// Default constructor + SaveILLCosmosAscii(); + /// Destructor + ~SaveILLCosmosAscii() {} + /// Algorithm's name for identification overriding a virtual method + virtual const std::string name() const { return "SaveILLCosmosAscii"; } + /// Algorithm's version for identification overriding a virtual method + virtual int version() const { return 1; } + /// Algorithm's category for identification overriding a virtual method + virtual const std::string category() const { return "DataHandling\\Text"; } + + private: + /// Sets documentation strings for this algorithm + virtual void initDocs(); + /// Overwrites Algorithm method. + void init(); + /// Overwrites Algorithm method + void exec(); + /// returns true if the value is NaN + bool checkIfNan(const double& value) const; + /// returns true if the value if + or - infinity + bool checkIfInfinite(const double& value) const; + /// print the appropriate value to file + void outputval (double val, std::ofstream & file, bool leadingSep = true); + ///static reference to the logger class + static Kernel::Logger& g_log; + + /// Map the separator options to their string equivalents + std::map m_separatorIndex; + + int m_nBins; + char m_sep; + bool m_writeDX; + bool m_isHistogram; + API::MatrixWorkspace_const_sptr m_ws; + }; + } // namespace DataHandling +} // namespace Mantid + +#endif /* MANTID_DATAHANDLING_SaveILLCosmosAscii_H_ */ diff --git a/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp b/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp new file mode 100644 index 000000000000..dda8c67897e2 --- /dev/null +++ b/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp @@ -0,0 +1,188 @@ +/*WIKI* + +The workspace data are stored in the file in columns: the first column contains the X-values, followed by pairs of Y and E values. Columns are separated by commas. The resulting file can normally be loaded into a workspace by the [[LoadAscii2]] algorithm. + +==== Limitations ==== +The algorithm assumes that the workspace has common X values for all spectra (i.e. is not a [[Ragged Workspace|ragged workspace]]). Only the X values from the first spectrum in the workspace are saved out. + +*WIKI*/ +//---------------------------------------------------------------------- +// Includes +//---------------------------------------------------------------------- +#include "MantidDataHandling/SaveILLCosmosAscii.h" +#include "MantidKernel/UnitFactory.h" +#include "MantidKernel/ArrayProperty.h" +#include "MantidAPI/FileProperty.h" +#include "MantidKernel/BoundedValidator.h" +#include "MantidKernel/VisibleWhenProperty.h" +#include "MantidKernel/ListValidator.h" +#include +#include +#include +#include + +namespace Mantid +{ + namespace DataHandling + { + // Register the algorithm into the algorithm factory + DECLARE_ALGORITHM(SaveILLCosmosAscii) + + /// Sets documentation strings for this algorithm + void SaveILLCosmosAscii::initDocs() + { + this->setWikiSummary("Saves a 2D [[workspace]] to a comma separated ascii file. "); + this->setOptionalMessage("Saves a 2D workspace to a ascii file."); + } + + using namespace Kernel; + using namespace API; + + // Initialise the logger + Logger& SaveILLCosmosAscii::g_log = Logger::get("SaveILLCosmosAscii"); + + /// Empty constructor + SaveILLCosmosAscii::SaveILLCosmosAscii() : m_separatorIndex() + { + } + + /// Initialisation method. + void SaveILLCosmosAscii::init() + { + declareProperty(new WorkspaceProperty<>("InputWorkspace", + "",Direction::Input), "The name of the workspace containing the data you want to save to a ANSTO file."); + std::vector exts; + exts.push_back(".mft"); + declareProperty(new FileProperty("Filename", "", FileProperty::Save, exts), + "The filename of the output ANSTO file."); + declareProperty(new ArrayProperty("LogList"),"List of logs to write to file."); + declareProperty("Usercontact", "", "Text to be written to the User-local contact field"); + m_sep = '\t'; + } + + /** + * Executes the algorithm. + */ + void SaveILLCosmosAscii::exec() + { + std::string filename = getProperty("Filename"); + std::ofstream file(filename.c_str()); + if (!file) + { + g_log.error("Unable to create file: " + filename); + throw Exception::FileError("Unable to create file: " , filename); + } + m_ws = getProperty("InputWorkspace"); + g_log.information("FILENAME: " + filename); + + auto title ='#'+ m_ws->getTitle(); + const std::vector & x1 = m_ws->readX(0); + const size_t xlength = x1.size() - 1; + std::vector X1(xlength, 0); + for (size_t i = 0; i < xlength; ++i) + { + X1[i]=(x1[i]+x1[i+1])/2.0; + } + const std::vector & y1 = m_ws->readY(0); + const std::vector & e1 = m_ws->readE(0); + double qres = (X1[1]-X1[0])/X1[1]; + + g_log.information("Constant dq/q from file: " + boost::lexical_cast(qres)); + + file << std::scientific; + file << "MFT" << std::endl; + file << "Instrument: "<< m_ws->getInstrument()->getName() << std::endl; + file << "User-local contact: " << std::endl; //add optional porperty + file << "Title: "<< m_ws->getTitle() << std::endl; + auto samp = m_ws->run(); + file << "Subtitle: " << samp.getLogData("run_title")->value() << std::endl; + file << "Start date + time: " << samp.getLogData("run_start")->value() << std::endl; + file << "End date + time: " << samp.getLogData("run_end")->value() << std::endl; + + const std::vector logList = getProperty("LogList"); + ///logs + for (auto log = logList.begin(); log != logList.end(); ++log) + { + file << boost::lexical_cast(*log) << ": " << boost::lexical_cast(samp.getLogData(*log)->value()) << std::endl; + } + + file << "Number of file format: 2" << std::endl; + file << "Number of data points:" << m_sep << xlength << std::endl; + file << std::endl; + + file << m_sep << "q" << m_sep << "refl" << m_sep << "refl_err" << m_sep << "q_res" << std::endl; + + for (size_t i = 0; i < xlength; ++i) + { + double dq = X1[i]*qres; + outputval(X1[i], file); + outputval(y1[i], file); + outputval(e1[i], file); + outputval(dq, file); + file << std::endl; + } + file.close(); + } + + void SaveILLCosmosAscii::outputval (double val, std::ofstream & file, bool leadingSep) + { + bool nancheck = checkIfNan(val); + bool infcheck = checkIfInfinite(val); + if (leadingSep) + { + if (!nancheck && !infcheck) + { + file << m_sep << val; + } + else if (nancheck) + { + //not a number - output nan + file << m_sep << "nan"; + } + else if (infcheck) + { + //infinite - output 'inf' + file << m_sep << "inf"; + } + else + { + //not valid, nan or inf - so output 'und' + file << m_sep << "und"; + } + } + else + { + if (!nancheck && !infcheck) + { + file << val; + } + else if (nancheck) + { + //not a number - output nan + file << "nan"; + } + else if (infcheck) + { + //infinite - output 'inf' + file << "inf"; + } + else + { + //not valid, nan or inf - so output 'und' + file << "und"; + } + } + } + + bool SaveILLCosmosAscii::checkIfNan(const double& value) const + { + return (boost::math::isnan(value)); + } + + bool SaveILLCosmosAscii::checkIfInfinite(const double& value) const + { + return (std::abs(value) == std::numeric_limits::infinity()); + } + + } // namespace DataHandling +} // namespace Mantid From 66f56598fbd5dd95056405b375eac497a1046b78 Mon Sep 17 00:00:00 2001 From: Keith Brown Date: Fri, 14 Mar 2014 10:07:36 +0000 Subject: [PATCH 04/11] Refs #8960 ILL Cosmos and ANSTO now inherit from a new base class AsciiPointBase is a new class that contains the base functionality for the ILL Cosmos and ANSTO save routines. It holds the data writing, value checking and property declaring functionality and if any extra functionality is required theres a couple of spots to add it as overridden methods. Unused headers left over have been removed --- .../Framework/DataHandling/CMakeLists.txt | 2 + .../inc/MantidDataHandling/AsciiPointBase.h | 73 +++++++++ .../inc/MantidDataHandling/SaveANSTOAscii.h | 27 ++- .../MantidDataHandling/SaveILLCosmosAscii.h | 32 +--- .../DataHandling/src/AsciiPointBase.cpp | 151 +++++++++++++++++ .../DataHandling/src/SaveANSTOAscii.cpp | 142 +--------------- .../DataHandling/src/SaveILLCosmosAscii.cpp | 155 ++---------------- 7 files changed, 270 insertions(+), 312 deletions(-) create mode 100644 Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h create mode 100644 Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp diff --git a/Code/Mantid/Framework/DataHandling/CMakeLists.txt b/Code/Mantid/Framework/DataHandling/CMakeLists.txt index 3d583adaa8cf..aee795fc8153 100644 --- a/Code/Mantid/Framework/DataHandling/CMakeLists.txt +++ b/Code/Mantid/Framework/DataHandling/CMakeLists.txt @@ -1,5 +1,6 @@ set ( SRC_FILES src/AppendGeometryToSNSNexus.cpp + src/AsciiPointBase.cpp src/CompressEvents.cpp src/CreateChopperModel.cpp src/CreateModeratorModel.cpp @@ -132,6 +133,7 @@ set ( SRC_FILES set ( INC_FILES inc/MantidDataHandling/AppendGeometryToSNSNexus.h + inc/MantidDataHandling/AsciiPointBase.h inc/MantidDataHandling/CompressEvents.h inc/MantidDataHandling/CreateChopperModel.h inc/MantidDataHandling/CreateModeratorModel.h diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h new file mode 100644 index 000000000000..2ec7295af14f --- /dev/null +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h @@ -0,0 +1,73 @@ +#ifndef MANTID_DATAHANDLING_ASCIIPOINTBASE_H_ +#define MANTID_DATAHANDLING_ASCIIPOINTBASE_H_ + +//---------------------------------------------------------------------- +// Includes +//---------------------------------------------------------------------- +#include "MantidAPI/Algorithm.h" + +namespace Mantid +{ + namespace DataHandling + { + class DLLExport AsciiPointBase : public API::Algorithm + { + public: + /// Default constructor + AsciiPointBase() {} + /// Destructor + ~AsciiPointBase() {} + /// Algorithm's name for identification overriding a virtual method + virtual const std::string name() const = 0; + /// Algorithm's version for identification overriding a virtual method + virtual int version() const = 0; + /// Algorithm's category for identification overriding a virtual method + virtual const std::string category() const { return "DataHandling\\Text"; } + /* + /// Algorithm's name for identification overriding a virtual method + virtual const std::string name() const { return "AsciiPointBase"; } + /// Algorithm's version for identification overriding a virtual method + virtual int version() const { return 1; } + /// Algorithm's category for identification overriding a virtual method + virtual const std::string category() const { return "DataHandling\\Text"; } + */ + private: + /// Sets documentation strings for this algorithm + virtual void initDocs() = 0; + /// Return the file extension this algorthm should output. + virtual std::string ext() = 0; + /// Return the separator character + virtual char sep() {return '\t';} + /// return if the line should start with a separator + virtual bool leadingSep() {return true;} + /// Add extra properties + virtual void extraProps() = 0; + /// write any extra information required + virtual void extraHeaders(std::ofstream & file) = 0; + /// write the main content of the data + virtual void data(std::ofstream & file, const std::vector & XData); + /// Overwrites Algorithm method. + void init(); + /// Overwrites Algorithm method + void exec(); + /// returns true if the value is NaN + bool checkIfNan(const double& value) const; + /// returns true if the value if + or - infinity + bool checkIfInfinite(const double& value) const; + /// print the appropriate value to file + void outputval (double val, std::ofstream & file, bool leadingSep = true); + /// write the top of the file + virtual std::vector header(std::ofstream & file); + ///static reference to the logger class + static Kernel::Logger& g_log; + protected: + char m_sep; + double m_qres; + size_t m_xlength; + API::MatrixWorkspace_const_sptr m_ws; + }; + + } // namespace DataHandling +} // namespace Mantid + +#endif /* MANTID_DATAHANDLING_SAVEANSTO_H_ */ diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveANSTOAscii.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveANSTOAscii.h index 0a6cebe4c705..78c31b142136 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveANSTOAscii.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveANSTOAscii.h @@ -5,42 +5,37 @@ // Includes //---------------------------------------------------------------------- #include "MantidAPI/Algorithm.h" +#include "MantidDataHandling/AsciiPointBase.h" namespace Mantid { namespace DataHandling { - class DLLExport SaveANSTOAscii : public API::Algorithm + class DLLExport SaveANSTOAscii : public DataHandling::AsciiPointBase { public: /// Default constructor - SaveANSTOAscii(); + SaveANSTOAscii() {} /// Destructor ~SaveANSTOAscii() {} /// Algorithm's name for identification overriding a virtual method virtual const std::string name() const { return "SaveANSTOAscii"; } /// Algorithm's version for identification overriding a virtual method virtual int version() const { return 1; } - /// Algorithm's category for identification overriding a virtual method - virtual const std::string category() const { return "DataHandling\\Text"; } private: /// Sets documentation strings for this algorithm virtual void initDocs(); - /// Overwrites Algorithm method. - void init(); - /// Overwrites Algorithm method - void exec(); - /// returns true if the value is NaN - bool checkIfNan(const double& value) const; - /// returns true if the value if + or - infinity - bool checkIfInfinite(const double& value) const; - /// print the appropriate value to file - void outputval (double val, std::ofstream & file, bool leadingSep = true); + /// Return the file extension this algorthm should output. + virtual std::string ext() {return ".txt";} + /// return if the line should start with a separator + virtual bool leadingSep() {return false;} + /// no extra properties required so override blank + virtual void extraProps() {} + /// no extra information required so override blank + virtual void extraHeaders(std::ofstream & file) {} ///static reference to the logger class static Kernel::Logger& g_log; - char m_sep; - API::MatrixWorkspace_const_sptr m_ws; }; } // namespace DataHandling diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h index d66749001997..768df3562d8b 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h @@ -6,6 +6,7 @@ //---------------------------------------------------------------------- #include "MantidAPI/Algorithm.h" #include "MantidAPI/MatrixWorkspace.h" +#include "MantidDataHandling/AsciiPointBase.h" namespace Mantid { @@ -48,44 +49,29 @@ namespace Mantid File change history is stored at: . Code Documentation is available at: */ - class DLLExport SaveILLCosmosAscii : public API::Algorithm + class DLLExport SaveILLCosmosAscii : public DataHandling::AsciiPointBase { public: /// Default constructor - SaveILLCosmosAscii(); + SaveILLCosmosAscii() {} /// Destructor ~SaveILLCosmosAscii() {} /// Algorithm's name for identification overriding a virtual method virtual const std::string name() const { return "SaveILLCosmosAscii"; } /// Algorithm's version for identification overriding a virtual method virtual int version() const { return 1; } - /// Algorithm's category for identification overriding a virtual method - virtual const std::string category() const { return "DataHandling\\Text"; } private: /// Sets documentation strings for this algorithm virtual void initDocs(); - /// Overwrites Algorithm method. - void init(); - /// Overwrites Algorithm method - void exec(); - /// returns true if the value is NaN - bool checkIfNan(const double& value) const; - /// returns true if the value if + or - infinity - bool checkIfInfinite(const double& value) const; - /// print the appropriate value to file - void outputval (double val, std::ofstream & file, bool leadingSep = true); + /// Return the file extension this algorthm should output. + virtual std::string ext() {return ".mft";} ///static reference to the logger class static Kernel::Logger& g_log; - - /// Map the separator options to their string equivalents - std::map m_separatorIndex; - - int m_nBins; - char m_sep; - bool m_writeDX; - bool m_isHistogram; - API::MatrixWorkspace_const_sptr m_ws; + ///extra properties specifically for this + virtual void extraProps(); + /// write any extra information required + virtual void extraHeaders(std::ofstream & file); }; } // namespace DataHandling } // namespace Mantid diff --git a/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp b/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp new file mode 100644 index 000000000000..723c0c02a82b --- /dev/null +++ b/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp @@ -0,0 +1,151 @@ +/* +AsciiPointBase is an abstract class holding the functionality for the SaveILLCosmosAscii and SaveANSTOAscii export-only Acii-based save formats. It is based on a python script by Maximilian Skoda, written for the ISIS Reflectometry GUI +*/ +//---------------------------------------------------------------------- +// Includes +//---------------------------------------------------------------------- +#include "MantidDataHandling/AsciiPointBase.h" +#include "MantidAPI/FileProperty.h" +#include +#include +#include +#include + +namespace Mantid +{ + namespace DataHandling + { + using namespace Kernel; + using namespace API; + + // Initialise the logger + Logger& AsciiPointBase::g_log = Logger::get("AsciiPointBase"); + + /// Initialisation method. + void AsciiPointBase::init() + { + declareProperty(new WorkspaceProperty<>("InputWorkspace", + "",Direction::Input), "The name of the workspace containing the data you want to save."); + + std::vector exts; + exts.push_back(ext()); + declareProperty(new FileProperty("Filename", "", FileProperty::Save, exts), "The filename of the output file."); + m_sep = sep(); + extraProps(); + } + + /** + * Executes the algorithm. + */ + void AsciiPointBase::exec() + { + std::string filename = getProperty("Filename"); + std::ofstream file(filename.c_str()); + if (!file) + { + g_log.error("Unable to create file: " + filename); + throw Exception::FileError("Unable to create file: " , filename); + } + m_ws = getProperty("InputWorkspace"); + g_log.information("FILENAME: " + filename); + + std::vector XData = header(file); + extraHeaders(file); + data(file, XData); + file.close(); + } + + std::vector AsciiPointBase::header(std::ofstream & file) + { + auto title ='#'+ m_ws->getTitle(); + const std::vector & xTemp = m_ws->readX(0); + m_xlength = xTemp.size() - 1; + std::vector XData(m_xlength, 0); + for (size_t i = 0; i < m_xlength; ++i) + { + XData[i]=(xTemp[i]+xTemp[i+1])/2.0; + } + + m_qres = (XData[1]-XData[0])/XData[1]; + g_log.information("Constant dq/q from file: " + boost::lexical_cast(m_qres)); + file << std::scientific; + return XData; + } + + void AsciiPointBase::data(std::ofstream & file, const std::vector & XData) + { + const std::vector & yData = m_ws->readY(0); + const std::vector & eData = m_ws->readE(0); + double dq; + for (size_t i = 0; i < m_xlength; ++i) + { + dq = XData[i]*m_qres; + outputval(XData[i], file, leadingSep()); + outputval(yData[i], file); + outputval(eData[i], file); + outputval(dq, file); + file << std::endl; + } + } + + void AsciiPointBase::outputval (double val, std::ofstream & file, bool leadingSep) + { + bool nancheck = checkIfNan(val); + bool infcheck = checkIfInfinite(val); + if (leadingSep) + { + if (!nancheck && !infcheck) + { + file << m_sep << val; + } + else if (nancheck) + { + //not a number - output nan + file << m_sep << "nan"; + } + else if (infcheck) + { + //infinite - output 'inf' + file << m_sep << "inf"; + } + else + { + //not valid, nan or inf - so output 'und' + file << m_sep << "und"; + } + } + else + { + if (!nancheck && !infcheck) + { + file << val; + } + else if (nancheck) + { + //not a number - output nan + file << "nan"; + } + else if (infcheck) + { + //infinite - output 'inf' + file << "inf"; + } + else + { + //not valid, nan or inf - so output 'und' + file << "und"; + } + } + } + + bool AsciiPointBase::checkIfNan(const double& value) const + { + return (boost::math::isnan(value)); + } + + bool AsciiPointBase::checkIfInfinite(const double& value) const + { + return (std::abs(value) == std::numeric_limits::infinity()); + } + } // namespace DataHandling +} // namespace Mantid diff --git a/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp b/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp index f22afbe4111a..9ae69fb91136 100644 --- a/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp +++ b/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp @@ -1,5 +1,5 @@ /*WIKI* -SaveANSTOAscii is an export-only Acii-based save format with no associated loader. It is based on a python script by Maximilian Skoda, written for the ISIS Reflectometry GUI +SaveANSTOAscii is an export-only Ascii-based save format with no associated loader. It is based on a python script by Maximilian Skoda, written for the ISIS Reflectometry GUI ==== Limitations ==== While Files saved with SaveANSTOAscii can be loaded back into mantid using LoadAscii, the resulting workspaces won't be usful as the data written by SaveANSTOAscii is not in the normal X,Y,E,DX format. *WIKI*/ @@ -7,17 +7,7 @@ While Files saved with SaveANSTOAscii can be loaded back into mantid using LoadA // Includes //---------------------------------------------------------------------- #include "MantidDataHandling/SaveANSTOAscii.h" -#include "MantidKernel/UnitFactory.h" -#include "MantidKernel/ArrayProperty.h" -#include "MantidAPI/FileProperty.h" -#include "MantidKernel/BoundedValidator.h" -#include "MantidKernel/VisibleWhenProperty.h" -#include "MantidKernel/ListValidator.h" -#include -#include -#include -#include -#include +#include "MantidDataHandling/AsciiPointBase.h" namespace Mantid { @@ -25,135 +15,17 @@ namespace Mantid { // Register the algorithm into the algorithm factory DECLARE_ALGORITHM(SaveANSTOAscii) - - /// Sets documentation strings for this algorithm - void SaveANSTOAscii::initDocs() - { - this->setWikiSummary("Saves a 2D [[workspace]] to a comma separated ascii file. "); - this->setOptionalMessage("Saves a 2D workspace to a ascii file."); - } - using namespace Kernel; using namespace API; // Initialise the logger Logger& SaveANSTOAscii::g_log = Logger::get("SaveANSTOAscii"); - - /// Empty constructor - SaveANSTOAscii::SaveANSTOAscii() - { - } - - /// Initialisation method. - void SaveANSTOAscii::init() - { - declareProperty(new WorkspaceProperty<>("InputWorkspace", - "",Direction::Input), "The name of the workspace containing the data you want to save to a ANSTO file."); - - std::vector exts; - exts.push_back(".txt"); - declareProperty(new FileProperty("Filename", "", FileProperty::Save, exts), - "The filename of the output ANSTO file."); - m_sep = '\t'; - } - - /** - * Executes the algorithm. - */ - void SaveANSTOAscii::exec() - { - std::string filename = getProperty("Filename"); - std::ofstream file(filename.c_str()); - if (!file) - { - g_log.error("Unable to create file: " + filename); - throw Exception::FileError("Unable to create file: " , filename); - } - m_ws = getProperty("InputWorkspace"); - g_log.information("FILENAME: " + filename); - auto title ='#'+ m_ws->getTitle(); - const std::vector & xTemp = m_ws->readX(0); - const size_t xlength = xTemp.size() - 1; - std::vector XData(xlength, 0); - for (size_t i = 0; i < xlength; ++i) - { - XData[i]=(xTemp[i]+xTemp[i+1])/2.0; - } - const std::vector & yData = m_ws->readY(0); - const std::vector & eData = m_ws->readE(0); - double qres = (XData[1]-XData[0])/XData[1]; - g_log.information("Constant dq/q from file: " + boost::lexical_cast(qres)); - file << std::scientific; - for (size_t i = 0; i < xlength; ++i) - { - double dq = XData[i]*qres; - outputval(XData[i], file, false); - outputval(yData[i], file); - outputval(eData[i], file); - outputval(dq, file); - file << std::endl; - } - file.close(); - } - - void SaveANSTOAscii::outputval (double val, std::ofstream & file, bool leadingSep) - { - bool nancheck = checkIfNan(val); - bool infcheck = checkIfInfinite(val); - if (leadingSep) - { - if (!nancheck && !infcheck) - { - file << m_sep << val; - } - else if (nancheck) - { - //not a number - output nan - file << m_sep << "nan"; - } - else if (infcheck) - { - //infinite - output 'inf' - file << m_sep << "inf"; - } - else - { - //not valid, nan or inf - so output 'und' - file << m_sep << "und"; - } - } - else - { - if (!nancheck && !infcheck) - { - file << val; - } - else if (nancheck) - { - //not a number - output nan - file << "nan"; - } - else if (infcheck) - { - //infinite - output 'inf' - file << "inf"; - } - else - { - //not valid, nan or inf - so output 'und' - file << "und"; - } - } - } - - bool SaveANSTOAscii::checkIfNan(const double& value) const - { - return (boost::math::isnan(value)); - } - - bool SaveANSTOAscii::checkIfInfinite(const double& value) const + + /// Sets documentation strings for this algorithm + void SaveANSTOAscii::initDocs() { - return (std::abs(value) == std::numeric_limits::infinity()); + this->setWikiSummary("Saves a 2D [[workspace]] to a tab separated ascii file. "); + this->setOptionalMessage("Saves a 2D workspace to a ascii file."); } } // namespace DataHandling } // namespace Mantid diff --git a/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp b/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp index dda8c67897e2..c3fcbcced0ca 100644 --- a/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp +++ b/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp @@ -1,25 +1,15 @@ /*WIKI* - -The workspace data are stored in the file in columns: the first column contains the X-values, followed by pairs of Y and E values. Columns are separated by commas. The resulting file can normally be loaded into a workspace by the [[LoadAscii2]] algorithm. - +SaveILLCosmosAscii is an export-only Ascii-based save format with no associated loader. It is based on a python script by Maximilian Skoda, written for the ISIS Reflectometry GUI ==== Limitations ==== -The algorithm assumes that the workspace has common X values for all spectra (i.e. is not a [[Ragged Workspace|ragged workspace]]). Only the X values from the first spectrum in the workspace are saved out. - +While Files saved with SaveILLCosmosAscii can be loaded back into mantid using LoadAscii, the resulting workspaces won't be usful as the data written by SaveILLCosmosAscii is not in the normal X,Y,E,DX format. *WIKI*/ //---------------------------------------------------------------------- // Includes //---------------------------------------------------------------------- #include "MantidDataHandling/SaveILLCosmosAscii.h" -#include "MantidKernel/UnitFactory.h" +#include "MantidDataHandling/AsciiPointBase.h" #include "MantidKernel/ArrayProperty.h" -#include "MantidAPI/FileProperty.h" -#include "MantidKernel/BoundedValidator.h" -#include "MantidKernel/VisibleWhenProperty.h" -#include "MantidKernel/ListValidator.h" -#include #include -#include -#include namespace Mantid { @@ -27,73 +17,34 @@ namespace Mantid { // Register the algorithm into the algorithm factory DECLARE_ALGORITHM(SaveILLCosmosAscii) - - /// Sets documentation strings for this algorithm - void SaveILLCosmosAscii::initDocs() - { - this->setWikiSummary("Saves a 2D [[workspace]] to a comma separated ascii file. "); - this->setOptionalMessage("Saves a 2D workspace to a ascii file."); - } - using namespace Kernel; using namespace API; // Initialise the logger Logger& SaveILLCosmosAscii::g_log = Logger::get("SaveILLCosmosAscii"); - /// Empty constructor - SaveILLCosmosAscii::SaveILLCosmosAscii() : m_separatorIndex() + /// Sets documentation strings for this algorithm + void SaveILLCosmosAscii::initDocs() { + this->setWikiSummary("Saves a 2D [[workspace]] to a tab separated ascii file with headers for extra information. "); + this->setOptionalMessage("Saves a 2D workspace to a ascii file."); } - - /// Initialisation method. - void SaveILLCosmosAscii::init() + + void SaveILLCosmosAscii::extraProps() { - declareProperty(new WorkspaceProperty<>("InputWorkspace", - "",Direction::Input), "The name of the workspace containing the data you want to save to a ANSTO file."); - std::vector exts; - exts.push_back(".mft"); - declareProperty(new FileProperty("Filename", "", FileProperty::Save, exts), - "The filename of the output ANSTO file."); declareProperty(new ArrayProperty("LogList"),"List of logs to write to file."); - declareProperty("Usercontact", "", "Text to be written to the User-local contact field"); - m_sep = '\t'; + declareProperty("UserContact", "", "Text to be written to the User-local contact field"); + declareProperty("Title", "", "Text to be written to the Title field"); } - /** - * Executes the algorithm. - */ - void SaveILLCosmosAscii::exec() + void SaveILLCosmosAscii::extraHeaders(std::ofstream & file) { - std::string filename = getProperty("Filename"); - std::ofstream file(filename.c_str()); - if (!file) - { - g_log.error("Unable to create file: " + filename); - throw Exception::FileError("Unable to create file: " , filename); - } - m_ws = getProperty("InputWorkspace"); - g_log.information("FILENAME: " + filename); - - auto title ='#'+ m_ws->getTitle(); - const std::vector & x1 = m_ws->readX(0); - const size_t xlength = x1.size() - 1; - std::vector X1(xlength, 0); - for (size_t i = 0; i < xlength; ++i) - { - X1[i]=(x1[i]+x1[i+1])/2.0; - } - const std::vector & y1 = m_ws->readY(0); - const std::vector & e1 = m_ws->readE(0); - double qres = (X1[1]-X1[0])/X1[1]; - - g_log.information("Constant dq/q from file: " + boost::lexical_cast(qres)); - - file << std::scientific; file << "MFT" << std::endl; file << "Instrument: "<< m_ws->getInstrument()->getName() << std::endl; - file << "User-local contact: " << std::endl; //add optional porperty - file << "Title: "<< m_ws->getTitle() << std::endl; + std::string user = getProperty("UserContact"); + file << "User-local contact: " << user << std::endl; //add optional property + std::string title = getProperty("Title"); + file << "Title: " << title << std::endl; auto samp = m_ws->run(); file << "Subtitle: " << samp.getLogData("run_title")->value() << std::endl; file << "Start date + time: " << samp.getLogData("run_start")->value() << std::endl; @@ -107,82 +58,10 @@ namespace Mantid } file << "Number of file format: 2" << std::endl; - file << "Number of data points:" << m_sep << xlength << std::endl; + file << "Number of data points:" << m_sep << m_xlength << std::endl; file << std::endl; file << m_sep << "q" << m_sep << "refl" << m_sep << "refl_err" << m_sep << "q_res" << std::endl; - - for (size_t i = 0; i < xlength; ++i) - { - double dq = X1[i]*qres; - outputval(X1[i], file); - outputval(y1[i], file); - outputval(e1[i], file); - outputval(dq, file); - file << std::endl; - } - file.close(); } - - void SaveILLCosmosAscii::outputval (double val, std::ofstream & file, bool leadingSep) - { - bool nancheck = checkIfNan(val); - bool infcheck = checkIfInfinite(val); - if (leadingSep) - { - if (!nancheck && !infcheck) - { - file << m_sep << val; - } - else if (nancheck) - { - //not a number - output nan - file << m_sep << "nan"; - } - else if (infcheck) - { - //infinite - output 'inf' - file << m_sep << "inf"; - } - else - { - //not valid, nan or inf - so output 'und' - file << m_sep << "und"; - } - } - else - { - if (!nancheck && !infcheck) - { - file << val; - } - else if (nancheck) - { - //not a number - output nan - file << "nan"; - } - else if (infcheck) - { - //infinite - output 'inf' - file << "inf"; - } - else - { - //not valid, nan or inf - so output 'und' - file << "und"; - } - } - } - - bool SaveILLCosmosAscii::checkIfNan(const double& value) const - { - return (boost::math::isnan(value)); - } - - bool SaveILLCosmosAscii::checkIfInfinite(const double& value) const - { - return (std::abs(value) == std::numeric_limits::infinity()); - } - } // namespace DataHandling } // namespace Mantid From fb433265e2799ea53b6ba25fa6846c5b47e461fe Mon Sep 17 00:00:00 2001 From: Keith Brown Date: Fri, 14 Mar 2014 11:57:20 +0000 Subject: [PATCH 05/11] Refs #8960 Unit test and exception catching The ILLCosmos algorthim will no longer throw if the workspace is missing one or more logs, it will just not print that heading instead. The test algorithm has been adapted from SaveANSTOAscii (and could also probably be superclassed) and now checks the headings too. --- .../DataHandling/src/SaveILLCosmosAscii.cpp | 48 ++++++++++-- .../test/SaveILLCosmosAsciiTest.h | 73 ++++++++++++++----- 2 files changed, 96 insertions(+), 25 deletions(-) diff --git a/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp b/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp index c3fcbcced0ca..e02d03715cf0 100644 --- a/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp +++ b/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp @@ -39,16 +39,50 @@ namespace Mantid void SaveILLCosmosAscii::extraHeaders(std::ofstream & file) { + auto samp = m_ws->run(); + std::string instrument; + std::string user; + std::string title; + std::string subtitle; + std::string startDT; + std::string endDT; + try + {instrument = m_ws->getInstrument()->getName();} + catch (...) + {instrument = "";} + + try + {user = getProperty("UserContact");} + catch (...) + {user = "";} + + try + {title = getProperty("Title");} + catch (...) + {title = "";} + + try + {subtitle = samp.getLogData("run_title")->value();} + catch (...) + {subtitle = "";} + + try + {startDT = samp.getLogData("run_start")->value();} + catch (...) + {startDT = "";} + + try + {endDT = samp.getLogData("run_end")->value();} + catch (...) + {endDT = "";} + file << "MFT" << std::endl; - file << "Instrument: "<< m_ws->getInstrument()->getName() << std::endl; - std::string user = getProperty("UserContact"); + file << "Instrument: "<< instrument << std::endl; file << "User-local contact: " << user << std::endl; //add optional property - std::string title = getProperty("Title"); file << "Title: " << title << std::endl; - auto samp = m_ws->run(); - file << "Subtitle: " << samp.getLogData("run_title")->value() << std::endl; - file << "Start date + time: " << samp.getLogData("run_start")->value() << std::endl; - file << "End date + time: " << samp.getLogData("run_end")->value() << std::endl; + file << "Subtitle: " << subtitle << std::endl; + file << "Start date + time: " << startDT << std::endl; + file << "End date + time: " << endDT << std::endl; const std::vector logList = getProperty("LogList"); ///logs diff --git a/Code/Mantid/Framework/DataHandling/test/SaveILLCosmosAsciiTest.h b/Code/Mantid/Framework/DataHandling/test/SaveILLCosmosAsciiTest.h index 6db9ba1565dc..561b3e761f07 100644 --- a/Code/Mantid/Framework/DataHandling/test/SaveILLCosmosAsciiTest.h +++ b/Code/Mantid/Framework/DataHandling/test/SaveILLCosmosAsciiTest.h @@ -59,14 +59,18 @@ class SaveILLCosmosAsciiTest : public CxxTest::TestSuite TS_ASSERT( Poco::File(m_long_filename).exists() ); std::ifstream in(m_long_filename.c_str()); std::string fullline; + headingsTests(in, fullline); getline(in,fullline); + std::vector columns; boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on); - TS_ASSERT_EQUALS(columns.size(),4); - TS_ASSERT_DELTA(boost::lexical_cast(columns.at(0)), 1.5, 0.01); - TS_ASSERT_DELTA(boost::lexical_cast(columns.at(1)), 1, 0.01); + TS_ASSERT_EQUALS(columns.size(),5); + //the first is black due to the leading tab + TS_ASSERT(columns.at(0) == ""); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(1)), 1.5, 0.01); TS_ASSERT_DELTA(boost::lexical_cast(columns.at(2)), 1, 0.01); - TS_ASSERT_DELTA(boost::lexical_cast(columns.at(3)), 0.6, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(3)), 1, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(4)), 0.6, 0.01); in.close(); cleanupafterwards(); @@ -90,14 +94,17 @@ class SaveILLCosmosAsciiTest : public CxxTest::TestSuite TS_ASSERT( Poco::File(m_long_filename).exists() ); std::ifstream in(m_long_filename.c_str()); std::string fullline; + headingsTests(in, fullline); getline(in,fullline); std::vector columns; boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on); - TS_ASSERT_EQUALS(columns.size(),4); - TS_ASSERT_DELTA(boost::lexical_cast(columns.at(0)), 0, 0.01); - TS_ASSERT_DELTA(boost::lexical_cast(columns.at(1)), 1, 0.01); + TS_ASSERT_EQUALS(columns.size(),5); + //the first is black due to the leading tab + TS_ASSERT(columns.at(0) == ""); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(1)), 0, 0.01); TS_ASSERT_DELTA(boost::lexical_cast(columns.at(2)), 1, 0.01); - TS_ASSERT((columns.at(3) == "nan") || (columns.at(3) == "inf")); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(3)), 1, 0.01); + TS_ASSERT((columns.at(4) == "nan") || (columns.at(4) == "inf")); in.close(); cleanupafterwards(); @@ -121,14 +128,17 @@ class SaveILLCosmosAsciiTest : public CxxTest::TestSuite TS_ASSERT( Poco::File(m_long_filename).exists() ); std::ifstream in(m_long_filename.c_str()); std::string fullline; + headingsTests(in, fullline); getline(in,fullline); std::vector columns; boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on); - TS_ASSERT_EQUALS(columns.size(),4); - TS_ASSERT_DELTA(boost::lexical_cast(columns.at(0)), 1.5, 0.01); - TS_ASSERT_DELTA(boost::lexical_cast(columns.at(1)), 0, 0.01); - TS_ASSERT_DELTA(boost::lexical_cast(columns.at(2)), 1, 0.01); - TS_ASSERT_DELTA(boost::lexical_cast(columns.at(3)), 0.6, 0.01); + TS_ASSERT_EQUALS(columns.size(),5); + //the first is black due to the leading tab + TS_ASSERT(columns.at(0) == ""); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(1)), 1.5, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(2)), 0, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(3)), 1, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(4)), 0.6, 0.01); in.close(); cleanupafterwards(); @@ -152,14 +162,17 @@ class SaveILLCosmosAsciiTest : public CxxTest::TestSuite TS_ASSERT( Poco::File(m_long_filename).exists() ); std::ifstream in(m_long_filename.c_str()); std::string fullline; + headingsTests(in, fullline); getline(in,fullline); std::vector columns; boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on); - TS_ASSERT_EQUALS(columns.size(),4); - TS_ASSERT_DELTA(boost::lexical_cast(columns.at(0)), 1.5, 0.01); - TS_ASSERT_DELTA(boost::lexical_cast(columns.at(1)), 1, 0.01); - TS_ASSERT_DELTA(boost::lexical_cast(columns.at(2)), 0, 0.01); - TS_ASSERT_DELTA(boost::lexical_cast(columns.at(3)), 0.6, 0.01); + TS_ASSERT_EQUALS(columns.size(),5); + //the first is black due to the leading tab + TS_ASSERT(columns.at(0) == ""); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(1)), 1.5, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(2)), 1, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(3)), 0, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(4)), 0.6, 0.01); in.close(); cleanupafterwards(); @@ -179,6 +192,30 @@ class SaveILLCosmosAsciiTest : public CxxTest::TestSuite TS_ASSERT( !Poco::File(m_long_filename).exists() ); } private: + void headingsTests(std::ifstream & in,std::string & fullline) + { + getline(in,fullline); + TS_ASSERT(fullline == "MFT"); + getline(in,fullline); + TS_ASSERT(fullline == "Instrument: "); + getline(in,fullline); + TS_ASSERT(fullline == "User-local contact: "); + getline(in,fullline); + TS_ASSERT(fullline == "Title: "); + getline(in,fullline); + TS_ASSERT(fullline == "Subtitle: "); + getline(in,fullline); + TS_ASSERT(fullline == "Start date + time: "); + getline(in,fullline); + TS_ASSERT(fullline == "End date + time: "); + getline(in,fullline); + TS_ASSERT(fullline == "Number of file format: 2"); + getline(in,fullline); + TS_ASSERT(fullline == "Number of data points:\t9"); + getline(in,fullline); + getline(in,fullline); + TS_ASSERT(fullline == "\tq\trefl\trefl_err\tq_res"); + } void createWS(bool zeroX = false, bool zeroY = false, bool zeroE = false) { MatrixWorkspace_sptr ws = WorkspaceCreationHelper::Create2DWorkspace(1,10); From 210efd230d2aa2e41a064a21e2767c468ff9b077 Mon Sep 17 00:00:00 2001 From: Keith Brown Date: Wed, 19 Mar 2014 10:03:05 +0000 Subject: [PATCH 06/11] Refs #8960 Fix coverity warnings from original SaveAnstoAscii m_sep is no more, replaced totally by the virtual function sep() The data writing function has been refactored as that was a lot of redundant code. --- .../inc/MantidDataHandling/AsciiPointBase.h | 5 +- .../DataHandling/src/AsciiPointBase.cpp | 51 +++++-------------- .../DataHandling/src/SaveILLCosmosAscii.cpp | 4 +- 3 files changed, 16 insertions(+), 44 deletions(-) diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h index 2ec7295af14f..4c3b350bcbb0 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h @@ -36,8 +36,6 @@ namespace Mantid virtual void initDocs() = 0; /// Return the file extension this algorthm should output. virtual std::string ext() = 0; - /// Return the separator character - virtual char sep() {return '\t';} /// return if the line should start with a separator virtual bool leadingSep() {return true;} /// Add extra properties @@ -61,7 +59,8 @@ namespace Mantid ///static reference to the logger class static Kernel::Logger& g_log; protected: - char m_sep; + /// Return the separator character + virtual char sep() {return '\t';} double m_qres; size_t m_xlength; API::MatrixWorkspace_const_sptr m_ws; diff --git a/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp b/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp index 723c0c02a82b..f3f1c1a62f32 100644 --- a/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp +++ b/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp @@ -30,7 +30,6 @@ namespace Mantid std::vector exts; exts.push_back(ext()); declareProperty(new FileProperty("Filename", "", FileProperty::Save, exts), "The filename of the output file."); - m_sep = sep(); extraProps(); } @@ -94,47 +93,21 @@ namespace Mantid bool infcheck = checkIfInfinite(val); if (leadingSep) { - if (!nancheck && !infcheck) - { - file << m_sep << val; - } - else if (nancheck) - { - //not a number - output nan - file << m_sep << "nan"; - } - else if (infcheck) - { - //infinite - output 'inf' - file << m_sep << "inf"; - } - else - { - //not valid, nan or inf - so output 'und' - file << m_sep << "und"; - } + file << sep(); + } + if (!nancheck && !infcheck) + { + file << val; + } + else if (infcheck) + { + //infinite - output 'inf' + file << "inf"; } else { - if (!nancheck && !infcheck) - { - file << val; - } - else if (nancheck) - { - //not a number - output nan - file << "nan"; - } - else if (infcheck) - { - //infinite - output 'inf' - file << "inf"; - } - else - { - //not valid, nan or inf - so output 'und' - file << "und"; - } + //not a number - output nan + file << "nan"; } } diff --git a/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp b/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp index e02d03715cf0..529b9decd38c 100644 --- a/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp +++ b/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp @@ -92,10 +92,10 @@ namespace Mantid } file << "Number of file format: 2" << std::endl; - file << "Number of data points:" << m_sep << m_xlength << std::endl; + file << "Number of data points:" << sep() << m_xlength << std::endl; file << std::endl; - file << m_sep << "q" << m_sep << "refl" << m_sep << "refl_err" << m_sep << "q_res" << std::endl; + file << sep() << "q" << sep() << "refl" << sep() << "refl_err" << sep() << "q_res" << std::endl; } } // namespace DataHandling } // namespace Mantid From d56dbb9d3ca267246bb65fa09604d9715cce6558 Mon Sep 17 00:00:00 2001 From: Keith Brown Date: Wed, 19 Mar 2014 10:44:21 +0000 Subject: [PATCH 07/11] Refs #8960 Fix style and property grabbing errors Eliminated a couple of try-catches due to them not being needed. Fixed style and indentation --- .../DataHandling/src/SaveILLCosmosAscii.cpp | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp b/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp index 529b9decd38c..180cc8fafc96 100644 --- a/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp +++ b/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp @@ -41,40 +41,43 @@ namespace Mantid { auto samp = m_ws->run(); std::string instrument; - std::string user; - std::string title; + std::string user = getProperty("UserContact"); + std::string title = getProperty("Title"); std::string subtitle; std::string startDT; std::string endDT; - try - {instrument = m_ws->getInstrument()->getName();} - catch (...) - {instrument = "";} - - try - {user = getProperty("UserContact");} - catch (...) - {user = "";} - - try - {title = getProperty("Title");} - catch (...) - {title = "";} + auto tempInst = m_ws->getInstrument(); + if (tempInst) + { + instrument = tempInst->getName(); + } try - {subtitle = samp.getLogData("run_title")->value();} - catch (...) - {subtitle = "";} + { + subtitle = samp.getLogData("run_title")->value(); + } + catch (Kernel::Exception::NotFoundError &) + { + subtitle = ""; + } try - {startDT = samp.getLogData("run_start")->value();} - catch (...) - {startDT = "";} + { + startDT = samp.getLogData("run_start")->value(); + } + catch (Kernel::Exception::NotFoundError &) + { + startDT = ""; + } try - {endDT = samp.getLogData("run_end")->value();} - catch (...) - {endDT = "";} + { + endDT = samp.getLogData("run_end")->value(); + } + catch (Kernel::Exception::NotFoundError &) + { + endDT = ""; + } file << "MFT" << std::endl; file << "Instrument: "<< instrument << std::endl; From 5908ebaa6a80e1fb3331c11963ade209cea9d3c7 Mon Sep 17 00:00:00 2001 From: Keith Brown Date: Wed, 19 Mar 2014 14:47:15 +0000 Subject: [PATCH 08/11] Refs #8960 Added tests for the properties and logs There is a new test checking that the logs and extra properties are read and work correctly. --- .../test/SaveILLCosmosAsciiTest.h | 81 ++++++++++++++++--- 1 file changed, 70 insertions(+), 11 deletions(-) diff --git a/Code/Mantid/Framework/DataHandling/test/SaveILLCosmosAsciiTest.h b/Code/Mantid/Framework/DataHandling/test/SaveILLCosmosAsciiTest.h index 561b3e761f07..9fea30935234 100644 --- a/Code/Mantid/Framework/DataHandling/test/SaveILLCosmosAsciiTest.h +++ b/Code/Mantid/Framework/DataHandling/test/SaveILLCosmosAsciiTest.h @@ -177,7 +177,43 @@ class SaveILLCosmosAsciiTest : public CxxTest::TestSuite cleanupafterwards(); } + void testParameters() + { + //create a new workspace and then delete it later on + createWS(false,false,false,true); + + Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveILLCosmosAscii"); + alg->setPropertyValue("InputWorkspace", m_name); + alg->setPropertyValue("Filename", m_filename); + alg->setPropertyValue("UserContact", "John Smith"); + alg->setPropertyValue("Title", "Testing this algorithm"); + TS_ASSERT_THROWS_NOTHING(alg->execute()); + + if ( ! alg->isExecuted() ) + { + TS_FAIL("Could not run SaveILLCosmosAscii"); + } + m_long_filename= alg->getPropertyValue("Filename"); + // has the algorithm written a file to disk? + TS_ASSERT( Poco::File(m_long_filename).exists() ); + std::ifstream in(m_long_filename.c_str()); + std::string fullline; + headingsTests(in, fullline,true); + getline(in,fullline); + + std::vector columns; + boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on); + TS_ASSERT_EQUALS(columns.size(),5); + //the first is black due to the leading tab + TS_ASSERT(columns.at(0) == ""); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(1)), 1.5, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(2)), 1, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(3)), 1, 0.01); + TS_ASSERT_DELTA(boost::lexical_cast(columns.at(4)), 0.6, 0.01); + in.close(); + cleanupafterwards(); + } void test_fail_invalid_workspace() { Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveILLCosmosAscii"); @@ -192,22 +228,37 @@ class SaveILLCosmosAsciiTest : public CxxTest::TestSuite TS_ASSERT( !Poco::File(m_long_filename).exists() ); } private: - void headingsTests(std::ifstream & in,std::string & fullline) + void headingsTests(std::ifstream & in,std::string & fullline, bool propertiesLogs = false) { getline(in,fullline); TS_ASSERT(fullline == "MFT"); getline(in,fullline); TS_ASSERT(fullline == "Instrument: "); getline(in,fullline); - TS_ASSERT(fullline == "User-local contact: "); - getline(in,fullline); - TS_ASSERT(fullline == "Title: "); - getline(in,fullline); - TS_ASSERT(fullline == "Subtitle: "); - getline(in,fullline); - TS_ASSERT(fullline == "Start date + time: "); - getline(in,fullline); - TS_ASSERT(fullline == "End date + time: "); + if (propertiesLogs) + { + TS_ASSERT(fullline == "User-local contact: John Smith"); + getline(in,fullline); + TS_ASSERT(fullline == "Title: Testing this algorithm"); + getline(in,fullline); + TS_ASSERT(fullline == "Subtitle: ILL COSMOS save test"); + getline(in,fullline); + TS_ASSERT(fullline == "Start date + time: 2011-12-16T01:27:30"); + getline(in,fullline); + TS_ASSERT(fullline == "End date + time: 2011-12-16T02:13:31"); + } + else + { + TS_ASSERT(fullline == "User-local contact: "); + getline(in,fullline); + TS_ASSERT(fullline == "Title: "); + getline(in,fullline); + TS_ASSERT(fullline == "Subtitle: "); + getline(in,fullline); + TS_ASSERT(fullline == "Start date + time: "); + getline(in,fullline); + TS_ASSERT(fullline == "End date + time: "); + } getline(in,fullline); TS_ASSERT(fullline == "Number of file format: 2"); getline(in,fullline); @@ -216,9 +267,17 @@ class SaveILLCosmosAsciiTest : public CxxTest::TestSuite getline(in,fullline); TS_ASSERT(fullline == "\tq\trefl\trefl_err\tq_res"); } - void createWS(bool zeroX = false, bool zeroY = false, bool zeroE = false) + void createWS(bool zeroX = false, bool zeroY = false, bool zeroE = false, bool createLogs = false) { MatrixWorkspace_sptr ws = WorkspaceCreationHelper::Create2DWorkspace(1,10); + + if (createLogs) + { + ws->mutableRun().addProperty("run_title",std::string("ILL COSMOS save test")); + ws->mutableRun().addProperty("run_start",std::string("2011-12-16T01:27:30")); + ws->mutableRun().addProperty("run_end",std::string("2011-12-16T02:13:31")); + } + AnalysisDataService::Instance().addOrReplace(m_name, ws); //Check if any of X, Y or E should be zeroed to check for divide by zero or similiar if (zeroX) From f290183a8b79f158bcdadbfae80d1e769d7a5600 Mon Sep 17 00:00:00 2001 From: Keith Brown Date: Thu, 20 Mar 2014 11:12:31 +0000 Subject: [PATCH 09/11] Refs #8960 Copyright info, Gcc and Cppcheck warnings Added copyright info to the three classes. Fixed a Gcc warning pertaining to an unused parameter Fixed a Cppcheck warning pertaining to an over-scoped variable --- .../inc/MantidDataHandling/AsciiPointBase.h | 36 ++++++++++++++----- .../inc/MantidDataHandling/SaveANSTOAscii.h | 28 ++++++++++++++- .../MantidDataHandling/SaveILLCosmosAscii.h | 25 ++++--------- .../DataHandling/src/AsciiPointBase.cpp | 3 +- .../DataHandling/src/SaveANSTOAscii.cpp | 4 +++ 5 files changed, 67 insertions(+), 29 deletions(-) diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h index 4c3b350bcbb0..cc9867ecdc06 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h @@ -5,11 +5,38 @@ // Includes //---------------------------------------------------------------------- #include "MantidAPI/Algorithm.h" +#include namespace Mantid { namespace DataHandling { + /** + Abstract base class for some ascii format save algorithms that print point data and dq/q. + AsciiPointBase is a framework for some algorithms. It overrides exec and init and provides full + implementation for any subclasses and as such any subclasses should only provide implementations + for the additional abstract and virtual methods provided by this class. + + Copyright © 2007-14 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory + + This file is part of Mantid. + + Mantid is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + Mantid is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + File change history is stored at: . + Code Documentation is available at: + */ class DLLExport AsciiPointBase : public API::Algorithm { public: @@ -23,14 +50,7 @@ namespace Mantid virtual int version() const = 0; /// Algorithm's category for identification overriding a virtual method virtual const std::string category() const { return "DataHandling\\Text"; } - /* - /// Algorithm's name for identification overriding a virtual method - virtual const std::string name() const { return "AsciiPointBase"; } - /// Algorithm's version for identification overriding a virtual method - virtual int version() const { return 1; } - /// Algorithm's category for identification overriding a virtual method - virtual const std::string category() const { return "DataHandling\\Text"; } - */ + private: /// Sets documentation strings for this algorithm virtual void initDocs() = 0; diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveANSTOAscii.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveANSTOAscii.h index 78c31b142136..40400cac7926 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveANSTOAscii.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveANSTOAscii.h @@ -11,6 +11,32 @@ namespace Mantid { namespace DataHandling { + /** + Saves a file in Ansto format and from a 2D workspace + (Workspace2D class). SaveANSTOAscii is an algorithm but inherits from the + AsciiPointBase class which provides the main implementation for the init() & exec() methods. + Output is tab delimited Ascii point data with dq/q. + + Copyright © 2007-14 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory + + This file is part of Mantid. + + Mantid is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + Mantid is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + File change history is stored at: . + Code Documentation is available at: + */ class DLLExport SaveANSTOAscii : public DataHandling::AsciiPointBase { public: @@ -33,7 +59,7 @@ namespace Mantid /// no extra properties required so override blank virtual void extraProps() {} /// no extra information required so override blank - virtual void extraHeaders(std::ofstream & file) {} + virtual void extraHeaders(std::ofstream & file); ///static reference to the logger class static Kernel::Logger& g_log; }; diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h index 768df3562d8b..9f6083525d2f 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h @@ -12,24 +12,13 @@ namespace Mantid { namespace DataHandling { - /** @class SaveILLCosmosAscii SaveILLCosmosAscii.h DataHandling/SaveILLCosmosAscii.h + /** + Saves a file in ILL Cosmos format from a 2D workspace + (Workspace2D class). SaveILLCosmosAscii is an algorithm but inherits frrm the + AsciiPointBase class which provides the main implementation for the init() & exec() methods. + Output is tab delimited Ascii point data with dq/q and extra header information. - Saves a workspace or selected spectra in a coma-separated ascii file. Spectra are saved in columns. - Properties: -
    -
  • Filename - the name of the file to write to.
  • -
  • Workspace - the workspace name to be saved.
  • -
  • SpectrumMin - the starting spectrum index to save (optional)
  • -
  • SpectrumMax - the ending spectrum index to save (optional)
  • -
  • SpectrumList - a list of comma-separated spectra indeces to save (optional)
  • -
  • Precision - the numeric precision - the number of significant digits for the saved data (optional)
  • -
- - - @author Keith Brown, ISIS, Placement student from the University of Derby - @date 10/10/13 - - Copyright © 2007-9 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory + Copyright © 2007-14 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory This file is part of Mantid. @@ -46,7 +35,7 @@ namespace Mantid You should have received a copy of the GNU General Public License along with this program. If not, see . - File change history is stored at: . + File change history is stored at: . Code Documentation is available at: */ class DLLExport SaveILLCosmosAscii : public DataHandling::AsciiPointBase diff --git a/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp b/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp index f3f1c1a62f32..85602aefb13e 100644 --- a/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp +++ b/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp @@ -75,10 +75,9 @@ namespace Mantid { const std::vector & yData = m_ws->readY(0); const std::vector & eData = m_ws->readE(0); - double dq; for (size_t i = 0; i < m_xlength; ++i) { - dq = XData[i]*m_qres; + double dq = XData[i]*m_qres; outputval(XData[i], file, leadingSep()); outputval(yData[i], file); outputval(eData[i], file); diff --git a/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp b/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp index 9ae69fb91136..20b935c512d5 100644 --- a/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp +++ b/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp @@ -27,5 +27,9 @@ namespace Mantid this->setWikiSummary("Saves a 2D [[workspace]] to a tab separated ascii file. "); this->setOptionalMessage("Saves a 2D workspace to a ascii file."); } + void SaveANSTOAscii::extraHeaders(std::ofstream & file) + { + UNUSED_ARG(file); + } } // namespace DataHandling } // namespace Mantid From d4501832824826ed8e50c9b4431967aceee14266 Mon Sep 17 00:00:00 2001 From: Keith Brown Date: Thu, 20 Mar 2014 11:34:17 +0000 Subject: [PATCH 10/11] Refs #8960 Added documentation Added Doxygen documentation to the classes --- .../DataHandling/src/AsciiPointBase.cpp | 21 ++++++++++++++++++- .../DataHandling/src/SaveANSTOAscii.cpp | 5 +++++ .../DataHandling/src/SaveILLCosmosAscii.cpp | 4 ++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp b/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp index 85602aefb13e..e08d162f61fd 100644 --- a/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp +++ b/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp @@ -34,7 +34,7 @@ namespace Mantid } /** - * Executes the algorithm. + * Executes the algorithm. In this case it provides the process for any child classes as this class is abstract */ void AsciiPointBase::exec() { @@ -54,6 +54,10 @@ namespace Mantid file.close(); } + /** Adds extra data to the top of the file. + * @param file :: pointer to output file stream + * @returns std::vector of point data for the X column + */ std::vector AsciiPointBase::header(std::ofstream & file) { auto title ='#'+ m_ws->getTitle(); @@ -71,6 +75,10 @@ namespace Mantid return XData; } + /** virtual method to add information to the file before the data + * @param file :: pointer to output file stream + * @param XData :: pointer to a std::vector containing the point data to be printed + */ void AsciiPointBase::data(std::ofstream & file, const std::vector & XData) { const std::vector & yData = m_ws->readY(0); @@ -86,6 +94,11 @@ namespace Mantid } } + /** writes a properly formatted line of data + * @param val :: the double value to be written + * @param file :: pointer to output file stream + * @param leadingSep :: boolean to determine if there should be a separator before this value, default true + */ void AsciiPointBase::outputval (double val, std::ofstream & file, bool leadingSep) { bool nancheck = checkIfNan(val); @@ -110,11 +123,17 @@ namespace Mantid } } + /** checks if a value is Not A Number + * @returns boolean true if the supplied value was Not a Number + */ bool AsciiPointBase::checkIfNan(const double& value) const { return (boost::math::isnan(value)); } + /** checks if a value is Infinite + * @returns boolean true if the supplied value was Infinite + */ bool AsciiPointBase::checkIfInfinite(const double& value) const { return (std::abs(value) == std::numeric_limits::infinity()); diff --git a/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp b/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp index 20b935c512d5..c0bda2c28251 100644 --- a/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp +++ b/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp @@ -27,6 +27,11 @@ namespace Mantid this->setWikiSummary("Saves a 2D [[workspace]] to a tab separated ascii file. "); this->setOptionalMessage("Saves a 2D workspace to a ascii file."); } + + /** virtual method to add information to the file before the data + * however this class doesn't have any but must implement it. + * @param file :: pointer to output file stream + */ void SaveANSTOAscii::extraHeaders(std::ofstream & file) { UNUSED_ARG(file); diff --git a/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp b/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp index 180cc8fafc96..2a9f400eb4e8 100644 --- a/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp +++ b/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp @@ -30,6 +30,7 @@ namespace Mantid this->setOptionalMessage("Saves a 2D workspace to a ascii file."); } + /// virtual method to set the extra properties required for this algorithm void SaveILLCosmosAscii::extraProps() { declareProperty(new ArrayProperty("LogList"),"List of logs to write to file."); @@ -37,6 +38,9 @@ namespace Mantid declareProperty("Title", "", "Text to be written to the Title field"); } + /** virtual method to add information to the file before the data + * @param file :: pointer to output file stream + */ void SaveILLCosmosAscii::extraHeaders(std::ofstream & file) { auto samp = m_ws->run(); From 1514cfee98551158b29f23d81c37920c49bd4e13 Mon Sep 17 00:00:00 2001 From: Martyn Gigg Date: Fri, 21 Mar 2014 11:43:38 +0000 Subject: [PATCH 11/11] Remove unnecessary static loggers. Just use the base-algorithm class logger. Refs #8960 --- .../DataHandling/inc/MantidDataHandling/AsciiPointBase.h | 2 -- .../DataHandling/inc/MantidDataHandling/SaveANSTOAscii.h | 2 -- .../DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h | 2 -- Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp | 3 --- Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp | 3 --- Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp | 3 --- 6 files changed, 15 deletions(-) diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h index cc9867ecdc06..91ff5ddd30b6 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/AsciiPointBase.h @@ -76,8 +76,6 @@ namespace Mantid void outputval (double val, std::ofstream & file, bool leadingSep = true); /// write the top of the file virtual std::vector header(std::ofstream & file); - ///static reference to the logger class - static Kernel::Logger& g_log; protected: /// Return the separator character virtual char sep() {return '\t';} diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveANSTOAscii.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveANSTOAscii.h index 40400cac7926..a57dd49f95aa 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveANSTOAscii.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveANSTOAscii.h @@ -60,8 +60,6 @@ namespace Mantid virtual void extraProps() {} /// no extra information required so override blank virtual void extraHeaders(std::ofstream & file); - ///static reference to the logger class - static Kernel::Logger& g_log; }; } // namespace DataHandling diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h index 9f6083525d2f..9379c2027317 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/SaveILLCosmosAscii.h @@ -55,8 +55,6 @@ namespace Mantid virtual void initDocs(); /// Return the file extension this algorthm should output. virtual std::string ext() {return ".mft";} - ///static reference to the logger class - static Kernel::Logger& g_log; ///extra properties specifically for this virtual void extraProps(); /// write any extra information required diff --git a/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp b/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp index e08d162f61fd..a4c7b89926e1 100644 --- a/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp +++ b/Code/Mantid/Framework/DataHandling/src/AsciiPointBase.cpp @@ -18,9 +18,6 @@ namespace Mantid using namespace Kernel; using namespace API; - // Initialise the logger - Logger& AsciiPointBase::g_log = Logger::get("AsciiPointBase"); - /// Initialisation method. void AsciiPointBase::init() { diff --git a/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp b/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp index c0bda2c28251..cbc0484c019c 100644 --- a/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp +++ b/Code/Mantid/Framework/DataHandling/src/SaveANSTOAscii.cpp @@ -18,9 +18,6 @@ namespace Mantid using namespace Kernel; using namespace API; - // Initialise the logger - Logger& SaveANSTOAscii::g_log = Logger::get("SaveANSTOAscii"); - /// Sets documentation strings for this algorithm void SaveANSTOAscii::initDocs() { diff --git a/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp b/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp index 2a9f400eb4e8..9b638370f3f5 100644 --- a/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp +++ b/Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp @@ -20,9 +20,6 @@ namespace Mantid using namespace Kernel; using namespace API; - // Initialise the logger - Logger& SaveILLCosmosAscii::g_log = Logger::get("SaveILLCosmosAscii"); - /// Sets documentation strings for this algorithm void SaveILLCosmosAscii::initDocs() {