-
Notifications
You must be signed in to change notification settings - Fork 122
/
AddSampleLog.cpp
123 lines (106 loc) · 3.82 KB
/
AddSampleLog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*WIKI*
Workspaces contain information in logs. Often these detail what happened to the sample during the experiment. This algorithm allows one named log to be entered.
The log can be either a String, a Number, or a Number Series. If you select Number Series, the workspace start time will be used as the time of the log entry, and the number in the text used as the (only) value.
If the LogText contains a numeric value, the created log will be of integer type if an integer is passed and floating point (double) otherwise. This applies to both the Number & Number Series options.
*WIKI*/
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAlgorithms/AddSampleLog.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidKernel/Strings.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/PropertyWithValue.h"
#include <string>
namespace Mantid
{
namespace Algorithms
{
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(AddSampleLog)
/// Sets documentation strings for this algorithm
void AddSampleLog::initDocs()
{
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.");
}
using namespace Kernel;
using namespace API;
void AddSampleLog::init()
{
declareProperty(new WorkspaceProperty<>("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");
declareProperty("LogText", "",
"The content of the log");
std::vector<std::string> propOptions;
propOptions.push_back("String");
propOptions.push_back("Number");
propOptions.push_back("Number Series");
declareProperty("LogType", "String",boost::make_shared<StringListValidator>(propOptions),
"The type that the log data will be."
);
}
void AddSampleLog::exec()
{
// A pointer to the workspace to add a log to
MatrixWorkspace_sptr wSpace = getProperty("Workspace");
// we're going to edit the workspaces run details so get a non-const reference to it
Run &theRun = wSpace->mutableRun();
// get the data that the user wants to add
std::string propName = getProperty("LogName");
std::string propValue = getProperty("LogText");
std::string propType = getPropertyValue("LogType");
// Remove any existing log
if (theRun.hasProperty(propName))
{
theRun.removeLogData(propName);
}
if (propType == "String")
{
theRun.addLogData(new PropertyWithValue<std::string>(propName, propValue));
return;
}
bool valueIsInt(false);
int intVal;
double dblVal;
if ( Strings::convert(propValue, intVal) )
{
valueIsInt = true;
}
else if ( !Strings::convert(propValue, dblVal) )
{
throw std::invalid_argument("Error interpreting string '" + propValue + "' as a number.");
}
if (propType == "Number")
{
if (valueIsInt) theRun.addLogData(new PropertyWithValue<int>(propName, intVal));
else theRun.addLogData(new PropertyWithValue<double>(propName, dblVal));
}
else if (propType == "Number Series")
{
Kernel::DateAndTime startTime;
try {
startTime = theRun.startTime();
} catch (std::runtime_error&) {
// Swallow the error - startTime will just be 0
}
if (valueIsInt)
{
auto tsp = new TimeSeriesProperty<int>(propName);
tsp->addValue(startTime, intVal);
theRun.addLogData(tsp);
}
else
{
auto tsp = new TimeSeriesProperty<double>(propName);
tsp->addValue(startTime, dblVal);
theRun.addLogData(tsp);
}
}
}
} // namespace Algorithms
} // namespace Mantid