Mantid
Loading...
Searching...
No Matches
LoadRawBin0.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8#include "LoadRaw/isisraw2.h"
18
19#include <Poco/Path.h>
20#include <cmath>
21#include <cstdio> //Required for gcc 4.4
22#include <memory>
23
24namespace Mantid::DataHandling {
25// Register the algorithm into the algorithm factory
26DECLARE_ALGORITHM(LoadRawBin0)
27
28using namespace Kernel;
29using namespace API;
30
33 : isisRaw(), m_filename(), m_numberOfSpectra(0), m_noTimeRegimes(0), m_cache_options(), m_specTimeRegimes(),
34 m_prog(0.0), m_lengthIn(0), m_perioids(), m_total_specs(0), m_timeChannelsVec() {}
35
39 auto mustBePositive = std::make_shared<BoundedValidator<int>>();
40 mustBePositive->setLower(1);
41 declareProperty("SpectrumMin", 1, mustBePositive, "The number of the first spectrum to read.");
42 declareProperty("SpectrumMax", EMPTY_INT(), mustBePositive, "The number of the last spectrum to read.");
43 declareProperty(std::make_unique<ArrayProperty<specnum_t>>("SpectrumList"),
44 "A comma-separated list of individual spectra to read. Only used if "
45 "explicitly set.");
46}
47
56 // Retrieve the filename from the properties
57 m_filename = getPropertyValue("Filename");
58
59 bool bLoadlogFiles = getProperty("LoadLogFiles");
60
61 // open the raw file
62 FILE *file = openRawFile(m_filename);
63
64 // Need to check that the file is not a text file as the ISISRAW routines
65 // don't deal with these very well, i.e
66 // reading continues until a bad_alloc is encountered.
68 g_log.error() << "File \"" << m_filename << "\" is not a valid RAW file.\n";
69 throw std::invalid_argument("Incorrect file type encountered.");
70 }
71 std::string title;
72 readTitle(file, title);
73
75
78
79 // to validate the optional parameters, if set
81
82 // Calculate the size of a workspace, given its number of periods & spectra to
83 // read
85
86 // no real X values for bin 0,so initialize this to zero
87 auto channelsVec = std::make_shared<HistogramData::HistogramX>(1, 0);
88 m_timeChannelsVec.emplace_back(channelsVec);
89
90 auto histTotal = static_cast<double>(m_total_specs * m_numberOfPeriods);
91 int64_t histCurrent = -1;
92
93 // Create the 2D workspace for the output xlength and ylength is one
94 DataObjects::Workspace2D_sptr localWorkspace = createWorkspace(m_total_specs, 1, 1, title);
95 Run &run = localWorkspace->mutableRun();
96 if (bLoadlogFiles) {
97 runLoadLog(m_filename, localWorkspace, 0.0, 0.0);
98 const int period_number = 1;
99 createPeriodLogs(period_number, localWorkspace);
100 }
101 // Set the total proton charge for this run
102 setProtonCharge(run);
103
105 setWorkspaceProperty("OutputWorkspace", title, ws_grp, localWorkspace, m_numberOfPeriods, false, this);
106
107 // Loop over the number of periods in the raw file, putting each period in a
108 // separate workspace
109 for (int period = 0; period < m_numberOfPeriods; ++period) {
110 if (period > 0) {
111 localWorkspace = createWorkspace(localWorkspace);
112
113 if (bLoadlogFiles) {
114 // remove previous period data
115 std::stringstream prevPeriod;
116 prevPeriod << "PERIOD " << (period);
117 Run &runObj = localWorkspace->mutableRun();
118 runObj.removeLogData(prevPeriod.str());
119 runObj.removeLogData("current_period");
120 // add current period data
121 const int period_number = period + 1;
122 createPeriodLogs(period_number, localWorkspace);
123 }
124 }
125
126 const int64_t periodTimesNSpectraP1 = period * (static_cast<int64_t>(m_numberOfSpectra) + 1);
127 skipData(file, periodTimesNSpectraP1);
128 int64_t wsIndex = 0;
129 for (specnum_t i = 1; i <= m_numberOfSpectra; ++i) {
130 int64_t histToRead = i + periodTimesNSpectraP1;
131 if ((i >= m_spec_min && i < m_spec_max) ||
132 (m_list && find(m_spec_list.begin(), m_spec_list.end(), i) != m_spec_list.end())) {
133 progress(m_prog, "Reading raw file data...");
134 // readData(file, histToRead);
135 // read spectrum
136 if (!readData(file, histToRead)) {
137 throw std::runtime_error("Error reading raw file");
138 }
139 int64_t binStart = 0;
140 setWorkspaceData(localWorkspace, m_timeChannelsVec, wsIndex, i, m_noTimeRegimes, 1, binStart);
141 ++wsIndex;
142
143 if (m_numberOfPeriods == 1) {
144 if (++histCurrent % 100 == 0) {
145 m_prog = double(histCurrent) / histTotal;
146 }
148 }
149
150 } else {
151 skipData(file, histToRead);
152 }
153 }
154
155 if (m_numberOfPeriods > 1) {
156 setWorkspaceProperty(localWorkspace, ws_grp, period, false, this);
157 // progress for workspace groups
158 m_prog = static_cast<double>(period) / static_cast<double>(m_numberOfPeriods - 1);
159 }
160
161 } // loop over periods
162 // Clean up
163 isisRaw.reset();
164 fclose(file);
165}
166
169 // read in the settings passed to the algorithm
170 m_spec_list = getProperty("SpectrumList");
171 m_spec_max = getProperty("SpectrumMax");
172 m_spec_min = getProperty("SpectrumMin");
173}
174
175} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
void interruption_point()
This is called during long-running operations, and check if the algorithm has requested that it be ca...
Definition: Algorithm.cpp:1687
void removeLogData(const std::string &name, const bool delproperty=true)
Remove a named log entry.
Definition: LogManager.h:140
This class stores information regarding an experimental run as a series of log entries.
Definition: Run.h:38
std::shared_ptr< ISISRAW2 > isisRaw
ISISRAW class instance which does raw file reading.
Definition: LoadRawBin0.h:80
std::string m_filename
The name and path of the input file.
Definition: LoadRawBin0.h:82
int64_t m_noTimeRegimes
number of time regime
Definition: LoadRawBin0.h:87
double m_prog
The current value of the progress counter.
Definition: LoadRawBin0.h:94
void init() override
Overwrites Algorithm method.
Definition: LoadRawBin0.cpp:37
int64_t m_lengthIn
Read in the time bin boundaries.
Definition: LoadRawBin0.h:97
std::vector< std::shared_ptr< HistogramData::HistogramX > > m_timeChannelsVec
time channel vector
Definition: LoadRawBin0.h:104
void exec() override
Overwrites Algorithm method.
Definition: LoadRawBin0.cpp:55
specnum_t m_numberOfSpectra
The number of spectra in the raw file.
Definition: LoadRawBin0.h:85
LoadRawBin0()
Default constructor.
Definition: LoadRawBin0.cpp:32
void setOptionalProperties()
This sets the optional property to the LoadRawHelper class.
specnum_t m_total_specs
total number of specs
Definition: LoadRawBin0.h:102
void readTitle(FILE *file, std::string &title)
Reads title from the isisraw class.
void skipData(FILE *file, int hist)
skips histrogram data from raw file.
static void setWorkspaceProperty(const std::string &propertyName, const std::string &title, const API::WorkspaceGroup_sptr &grpws_sptr, const DataObjects::Workspace2D_sptr &ws_sptr, int64_t numberOfPeriods, bool bMonitor, API::Algorithm *const pAlg)
sets the workspace property
static DataObjects::Workspace2D_sptr createWorkspace(const DataObjects::Workspace2D_sptr &ws_sptr, int64_t nVectors=-1, int64_t xLengthIn=-1, int64_t yLengthIn=-1)
creates shared pointer to workspace from parent workspace
void init() override
Overwrites Algorithm method.
specnum_t m_spec_max
The value of the spectrum_max property.
std::vector< specnum_t > m_spec_list
The value of the spectrum_list property.
bool readData(FILE *file, int hist)
reads data
void createPeriodLogs(int64_t period, const DataObjects::Workspace2D_sptr &local_workspace)
Create the period specific logs.
specnum_t m_spec_min
The value of the spectrum_min property.
void runLoadLog(const std::string &fileName, const DataObjects::Workspace2D_sptr &, double, double)
load log algorithm
FILE * openRawFile(const std::string &fileName)
Opens Raw File.
void readworkspaceParameters(specnum_t &numberOfSpectra, int &numberOfPeriods, int64_t &lengthIn, int64_t &noTimeRegimes)
reads workspace parameters like number of histograms,size of vectors etc
bool m_list
Has the spectrum_list property been set?
void setProtonCharge(API::Run &run)
set proton charge
static API::WorkspaceGroup_sptr createGroupWorkspace()
creates shared pointer to group workspace
specnum_t calculateWorkspaceSize()
calculate workspace size
void setWorkspaceData(const DataObjects::Workspace2D_sptr &newWorkspace, const std::vector< std::shared_ptr< HistogramData::HistogramX > > &timeChannelsVec, int64_t wsIndex, specnum_t nspecNum, int64_t noTimeRegimes, int64_t lengthIn, int64_t binStart)
This method sets the raw file data to workspace vectors.
void checkOptionalProperties()
Validates the optional 'spectra to read' properties, if they have been set.
int m_numberOfPeriods
The number of periods in the raw file.
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
bool isAscii() const
Returns true if the descriptor is looking at an ascii file.
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
std::shared_ptr< WorkspaceGroup > WorkspaceGroup_sptr
shared pointer to Mantid::API::WorkspaceGroup
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< Workspace2D > Workspace2D_sptr
shared pointer to Mantid::DataObjects::Workspace2D
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition: EmptyValues.h:25
int32_t specnum_t
Typedef for a spectrum Number.
Definition: IDTypes.h:16