SetInstrumentParameter.cpp 5.6 KB
Newer Older
1
#include "MantidAlgorithms/SetInstrumentParameter.h"
2
#include "MantidAPI/InstrumentValidator.h"
3
#include "MantidAPI/MatrixWorkspace.h"
4
#include "MantidGeometry/Instrument.h"
5
6
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/ListValidator.h"
7
#include "MantidKernel/MandatoryValidator.h"
8
9
#include "MantidKernel/Strings.h"

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
namespace Mantid {
namespace Algorithms {

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SetInstrumentParameter)

using namespace Kernel;
using namespace Geometry;
using namespace API;

//----------------------------------------------------------------------------------------------
/** Constructor
 */
SetInstrumentParameter::SetInstrumentParameter() {}

//----------------------------------------------------------------------------------------------
/** Destructor
 */
SetInstrumentParameter::~SetInstrumentParameter() {}

//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string SetInstrumentParameter::name() const {
  return "SetInstrumentParameter";
34
}
35
36

/// Algorithm's version for identification. @see Algorithm::version
37
int SetInstrumentParameter::version() const { return 1; }
38
39
40
41
42
43
44
45
46
47
48
49

/// Algorithm's category for identification. @see Algorithm::category
const std::string SetInstrumentParameter::category() const {
  return "DataHandling\\Instrument";
}

//----------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
 */
void SetInstrumentParameter::init() {
50
51
52
53
  declareProperty(make_unique<WorkspaceProperty<>>(
                      "Workspace", "", Direction::InOut,
                      boost::make_shared<InstrumentValidator>()),
                  "Workspace to add the log entry to");
54
55
56
  declareProperty("ComponentName", "", "The name of the component to attach "
                                       "the parameter to. Default: the whole "
                                       "instrument");
57
  declareProperty(make_unique<ArrayProperty<detid_t>>("DetectorList"),
58
59
60
61
62
63
                  "The detector ID list to attach the parameter to. If set "
                  "this will override any ComponentName");
  declareProperty("ParameterName", "",
                  boost::make_shared<MandatoryValidator<std::string>>(),
                  "The name that will identify the parameter");

64
  std::vector<std::string> propOptions{"String", "Number"};
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
  declareProperty("ParameterType", "String",
                  boost::make_shared<StringListValidator>(propOptions),
                  "The type that the parameter value will be.");

  declareProperty("Value", "", "The content of the Parameter");
}

//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
 */
void SetInstrumentParameter::exec() {
  // A pointer to the workspace to add a log to
  MatrixWorkspace_sptr ws = getProperty("Workspace");

  // get the data that the user wants to add
  std::string cmptName = getProperty("ComponentName");
  const std::vector<detid_t> detectorList = getProperty("DetectorList");
  std::string paramName = getProperty("ParameterName");
  std::string paramType = getProperty("ParameterType");
  std::string paramValue = getPropertyValue("Value");

  Strings::strip(cmptName);
  Strings::strip(paramName);
  Strings::strip(paramValue);

  auto inst = ws->getInstrument();
  std::vector<IDetector_const_sptr> dets;
  std::vector<IComponent_const_sptr> cmptList;
  // set default to whole instrument
  cmptList.push_back(inst);

  if (!detectorList.empty()) {
    dets = inst->getDetectors(detectorList);
  } else if (cmptName.length() > 0) {
    // get all matching cmpts
    cmptList = inst->getAllComponentsWithName(cmptName);
101
102
  }

103
104
  auto &paramMap = ws->instrumentParameters();
  if (!dets.empty()) {
105
106
    for (auto &det : dets) {
      addParameter(paramMap, det.get(), paramName, paramType, paramValue);
107
    }
108
  } else {
109
    if (!cmptList.empty()) {
Hahn, Steven's avatar
Hahn, Steven committed
110
111
      for (auto &cmpt : cmptList) {
        addParameter(paramMap, cmpt.get(), paramName, paramType, paramValue);
112
      }
113
114
    } else {
      g_log.warning("Could not find the component requested.");
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
}

/** Adds a parameter to the component
 *  @param pmap  The parameter map to use
 *  @param cmptId  The component id to add the the parameter to
 *  @param paramName  The parameter name to use
 *  @param paramType  The parameter type
 *  @param paramValue  The parameter value as a string
 */
void SetInstrumentParameter::addParameter(
    Mantid::Geometry::ParameterMap &pmap,
    const Mantid::Geometry::IComponent *cmptId, const std::string &paramName,
    const std::string &paramType, const std::string &paramValue) const {

  // remove existing parameters first
  pmap.clearParametersByName(paramName, cmptId);
  // then add the new one
  if (paramType == "String") {
    pmap.addString(cmptId, paramName, paramValue);
  } else if (paramType == "Number") {
    bool valueIsInt(false);
    int intVal;
    double dblVal;
    if (Strings::convert(paramValue, intVal)) {
      valueIsInt = true;
    } else if (!Strings::convert(paramValue, dblVal)) {
      throw std::invalid_argument("Error interpreting string '" + paramValue +
                                  "' as a number.");
145
    }
146
147
148

    if (valueIsInt)
      pmap.addInt(cmptId, paramName, intVal);
149
    else
150
151
152
      pmap.addDouble(cmptId, paramName, dblVal);
  } else {
    throw std::invalid_argument("Unknown Parameter Type " + paramType);
153
  }
154
}
155
156

} // namespace Algorithms
157
} // namespace Mantid