-
Notifications
You must be signed in to change notification settings - Fork 122
/
AddSampleLog.cpp
148 lines (128 loc) · 5.11 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAlgorithms/AddSampleLog.h"
#include "MantidAPI/ExperimentInfo.h"
#include "MantidAPI/Run.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 {
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 {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(AddSampleLog)
using namespace Kernel;
using namespace API;
void AddSampleLog::init() {
declareProperty(Kernel::make_unique<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");
declareProperty("LogText", "", "The content of the log");
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'.");
}
void AddSampleLog::exec() {
// 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();
// get the data that the user wants to add
std::string propName = getProperty("LogName");
std::string propValue = getProperty("LogText");
std::string propUnit = getProperty("LogUnit");
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'");
}
// Remove any existing log
if (theRun.hasProperty(propName)) {
theRun.removeLogData(propName);
}
if (propType == stringLogOption) {
theRun.addLogData(new PropertyWithValue<std::string>(propName, propValue));
theRun.getProperty(propName)->setUnits(propUnit);
return;
}
int intVal;
double dblVal;
bool value_is_int = false;
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) {
if (value_is_int)
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
}
if (value_is_int) {
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);
}
}
theRun.getProperty(propName)->setUnits(propUnit);
}
} // namespace Algorithms
} // namespace Mantid