Mantid
Loading...
Searching...
No Matches
LoadRawSpectrum0.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#include <Poco/Path.h>
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(LoadRawSpectrum0)
26
27using namespace Kernel;
28using namespace API;
29
32 : isisRaw(), m_filename(), m_numberOfSpectra(0), m_cache_options(), m_specTimeRegimes(), m_prog(0.0), m_lengthIn(0),
33 m_noTimeRegimes(0) {}
34
37
46 // Retrieve the filename from the properties
47 m_filename = getPropertyValue("Filename");
48
49 bool bLoadlogFiles = getProperty("LoadLogFiles");
50
51 // open the raw file
52 FILE *file = openRawFile(m_filename);
53
54 // Need to check that the file is not a text file as the ISISRAW routines
55 // don't deal with these very well, i.e
56 // reading continues until a bad_alloc is encountered.
58 g_log.error() << "File \"" << m_filename << "\" is not a valid RAW file.\n";
59 throw std::invalid_argument("Incorrect file type encountered.");
60 }
61
62 std::string title;
63 readTitle(file, title);
64
66
67 // Calculate the size of a workspace, given its number of periods & spectra to
68 // read
69 const int64_t total_specs = 1;
70
71 // Get the time channel array(s) and store in a vector inside a shared pointer
72 auto timeChannelsVec = getTimeChannels(m_noTimeRegimes, m_lengthIn);
73
74 auto histTotal = static_cast<double>(total_specs * m_numberOfPeriods);
75 int64_t histCurrent = -1;
76
77 // Create the 2D workspace for the output
78 DataObjects::Workspace2D_sptr localWorkspace = createWorkspace(total_specs, m_lengthIn, m_lengthIn - 1, title);
79
80 Run &run = localWorkspace->mutableRun();
81
82 if (bLoadlogFiles) {
83 runLoadLog(m_filename, localWorkspace, 0.0, 0.0);
84 const int period_number = 1;
85 createPeriodLogs(period_number, localWorkspace);
86 }
87 // Set the total proton charge for this run
88 setProtonCharge(run);
89
91 setWorkspaceProperty("OutputWorkspace", title, wsgrp_sptr, localWorkspace, m_numberOfPeriods, false, this);
92
93 // Loop over the number of periods in the raw file, putting each period in a
94 // separate workspace
95 for (int period = 0; period < m_numberOfPeriods; ++period) {
96 if (period > 0) {
97 localWorkspace =
98 std::dynamic_pointer_cast<DataObjects::Workspace2D>(WorkspaceFactory::Instance().create(localWorkspace));
99
100 if (bLoadlogFiles) {
101 // remove previous period data
102 std::stringstream prevPeriod;
103 prevPeriod << "PERIOD " << (period);
104 Run &runObj = localWorkspace->mutableRun();
105 runObj.removeLogData(prevPeriod.str());
106 runObj.removeLogData("current_period");
107 // add current period data
108 auto period_number = static_cast<int>(period + 1);
109 createPeriodLogs(period_number, localWorkspace);
110 }
111 // skip all spectra except the first one in each period
112 for (int i = 1; i <= m_numberOfSpectra; ++i) {
113 skipData(file, i + (period - 1) * (m_numberOfSpectra + 1));
114 }
115 }
116
117 int64_t wsIndex = 0;
118 // for each period read first spectrum
119 int64_t histToRead = period * (static_cast<int64_t>(m_numberOfSpectra) + 1);
120
121 progress(m_prog, "Reading raw file data...");
122 // isisRaw->readData(file, histToRead);
123 // readData(file, histToRead);
124 // read spectrum
125 if (!readData(file, histToRead)) {
126 throw std::runtime_error("Error reading raw file");
127 }
128
129 // set the workspace data
130 setWorkspaceData(localWorkspace, timeChannelsVec, wsIndex, 0, m_noTimeRegimes, m_lengthIn, 1);
131
132 if (m_numberOfPeriods == 1) {
133 if (++histCurrent % 100 == 0) {
134 m_prog = double(histCurrent) / histTotal;
135 }
137 }
138 if (m_numberOfPeriods > 1) {
139 setWorkspaceProperty(localWorkspace, wsgrp_sptr, period, false, this);
140 // progress for workspace groups
141 m_prog = static_cast<double>(period) / static_cast<double>(m_numberOfPeriods - 1);
142 }
143
144 } // loop over periods
145
146 // Clean up
147 reset();
148 fclose(file);
149}
150} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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::vector< std::shared_ptr< HistogramData::HistogramX > > getTimeChannels(const int64_t &regimes, const int64_t &lengthIn)
Constructs the time channel (X) vector(s)
void reset()
resets the isisraw shared pointer
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.
bool readData(FILE *file, int hist)
reads data
void createPeriodLogs(int64_t period, const DataObjects::Workspace2D_sptr &local_workspace)
Create the period specific logs.
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
void setProtonCharge(API::Run &run)
set proton charge
static API::WorkspaceGroup_sptr createGroupWorkspace()
creates shared pointer to group workspace
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.
int m_numberOfPeriods
The number of periods in the raw file.
int64_t m_lengthIn
Read in the time bin boundaries.
void exec() override
Overwrites Algorithm method.
double m_prog
The current value of the progress counter.
std::string m_filename
The name and path of the input file.
int64_t m_noTimeRegimes
number of time regime
specnum_t m_numberOfSpectra
The number of spectra in the raw file.
void init() override
Overwrites Algorithm method.
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
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
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
std::unique_ptr< T > create(const P &parent, const IndexArg &indexArg, const HistArg &histArg)
This is the create() method that all the other create() methods call.