-
Notifications
You must be signed in to change notification settings - Fork 121
/
LoadRawSpectrum0.cpp
152 lines (127 loc) · 5.27 KB
/
LoadRawSpectrum0.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
// 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 "MantidDataHandling/LoadRawSpectrum0.h"
#include "LoadRaw/isisraw2.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidDataHandling/LoadLog.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/UnitFactory.h"
#include <Poco/Path.h>
#include <cmath>
#include <cstdio> //Required for gcc 4.4
#include <memory>
namespace Mantid {
namespace DataHandling {
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(LoadRawSpectrum0)
using namespace Kernel;
using namespace API;
/// Constructor
LoadRawSpectrum0::LoadRawSpectrum0()
: isisRaw(), m_filename(), m_numberOfSpectra(0), m_cache_options(), m_specTimeRegimes(), m_prog(0.0), m_lengthIn(0),
m_noTimeRegimes(0) {}
/// Initialisation method.
void LoadRawSpectrum0::init() { LoadRawHelper::init(); }
/** Executes the algorithm. Reading in the file and creating and populating
* the output workspace
*
* @throw Exception::FileError If the RAW file cannot be found/opened
* @throw std::invalid_argument If the optional properties are set to invalid
*values
*/
void LoadRawSpectrum0::exec() {
// Retrieve the filename from the properties
m_filename = getPropertyValue("Filename");
bool bLoadlogFiles = getProperty("LoadLogFiles");
// open the raw file
FILE *file = openRawFile(m_filename);
// Need to check that the file is not a text file as the ISISRAW routines
// don't deal with these very well, i.e
// reading continues until a bad_alloc is encountered.
if (Kernel::FileDescriptor::isAscii(file)) {
g_log.error() << "File \"" << m_filename << "\" is not a valid RAW file.\n";
throw std::invalid_argument("Incorrect file type encountered.");
}
std::string title;
readTitle(file, title);
readworkspaceParameters(m_numberOfSpectra, m_numberOfPeriods, m_lengthIn, m_noTimeRegimes);
// Calculate the size of a workspace, given its number of periods & spectra to
// read
const int64_t total_specs = 1;
// Get the time channel array(s) and store in a vector inside a shared pointer
auto timeChannelsVec = getTimeChannels(m_noTimeRegimes, m_lengthIn);
auto histTotal = static_cast<double>(total_specs * m_numberOfPeriods);
int64_t histCurrent = -1;
// Create the 2D workspace for the output
DataObjects::Workspace2D_sptr localWorkspace = createWorkspace(total_specs, m_lengthIn, m_lengthIn - 1, title);
Run &run = localWorkspace->mutableRun();
if (bLoadlogFiles) {
runLoadLog(m_filename, localWorkspace, 0.0, 0.0);
const int period_number = 1;
createPeriodLogs(period_number, localWorkspace);
}
// Set the total proton charge for this run
setProtonCharge(run);
WorkspaceGroup_sptr wsgrp_sptr = createGroupWorkspace();
setWorkspaceProperty("OutputWorkspace", title, wsgrp_sptr, localWorkspace, m_numberOfPeriods, false, this);
// Loop over the number of periods in the raw file, putting each period in a
// separate workspace
for (int period = 0; period < m_numberOfPeriods; ++period) {
if (period > 0) {
localWorkspace =
std::dynamic_pointer_cast<DataObjects::Workspace2D>(WorkspaceFactory::Instance().create(localWorkspace));
if (bLoadlogFiles) {
// remove previous period data
std::stringstream prevPeriod;
prevPeriod << "PERIOD " << (period);
Run &runObj = localWorkspace->mutableRun();
runObj.removeLogData(prevPeriod.str());
runObj.removeLogData("current_period");
// add current period data
auto period_number = static_cast<int>(period + 1);
createPeriodLogs(period_number, localWorkspace);
}
// skip all spectra except the first one in each period
for (int i = 1; i <= m_numberOfSpectra; ++i) {
skipData(file, i + (period - 1) * (m_numberOfSpectra + 1));
}
}
int64_t wsIndex = 0;
// for each period read first spectrum
int64_t histToRead = period * (static_cast<int64_t>(m_numberOfSpectra) + 1);
progress(m_prog, "Reading raw file data...");
// isisRaw->readData(file, histToRead);
// readData(file, histToRead);
// read spectrum
if (!readData(file, histToRead)) {
throw std::runtime_error("Error reading raw file");
}
// set the workspace data
setWorkspaceData(localWorkspace, timeChannelsVec, wsIndex, 0, m_noTimeRegimes, m_lengthIn, 1);
if (m_numberOfPeriods == 1) {
if (++histCurrent % 100 == 0) {
m_prog = double(histCurrent) / histTotal;
}
interruption_point();
}
if (m_numberOfPeriods > 1) {
setWorkspaceProperty(localWorkspace, wsgrp_sptr, period, false, this);
// progress for workspace groups
m_prog = static_cast<double>(period) / static_cast<double>(m_numberOfPeriods - 1);
}
} // loop over periods
// Clean up
reset();
fclose(file);
}
} // namespace DataHandling
} // namespace Mantid