Newer
Older
Steve Williams
committed
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
Peterson, Peter
committed
#include "MantidAlgorithms/AddSampleLog.h"
Steve Williams
committed
#include "MantidKernel/Exception.h"
Janik Zikovsky
committed
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/Strings.h"
#include "MantidKernel/TimeSeriesProperty.h"
Steve Williams
committed
#include <string>
namespace Mantid
{
namespace Algorithms
{
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(AddSampleLog)
Janik Zikovsky
committed
/// Sets documentation strings for this algorithm
void AddSampleLog::initDocs()
{
Janik Zikovsky
committed
this->setWikiSummary("Used to insert a value into the sample logs in a workspace.");
this->setOptionalMessage("Used to insert a value into the sample logs in a workspace.");
Janik Zikovsky
committed
}
Steve Williams
committed
using namespace Kernel;
using namespace API;
void AddSampleLog::init()
{
declareProperty(new WorkspaceProperty<>("Workspace","",Direction::InOut),
"Workspace to add the log entry to");
declareProperty("LogName", "", new MandatoryValidator<std::string>,
"The name that will identify the log entry");
Janik Zikovsky
committed
std::vector<std::string> propOptions;
propOptions.push_back("String");
propOptions.push_back("Number");
propOptions.push_back("Number Series");
declareProperty("LogType", "String",new ListValidator(propOptions),
"The type that the log data will be."
);
Steve Williams
committed
declareProperty("LogText", "",
"The content of the log");
}
void AddSampleLog::exec()
{
// A pointer to the workspace to add a log to
MatrixWorkspace_sptr wSpace = getProperty("Workspace");
Gigg, Martyn Anthony
committed
// we're going to edit the workspaces run details so get a non-const reference to it
Run &theRun = wSpace->mutableRun();
Steve Williams
committed
// get the data that the user wants to add
std::string propName = getProperty("LogName");
std::string propValue = getProperty("LogText");
Janik Zikovsky
committed
std::string propType = getPropertyValue("LogType");
// Remove any existing log
if (theRun.hasProperty(propName))
theRun.removeLogData(propName);
Steve Williams
committed
Janik Zikovsky
committed
if (propType == "String")
{
theRun.addLogData(new PropertyWithValue<std::string>(propName, propValue));
}
else if (propType == "Number")
{
double val;
if (!Strings::convert(propValue, val))
throw std::invalid_argument("Error interpreting string '" + propValue + "' as a number.");
theRun.addLogData(new PropertyWithValue<double>(propName, val));
}
else if (propType == "Number Series")
{
double val;
if (!Strings::convert(propValue, val))
throw std::invalid_argument("Error interpreting string '" + propValue + "' as a number.");
Kernel::DateAndTime now = Kernel::DateAndTime::get_current_time();
TimeSeriesProperty<double> * tsp = new TimeSeriesProperty<double>(propName);
tsp->addValue(now, val);
theRun.addLogData(tsp);
}
Steve Williams
committed
setProperty("Workspace", wSpace);
}
} // namespace Algorithms
} // namespace Mantid