forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PropertyNexus.cpp
249 lines (222 loc) · 7.96 KB
/
PropertyNexus.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// 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 +
#include "MantidKernel/PropertyNexus.h"
// clang-format off
#include <nexus/NeXusFile.hpp>
#include <nexus/NeXusException.hpp>
// clang-format on
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/Property.h"
#include "MantidKernel/PropertyWithValue.h"
#include "MantidKernel/TimeSeriesProperty.h"
// PropertyWithValue implementation
#include "MantidKernel/PropertyWithValue.tcc"
#include <boost/algorithm/string/split.hpp>
#include <memory>
using namespace Mantid::Kernel;
using namespace ::NeXus;
#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable : 4805)
#endif
namespace Mantid::Kernel::PropertyNexus {
namespace {
//----------------------------------------------------------------------------------------------
/** Helper method to create a property
*
* @param file :: nexus file handle
* @param name :: name of the property being created
* @param times :: vector of times, empty = single property with value
* @return Property *
*/
template <typename NumT>
std::unique_ptr<Property> makeProperty(::NeXus::File *file, const std::string &name,
const std::vector<Types::Core::DateAndTime> ×) {
std::vector<NumT> values;
file->getData(values);
if (times.empty()) {
if (values.size() == 1) {
return std::make_unique<PropertyWithValue<NumT>>(name, values[0]);
} else {
return std::make_unique<ArrayProperty<NumT>>(name, std::move(values));
}
} else {
auto prop = std::make_unique<TimeSeriesProperty<NumT>>(name);
prop->addValues(times, values);
return std::unique_ptr<Property>(std::move(prop));
}
}
/** Helper method to create a time series property from a boolean
*
* @param file :: nexus file handle
* @param name :: name of the property being created
* @param times :: vector of times, empty = single property with value
* @return Property *
*/
std::unique_ptr<Property> makeTimeSeriesBoolProperty(::NeXus::File *file, const std::string &name,
const std::vector<Types::Core::DateAndTime> ×) {
std::vector<uint8_t> savedValues;
file->getData(savedValues);
const size_t nvals = savedValues.size();
std::vector<bool> realValues(nvals);
for (size_t i = 0; i < nvals; ++i) {
realValues[i] = (savedValues[i] != 0);
}
auto prop = std::make_unique<TimeSeriesProperty<bool>>(name);
prop->addValues(times, realValues);
return std::unique_ptr<Property>(std::move(prop));
}
/** Make a string/vector\<string\> property */
std::unique_ptr<Property> makeStringProperty(::NeXus::File *file, const std::string &name,
const std::vector<Types::Core::DateAndTime> ×) {
std::vector<std::string> values;
if (times.empty()) {
std::string bigString = file->getStrData();
return std::make_unique<PropertyWithValue<std::string>>(name, bigString);
} else {
if (file->getInfo().dims.size() != 2)
throw std::runtime_error("NXlog loading failed on field " + name + ". Expected rank 2.");
int64_t numStrings = file->getInfo().dims[0];
int64_t span = file->getInfo().dims[1];
auto data = std::make_unique<char[]>(numStrings * span);
file->getData(data.get());
values.reserve(static_cast<size_t>(numStrings));
for (int64_t i = 0; i < numStrings; i++)
values.emplace_back(data.get() + i * span);
auto prop = std::make_unique<TimeSeriesProperty<std::string>>(name);
prop->addValues(times, values);
return std::unique_ptr<Property>(std::move(prop));
}
}
/**
* Common function to populate "time" and "start" entries from NeXus file
*/
void getTimeAndStart(::NeXus::File *file, std::vector<double> &timeSec, std::string &startStr) {
file->openData("time");
file->getData(timeSec);
// Optionally get a start
try {
file->getAttr("start", startStr);
} catch (::NeXus::Exception &) {
}
file->closeData();
}
/**
* @brief Common function used by loadProperty overloads populating start and units if required
* @param file in
* @param group in
* @param timeSec in
* @param startStr out
* @param unitsStr out
* @return std::unique_ptr<Property>
*/
std::unique_ptr<Property> loadPropertyCommon(::NeXus::File *file, const std::string &group,
const std::vector<double> &timeSec, std::string &startStr) {
// Check the type. Boolean stored as UINT8
bool typeIsBool(false);
// Check for boolean attribute
if (file->hasAttr("boolean")) {
typeIsBool = true;
}
std::vector<Types::Core::DateAndTime> times;
if (!timeSec.empty()) {
// Use a default start time
if (startStr.empty())
startStr = "2000-01-01T00:00:00";
// Convert time in seconds to DateAndTime
Types::Core::DateAndTime start(startStr);
times.reserve(timeSec.size());
std::transform(timeSec.cbegin(), timeSec.cend(), std::back_inserter(times),
[&start](const auto &time) { return start + time; });
}
file->openData("value");
std::unique_ptr<Property> retVal = nullptr;
switch (file->getInfo().type) {
case ::NeXus::FLOAT32:
retVal = makeProperty<float>(file, group, times);
break;
case ::NeXus::FLOAT64:
retVal = makeProperty<double>(file, group, times);
break;
case ::NeXus::INT32:
retVal = makeProperty<int32_t>(file, group, times);
break;
case ::NeXus::UINT32:
retVal = makeProperty<uint32_t>(file, group, times);
break;
case ::NeXus::INT64:
retVal = makeProperty<int64_t>(file, group, times);
break;
case ::NeXus::UINT64:
retVal = makeProperty<uint64_t>(file, group, times);
break;
case ::NeXus::CHAR:
retVal = makeStringProperty(file, group, times);
break;
case ::NeXus::UINT8:
if (typeIsBool)
retVal = makeTimeSeriesBoolProperty(file, group, times);
break;
case ::NeXus::INT8:
case ::NeXus::INT16:
case ::NeXus::UINT16:
retVal = nullptr;
break;
}
std::string unitsStr;
if (file->hasAttr("units")) {
try {
file->getAttr("units", unitsStr);
} catch (::NeXus::Exception &) {
}
}
file->closeData();
file->closeGroup();
// add units
if (retVal)
retVal->setUnits(unitsStr);
return retVal;
}
} // namespace
//----------------------------------------------------------------------------------------------
std::unique_ptr<Property> loadProperty(::NeXus::File *file, const std::string &group,
const Mantid::Kernel::NexusHDF5Descriptor &fileInfo, const std::string &prefix) {
file->openGroup(group, "NXlog");
// Times in second offsets
std::vector<double> timeSec;
std::string startStr;
// Check if the "time" field is present
if (fileInfo.isEntry(prefix + "/" + group + "/time")) {
getTimeAndStart(file, timeSec, startStr);
}
return loadPropertyCommon(file, group, timeSec, startStr);
}
//----------------------------------------------------------------------------------------------
/** Opens a NXlog group in a nexus file and
* creates the correct Property object from it.
*
* @param file :: NXS file handle
* @param group :: name of NXlog group to open
* @return Property pointer
*/
std::unique_ptr<Property> loadProperty(::NeXus::File *file, const std::string &group) {
file->openGroup(group, "NXlog");
// Times in second offsets
std::vector<double> timeSec;
std::string startStr;
// Get the entries so that you can check if the "time" field is present
std::map<std::string, std::string> entries = file->getEntries();
if (entries.find("time") != entries.end()) {
getTimeAndStart(file, timeSec, startStr);
}
return loadPropertyCommon(file, group, timeSec, startStr);
}
#ifdef _WIN32
#pragma warning(pop)
#endif
} // namespace Mantid::Kernel::PropertyNexus