9#include <boost/scoped_array.hpp>
10#include <nexus/NeXusException.hpp>
18const string NXDATA(
"NXdata");
20const string NXENTRY(
"NXentry");
22const string NXLOG(
"NXlog");
24const string START_TIME(
"start_time");
30const std::string PERIOD_SEQUENCES(
"period_sequences");
31const std::string PERIOD_TYPE(
"period_type");
32const std::string FRAMES_PERIOD_REQUESTED(
"frames_period_requested");
33const std::string FRAMES_PERIOD_RAW(
"frames_period_raw");
34const std::string PERIOD_OUTPUT(
"period_output");
35const std::string TOTAL_COUNTS_PERIOD(
"total_counts_period");
36const std::string PERIOD_LABELS(
"period_labels");
39template <
class T> std::string convertVectorToString(T values,
const std::string &delim) {
40 std::string result(
"");
41 if (!values.empty()) {
42 for (
const auto &
value : values)
44 result.erase(result.length() - 1);
58 std::map<string, string> entries = handle.getEntries();
60 std::find_if(entries.cbegin(), entries.cend(), [](
const auto entry) { return entry.second == NXENTRY; });
61 if (entry == entries.cend())
62 throw std::runtime_error(
"Failed to find NXentry");
63 handle.openGroup(entry->first, NXENTRY);
83 NeXus::File handle(filename, NXACC_READ);
87 std::vector<string> nxdataname;
88 std::map<string, string> entries = handle.getEntries();
89 for (
auto &entry : entries) {
90 if (entry.second == NXDATA) {
91 nxdataname.emplace_back(entry.first);
94 handle.openGroup(nxdataname.front(), NXDATA);
100 handle.openData(
"counts");
101 info = handle.getInfo();
102 t_ntc1 =
static_cast<int>(info.dims[1]);
103 t_nsp1 =
static_cast<int>(info.dims[0]);
109 handle.openData(
"grouping");
110 info = handle.getInfo();
115 g_log.
debug(
"Muon nexus file does not contain grouping info");
119 handle.openData(
"corrected_time");
120 info = handle.getInfo();
129 handle.openGroup(
"instrument",
"NXinstrument");
134 handle.openGroup(
"beam",
"NXbeam");
138 g_log.
debug(
"Muon nexus file does not contain beam info");
145 entries = handle.getEntries();
147 if (std::any_of(entries.cbegin(), entries.cend(),
148 [](
const auto &entry) { return entry.first ==
"switching_states"; })) {
150 handle.readData(
"switching_states", ssPeriods);
171 auto parsePeriod = [&handle](
const std::string &logName,
auto &destAttr) {
173 std::remove_reference_t<
decltype(destAttr)> tempAttr;
174 handle.readData(logName, tempAttr);
177 g_log.
debug(
"Muon nexus file does not contain " + logName);
181 auto parseIntVector = [&handle](
const std::string &logName,
auto &destAttr) {
183 std::vector<int> tempIntVector;
184 handle.readData(logName, tempIntVector);
185 destAttr = convertVectorToString(tempIntVector,
";");
187 g_log.
debug(
"Muon nexus file does not contain " + logName);
191 auto parseFloatVector = [&handle](
const std::string &logName,
auto &destAttr) {
193 std::vector<float> tempFloatVector;
194 handle.readData(logName, tempFloatVector);
195 destAttr = convertVectorToString(tempFloatVector,
";");
197 g_log.
debug(
"Muon nexus file does not contain " + logName);
219 for (
int i = 0; i < nbnds - 1; i++)
221 timebnds[nbnds - 1] = timebnds[nbnds - 2] + float(2.0) * binHalfWidth;
241 NeXus::File handle(filename, NXACC_READ);
247 std::map<string, string> entries = handle.getEntries();
248 for (
auto &entrie : entries) {
249 string nxname = entrie.first;
250 string nxclass = entrie.second;
252 if (nxclass == NXLOG) {
253 handle.openGroup(nxname, nxclass);
261 if (nxclass ==
"NXSample" || nxclass ==
"NXsample")
263 handle.openGroup(nxname, nxclass);
267 if (nxname == START_TIME) {
271 boost::posix_time::ptime pt = boost::posix_time::time_from_string(
m_startTime);
280 const string NAME(
"name");
281 const string VALUES(
"values");
282 const string TIME(
"time");
286 handle.readData(NAME, dataName);
290 handle.openData(VALUES);
291 }
catch (NeXus::Exception &) {
292 g_log.
warning() <<
"No " << VALUES <<
" set in " << handle.getPath() <<
"\n";
296 std::vector<float> values;
297 std::vector<std::string> stringValues;
298 bool isNumeric(
false);
299 std::string units =
"";
301 NeXus::Info info = handle.getInfo();
302 if (info.type == NX_FLOAT32 && info.dims.size() == 1) {
304 boost::scoped_array<float> dataVals(
new float[info.dims[0]]);
305 handle.getAttr(
"units", units);
306 handle.getData(dataVals.get());
307 values.assign(dataVals.get(), dataVals.get() + info.dims[0]);
308 stringValues.resize(info.dims[0]);
309 }
else if (info.type == NX_CHAR && info.dims.size() == 2) {
310 boost::scoped_array<char> dataVals(
new char[info.dims[0] * info.dims[1] + 1]);
311 handle.getAttr(
"units", units);
312 handle.getData(dataVals.get());
313 dataVals[info.dims[0] * info.dims[1]] = 0;
314 for (
int i = 0; i < info.dims[0]; ++i) {
315 std::string str(&dataVals[i * info.dims[1]], &dataVals[(i + 1) * info.dims[1]]);
316 stringValues.emplace_back(str);
318 values.resize(info.dims[0]);
321 values.resize(info.dims[0]);
322 stringValues.resize(info.dims[0]);
328 handle.openData(TIME);
329 }
catch (NeXus::Exception &) {
330 g_log.
warning() <<
"No " << TIME <<
" set in " << handle.getPath() <<
"\n";
334 info = handle.getInfo();
335 boost::scoped_array<float> timeVals(
new float[info.dims[0]]);
336 if (info.type == NX_FLOAT32 && info.dims.size() == 1) {
337 handle.getData(timeVals.get());
339 throw std::runtime_error(
"Error in MuonNexusReader: expected float array for log times");
347 std::vector<float>
tmp(timeVals.get(), timeVals.get() + info.dims[0]);
359 double time =
m_logTimes[logNumber][logSequence];
371 const double time =
m_logTimes[logNumber][logSequence];
374 if (logSequence <
int(strings.size())) {
375 value = strings[logSequence];
double value
The value of the point.
The Logger class is in charge of the publishing messages from the framework through various channels.
void debug(const std::string &msg)
Logs at debug level.
void warning(const std::string &msg)
Logs at warning level.
std::string m_framesPeriodsRaw
std::vector< std::string > m_logNames
stores name read from file
std::vector< std::vector< std::string > > m_logStringValues
array of string values for i'th NXlog section
std::string m_periodsOutput
int numberOfLogs() const
Number of NXlog sections read from file.
std::string m_nexusSampleName
sample name read from Nexus
std::string m_framesPeriodsRequested
std::vector< std::vector< float > > m_logTimes
arrys of times for i'th NXlog section
std::time_t to_time_t(const boost::posix_time::ptime &t)
std::string logUnits(const int i) const
std::string m_periodNames
int m_nexusLogCount
number of NXlog sections read from file
bool readMuonLogData(NeXus::File &handle)
method to read the fields of open NXlog section
std::time_t m_startTime_time_t
file to base all NXlog times on
std::vector< float > m_correctedTimes
temp store for corrected times
void readLogData(const std::string &filename)
read log data
void getTimeChannels(float *timebnds, const int &nbnds) const
get time bin boundaries return sample name
std::vector< bool > m_logType
true if i'th log is numeric
int t_nper
number of periods in file (=1 at present)
void readFromFile(const std::string &filename)
read histogram data
int getLogLength(const int i) const
Lenght of i'th log.
std::vector< int > m_detectorGroupings
detector grouping info
bool logTypeNumeric(const int i) const
true if i'th log is of numeric type
std::string m_periodTypes
void openFirstNXentry(NeXus::File &handle)
Open the first NXentry of the supplied nexus file.
std::vector< std::string > m_logUnits
std::string m_nexusInstrumentName
name read from nexus file
std::string m_startTime
string startTime which must be read from Nexus
int t_ntc1
number of time channels in time regime 1
void readPeriodInfo(NeXus::File &handle)
Try to read in vairous peices of period information.
int t_nsp1
number of spectra in time regime 1
void getLogValues(const int &logNumber, const int &logSequence, std::time_t &logTime, double &value)
get logSequence pair of logNumber log
std::string getInstrumentName() const
return instrument name
std::string m_periodsCounts
std::vector< int > m_counts
temp store of histogram data
int m_numDetectors
detector count
std::string getLogName(const int i) const
Name of i'th log.
std::vector< std::vector< float > > m_logValues
array of values for i'th NXlog section
void getLogStringValues(const int &logNumber, const int &logSequence, std::time_t &logTime, std::string &value)
get logSequence pair of logNumber string log
Kernel::Logger g_log("ExperimentInfo")
static logger object
Helper class which provides the Collimation Length for SANS instruments.
std::string to_string(const wide_integer< Bits, Signed > &n)