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 <cmath>
20#include <cstdio> //Required for gcc 4.4
21#include <memory>
22
23namespace Mantid::DataHandling {
24// Register the algorithm into the algorithm factory
25DECLARE_ALGORITHM(LoadRawBin0)
26
27using namespace Kernel;
28using namespace API;
29
32 : isisRaw(), m_filename(), m_numberOfSpectra(0), m_noTimeRegimes(0), m_cache_options(), m_specTimeRegimes(),
33 m_prog(0.0), m_lengthIn(0), m_perioids(), m_total_specs(0), m_timeChannelsVec() {}
34
38 auto mustBePositive = std::make_shared<BoundedValidator<int>>();
39 mustBePositive->setLower(1);
40 declareProperty("SpectrumMin", 1, mustBePositive, "The number of the first spectrum to read.");
41 declareProperty("SpectrumMax", EMPTY_INT(), mustBePositive, "The number of the last spectrum to read.");
42 declareProperty(std::make_unique<ArrayProperty<specnum_t>>("SpectrumList"),
43 "A comma-separated list of individual spectra to read. Only used if "
44 "explicitly set.");
45}
46
55 // Retrieve the filename from the properties
56 m_filename = getPropertyValue("Filename");
57
58 bool bLoadlogFiles = getProperty("LoadLogFiles");
59
60 // open the raw file
61 FILE *file = openRawFile(m_filename);
62
63 // Need to check that the file is not a text file as the ISISRAW routines
64 // don't deal with these very well, i.e
65 // reading continues until a bad_alloc is encountered.
67 g_log.error() << "File \"" << m_filename << "\" is not a valid RAW file.\n";
68 throw std::invalid_argument("Incorrect file type encountered.");
69 }
70 std::string title;
71 readTitle(file, title);
72
74
77
78 // to validate the optional parameters, if set
80
81 // Calculate the size of a workspace, given its number of periods & spectra to
82 // read
84
85 // no real X values for bin 0,so initialize this to zero
86 auto channelsVec = std::make_shared<HistogramData::HistogramX>(1, 0);
87 m_timeChannelsVec.emplace_back(channelsVec);
88
89 auto histTotal = static_cast<double>(m_total_specs * m_numberOfPeriods);
90 int64_t histCurrent = -1;
91
92 // Create the 2D workspace for the output xlength and ylength is one
93 DataObjects::Workspace2D_sptr localWorkspace = createWorkspace(m_total_specs, 1, 1, title);
94 Run &run = localWorkspace->mutableRun();
95 if (bLoadlogFiles) {
96 runLoadLog(m_filename, localWorkspace, 0.0, 0.0);
97 const int period_number = 1;
98 createPeriodLogs(period_number, localWorkspace);
99 }
100 // Set the total proton charge for this run
101 setProtonCharge(run);
102
104 setWorkspaceProperty("OutputWorkspace", title, ws_grp, localWorkspace, m_numberOfPeriods, false, this);
105
106 // Loop over the number of periods in the raw file, putting each period in a
107 // separate workspace
108 for (int period = 0; period < m_numberOfPeriods; ++period) {
109 if (period > 0) {
110 localWorkspace = createWorkspace(localWorkspace);
111
112 if (bLoadlogFiles) {
113 // remove previous period data
114 std::stringstream prevPeriod;
115 prevPeriod << "PERIOD " << (period);
116 Run &runObj = localWorkspace->mutableRun();
117 runObj.removeLogData(prevPeriod.str());
118 runObj.removeLogData("current_period");
119 // add current period data
120 const int period_number = period + 1;
121 createPeriodLogs(period_number, localWorkspace);
122 }
123 }
124
125 const int64_t periodTimesNSpectraP1 = period * (static_cast<int64_t>(m_numberOfSpectra) + 1);
126 skipData(file, periodTimesNSpectraP1);
127 int64_t wsIndex = 0;
128 for (specnum_t i = 1; i <= m_numberOfSpectra; ++i) {
129 int64_t histToRead = i + periodTimesNSpectraP1;
130 if ((i >= m_spec_min && i < m_spec_max) ||
131 (m_list && find(m_spec_list.begin(), m_spec_list.end(), i) != m_spec_list.end())) {
132 progress(m_prog, "Reading raw file data...");
133 // readData(file, histToRead);
134 // read spectrum
135 if (!readData(file, histToRead)) {
136 throw std::runtime_error("Error reading raw file");
137 }
138 int64_t binStart = 0;
139 setWorkspaceData(localWorkspace, m_timeChannelsVec, wsIndex, i, m_noTimeRegimes, 1, binStart);
140 ++wsIndex;
141
142 if (m_numberOfPeriods == 1) {
143 if (++histCurrent % 100 == 0) {
144 m_prog = double(histCurrent) / histTotal;
145 }
147 }
148
149 } else {
150 skipData(file, histToRead);
151 }
152 }
153
154 if (m_numberOfPeriods > 1) {
155 setWorkspaceProperty(localWorkspace, ws_grp, period, false, this);
156 // progress for workspace groups
157 m_prog = static_cast<double>(period) / static_cast<double>(m_numberOfPeriods - 1);
158 }
159
160 } // loop over periods
161 // Clean up
162 isisRaw.reset();
163 fclose(file);
164}
165
168 // read in the settings passed to the algorithm
169 m_spec_list = getProperty("SpectrumList");
170 m_spec_max = getProperty("SpectrumMax");
171 m_spec_min = getProperty("SpectrumMin");
172}
173
174} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
void interruption_point()
This is called during long-running operations, and check if the algorithm has requested that it be ca...
void removeLogData(const std::string &name, const bool delproperty=true)
Remove a named log entry.
Definition LogManager.h:152
This class stores information regarding an experimental run as a series of log entries.
Definition Run.h:35
std::shared_ptr< ISISRAW2 > isisRaw
ISISRAW class instance which does raw file reading.
Definition LoadRawBin0.h:82
std::string m_filename
The name and path of the input file.
Definition LoadRawBin0.h:84
int64_t m_noTimeRegimes
number of time regime
Definition LoadRawBin0.h:89
double m_prog
The current value of the progress counter.
Definition LoadRawBin0.h:96
void init() override
Overwrites Algorithm method.
int64_t m_lengthIn
Read in the time bin boundaries.
Definition LoadRawBin0.h:99
std::vector< std::shared_ptr< HistogramData::HistogramX > > m_timeChannelsVec
time channel vector
void exec() override
Overwrites Algorithm method.
specnum_t m_numberOfSpectra
The number of spectra in the raw file.
Definition LoadRawBin0.h:87
LoadRawBin0()
Default constructor.
void setOptionalProperties()
This sets the optional property to the LoadRawHelper class.
specnum_t m_total_specs
total number of specs
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.
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:108
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:24
int32_t specnum_t
Typedef for a spectrum Number.
Definition IDTypes.h:14