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/MandatoryValidator.h"
Janik Zikovsky
committed
#include "MantidKernel/Strings.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/PropertyWithValue.h"
Steve Williams
committed
#include <string>
namespace {
static const std::string intTypeOption = "Int";
static const std::string doubleTypeOption = "Double";
static const std::string autoTypeOption = "AutoDetect";
static const std::string stringLogOption = "String";
static const std::string numberLogOption = "Number";
static const std::string numberSeriesLogOption = "Number Series";
}
namespace Mantid {
namespace Algorithms {
Steve Williams
committed
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(AddSampleLog)
using namespace Kernel;
using namespace API;
void AddSampleLog::init() {
declareProperty(
new WorkspaceProperty<Workspace>("Workspace", "", Direction::InOut),
"Workspace to add the log entry to");
declareProperty("LogName", "",
boost::make_shared<MandatoryValidator<std::string>>(),
"The name that will identify the log entry");
Janik Zikovsky
committed
declareProperty("LogText", "", "The content of the log");
Janik Zikovsky
committed
std::vector<std::string> propOptions;
propOptions.push_back(stringLogOption);
propOptions.push_back(numberLogOption);
propOptions.push_back(numberSeriesLogOption);
declareProperty("LogType", stringLogOption,
boost::make_shared<StringListValidator>(propOptions),
"The type that the log data will be.");
declareProperty("LogUnit", "", "The units of the log");
std::vector<std::string> typeOptions;
typeOptions.push_back(intTypeOption);
typeOptions.push_back(doubleTypeOption);
typeOptions.push_back(autoTypeOption);
declareProperty("NumberType", autoTypeOption,
boost::make_shared<StringListValidator>(typeOptions),
"Force LogText to be interpreted as a number of type 'int' "
"or 'double'.");
Steve Williams
committed
}
Steve Williams
committed
// A pointer to the workspace to add a log to
Workspace_sptr ws1 = getProperty("Workspace");
ExperimentInfo_sptr ws = boost::dynamic_pointer_cast<ExperimentInfo>(ws1);
// we're going to edit the workspaces run details so get a non-const reference
// to it
Run &theRun = ws->mutableRun();
Steve Williams
committed
// get the data that the user wants to add
std::string propName = getProperty("LogName");
std::string propValue = getProperty("LogText");
std::string propUnit = getProperty("LogUnit");
Janik Zikovsky
committed
std::string propType = getPropertyValue("LogType");
std::string propNumberType = getPropertyValue("NumberType");
if ((propNumberType != autoTypeOption) &&
((propType != numberLogOption) && (propType != numberSeriesLogOption))) {
throw std::invalid_argument(
"You may only use NumberType 'Int' or 'Double' options if "
"LogType is 'Number' or 'Number Series'");
Janik Zikovsky
committed
// Remove any existing log
Janik Zikovsky
committed
theRun.removeLogData(propName);
Steve Williams
committed
if (propType == stringLogOption) {
Janik Zikovsky
committed
theRun.addLogData(new PropertyWithValue<std::string>(propName, propValue));
theRun.getProperty(propName)->setUnits(propUnit);
return;
}
int intVal;
double dblVal;
if (propNumberType != autoTypeOption) {
value_is_int = (propNumberType == intTypeOption);
if (value_is_int) {
if (!Strings::convert(propValue, intVal)) {
throw std::invalid_argument("Error interpreting string '" + propValue +
"' as NumberType Int.");
}
} else if (!Strings::convert(propValue, dblVal)) {
throw std::invalid_argument("Error interpreting string '" + propValue +
"' as NumberType Double.");
}
} else {
if (Strings::convert(propValue, intVal)) {
value_is_int = true;
} else if (!Strings::convert(propValue, dblVal)) {
throw std::invalid_argument("Error interpreting string '" + propValue +
"' as a number.");
}
if (propType == numberLogOption) {
theRun.addLogData(new PropertyWithValue<int>(propName, intVal));
else
theRun.addLogData(new PropertyWithValue<double>(propName, dblVal));
} else if (propType == numberSeriesLogOption) {
Kernel::DateAndTime startTime;
try {
startTime = theRun.startTime();
} catch (std::runtime_error &) {
// Swallow the error - startTime will just be 0
}
auto tsp = new TimeSeriesProperty<int>(propName);
tsp->addValue(startTime, intVal);
theRun.addLogData(tsp);
auto tsp = new TimeSeriesProperty<double>(propName);
tsp->addValue(startTime, dblVal);
theRun.addLogData(tsp);
}
Janik Zikovsky
committed
}
theRun.getProperty(propName)->setUnits(propUnit);
Steve Williams
committed
}
} // namespace Algorithms
} // namespace Mantid