forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FacilityInfo.cpp
215 lines (189 loc) · 7.73 KB
/
FacilityInfo.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include <algorithm>
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/FacilityInfo.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/Strings.h"
#include <boost/algorithm/string.hpp>
#include <memory>
#include "MantidKernel/StringTokenizer.h"
#include <Poco/DOM/Element.h>
#include <Poco/DOM/NodeList.h>
#include <Poco/DOM/Text.h>
using Poco::XML::Element;
namespace Mantid::Kernel {
namespace {
/// static logger
Logger g_log("FacilityInfo");
} // namespace
/** Constructor.
* @param elem :: The Poco::XML::Element to read the data from
* @throw std::runtime_error if name or file extensions are not defined
*/
FacilityInfo::FacilityInfo(const Poco::XML::Element *elem)
: m_catalogs(elem), m_name(elem->getAttribute("name")), m_timezone(), m_zeroPadding(0), m_delimiter(),
m_extensions(), m_archiveSearch(), m_instruments(), m_noFilePrefix(), m_multiFileLimit(100) {
// Fill the various fields from the XML
fillZeroPadding(elem);
fillDelimiter(elem);
fillExtensions(elem);
fillArchiveNames(elem);
fillTimezone(elem);
fillNoFilePrefix(elem);
fillMultiFileLimit(elem);
fillInstruments(elem); // Make sure this is last as it picks up some defaults
// that are set above
}
/// Called from constructor to fill zero padding field
void FacilityInfo::fillZeroPadding(const Poco::XML::Element *elem) {
std::string paddingStr = elem->getAttribute("zeropadding");
if (paddingStr.empty() || !Mantid::Kernel::Strings::convert(paddingStr, m_zeroPadding)) {
m_zeroPadding = 0;
}
}
/// Called from constructor to fill the noFilePrefix flag
void FacilityInfo::fillNoFilePrefix(const Poco::XML::Element *elem) {
std::string noFilePrefixStr = elem->getAttribute("nofileprefix");
m_noFilePrefix = (noFilePrefixStr == "True");
}
/// Called from constructor to fill the multifile limit
void FacilityInfo::fillMultiFileLimit(const Poco::XML::Element *elem) {
const std::string multiFileLimitStr = elem->getAttribute("multifilelimit");
if (!multiFileLimitStr.empty()) {
size_t limit;
if (Mantid::Kernel::Strings::convert(multiFileLimitStr, limit)) {
m_multiFileLimit = limit;
}
}
}
/// Called from constructor to fill default delimiter
void FacilityInfo::fillDelimiter(const Poco::XML::Element *elem) {
// The string to separate the instrument name and the run number.
m_delimiter = elem->getAttribute("delimiter");
}
/// Called from constructor to fill file extensions
void FacilityInfo::fillExtensions(const Poco::XML::Element *elem) {
std::string extsStr = elem->getAttribute("FileExtensions");
if (extsStr.empty()) {
g_log.error("No file extensions defined");
throw std::runtime_error("No file extensions defined");
}
using tokenizer = Mantid::Kernel::StringTokenizer;
tokenizer exts(extsStr, ",", tokenizer::TOK_IGNORE_EMPTY | tokenizer::TOK_TRIM);
for (const auto &ext : exts) {
addExtension(ext);
}
}
/**
* Add new extension. Adds both a lowercase and uppercase version
* @param ext :: File extension, including the dot, e.g. ".nxs" or ".raw"
*/
void FacilityInfo::addExtension(const std::string &ext) {
auto it = std::find(m_extensions.begin(), m_extensions.end(), ext);
if (it == m_extensions.end())
m_extensions.emplace_back(ext);
}
/// Called from constructor to fill archive interface names
void FacilityInfo::fillArchiveNames(const Poco::XML::Element *elem) {
Poco::AutoPtr<Poco::XML::NodeList> pNL_archives = elem->getElementsByTagName("archive");
if (pNL_archives->length() > 1) {
g_log.error("Facility must have only one archive tag");
throw std::runtime_error("Facility must have only one archive tag");
} else if (pNL_archives->length() == 1) {
Poco::AutoPtr<Poco::XML::NodeList> pNL_interfaces = elem->getElementsByTagName("archiveSearch");
for (unsigned int i = 0; i < pNL_interfaces->length(); ++i) {
auto *elemenent = dynamic_cast<Poco::XML::Element *>(pNL_interfaces->item(i));
std::string plugin = elemenent->getAttribute("plugin");
if (!plugin.empty()) {
m_archiveSearch.emplace_back(plugin);
}
}
}
}
void FacilityInfo::fillTimezone(const Poco::XML::Element *elem) {
Poco::AutoPtr<Poco::XML::NodeList> pNL_timezones = elem->getElementsByTagName("timezone");
if (pNL_timezones->length() == 0) {
g_log.notice() << "No timezone specified for " << m_name << " in Facilities.xml\n";
} else if (pNL_timezones->length() == 1) {
pNL_timezones = pNL_timezones->item(0)->childNodes();
if (pNL_timezones->length() > 0) {
auto *txt = dynamic_cast<Poco::XML::Text *>(pNL_timezones->item(0));
m_timezone = txt->getData();
}
} else {
throw std::runtime_error("Facility " + m_name + " has more than one timezone specified");
}
}
/// Called from constructor to fill instrument list
void FacilityInfo::fillInstruments(const Poco::XML::Element *elem) {
Poco::AutoPtr<Poco::XML::NodeList> pNL_instrument = elem->getElementsByTagName("instrument");
unsigned long n = pNL_instrument->length();
m_instruments.reserve(n);
for (unsigned long i = 0; i < n; ++i) {
auto *elemenent = dynamic_cast<Poco::XML::Element *>(pNL_instrument->item(i));
if (elemenent) {
try {
InstrumentInfo instr(this, elemenent);
m_instruments.emplace_back(instr);
} catch (std::runtime_error &e) { /*skip this instrument*/
g_log.warning("Failed to load instument for: " + m_name + ": " + e.what());
}
}
}
if (m_instruments.empty()) {
throw std::runtime_error("Facility " + m_name + " does not have any instruments;");
}
}
/**
* Returns instrument with given name
* @param iName Instrument name
* @return the instrument information object
* @throw NotFoundError if iName was not found
*/
const InstrumentInfo &FacilityInfo::instrument(std::string iName) const {
if (iName.empty()) {
iName = ConfigService::Instance().getString("default.instrument");
g_log.debug() << "Blank instrument specified, using default instrument of " << iName << ".\n";
if (iName.empty()) {
return m_instruments.front();
}
}
auto instrument = std::find_if(m_instruments.cbegin(), m_instruments.cend(),
[&iName](const auto &inst) { return boost::iequals(inst.name(), iName); });
// if unsuccessful try lookup by short name
if (instrument == m_instruments.cend()) {
instrument = std::find_if(m_instruments.cbegin(), m_instruments.cend(),
[&iName](const auto &inst) { return boost::iequals(inst.shortName(), iName); });
}
if (instrument != m_instruments.cend()) {
g_log.debug() << "Instrument '" << iName << "' found as " << instrument->name() << " at " << name() << ".\n";
return *instrument;
}
g_log.debug("Instrument " + iName + " not found in facility " + name());
throw Exception::NotFoundError("FacilityInfo", iName);
}
/**
* Returns a list of instruments of given technique
* @param tech :: Technique name
* @return a list of instrument information objects
*/
std::vector<InstrumentInfo> FacilityInfo::instruments(const std::string &tech) const {
std::vector<InstrumentInfo> out;
auto it = m_instruments.begin();
for (; it != m_instruments.end(); ++it) {
if (it->techniques().count(tech)) {
out.emplace_back(*it);
}
}
return out;
}
} // namespace Mantid::Kernel