23#include <boost/date_time/gregorian/gregorian.hpp>
24#include <boost/date_time/date_parsing.hpp>
26#include <boost/lexical_cast.hpp>
27#include <boost/algorithm/string.hpp>
32#include <boost/regex.hpp>
43 std::string input = std::accumulate(codes.
begin(), codes.
end(), std::string(
""));
44 std::string reg(R
"(^[06][\w]+\([/ \w\^-]+\)$)");
45 boost::regex baseRegex(reg);
46 return boost::regex_match(input, baseRegex);
69void
LoadRKH::readLinesForRKH1D(
std::istream &stream,
int readStart,
int readEnd, HistogramData::Points &
x,
70 HistogramData::Counts &
y, HistogramData::CountStandardDeviations &ye,
71 HistogramData::PointStandardDeviations &xe,
Progress &prog,
bool readXError) {
73 std::vector<double> xData;
74 std::vector<double> yData;
75 std::vector<double> xError;
76 std::vector<double> yError;
78 xData.reserve(readEnd);
79 yData.reserve(readEnd);
80 xError.reserve(readEnd);
81 yError.reserve(readEnd);
85 getline(stream, fileline);
86 if (
index < readStart)
89 double xValue(0.), yValue(0.), yErrorValue(0.);
90 std::istringstream datastr(fileline);
91 datastr >> xValue >> yValue >> yErrorValue;
93 xData.emplace_back(xValue);
94 yData.emplace_back(yValue);
95 yError.emplace_back(yErrorValue);
99 double xErrorValue(0.);
100 datastr >> xErrorValue;
101 xError.emplace_back(xErrorValue);
123 auto &file = descriptor.
data();
124 std::string fileline;
130 std::getline(file, fileline);
132 if (boost::ifind_first(fileline,
"loq").empty() && boost::ifind_first(fileline,
"sans2d").empty())
136 static const char *MONTHS[12] = {
"-JAN-",
"-FEB-",
"-MAR-",
"-APR-",
"-MAY-",
"-JUN-",
137 "-JUL-",
"-AUG-",
"-SEP-",
"-OCT-",
"-NOV-",
"-DEC-"};
139 bool foundMonth(
false);
140 for (
auto &month : MONTHS) {
141 if (!boost::ifind_first(fileline, month).empty()) {
150 std::getline(file, fileline);
153 std::getline(file, fileline);
154 if (fileline.find(
"0 0 0 1") == std::string::npos)
158 std::getline(file, fileline);
159 if (fileline.find(
"0 0 0 0") == std::string::npos)
163 std::getline(file, fileline);
164 if (fileline.find(
"3 (F12.5,2E16.6)") == std::string::npos)
174 const std::vector<std::string> exts{
".txt",
".q",
".dat"};
176 "Name of the RKH file to load");
178 "The name to use for the output workspace");
181 m_unitKeys.insert(propOptions.begin(), propOptions.end());
191 declareProperty(
"FirstColumnValue",
"Wavelength", std::make_shared<Kernel::StringListValidator>(propOptions),
192 "Only used for 1D files, the units of the first column in the RKH "
193 "file (default Wavelength)");
208 g_log.
error(
"Unable to open file " + filename);
224 result->setDistribution(
true);
238 return isUnit(codes);
245 g_log.
information() <<
"file appears to contain 1D information, reading in 1D data mode\n";
250 int totalPoints(0), readStart(0), readEnd(0), buried(0);
251 std::string fileline;
254 std::istringstream is(fileline);
256 for (
int counter = 1; counter < 8; ++counter) {
273 g_log.
information() <<
"Total number of data points declared to be in the data file: " << totalPoints <<
"\n";
276 std::string firstColVal =
getProperty(
"FirstColumnValue");
277 bool colIsUnit(
true);
281 readEnd = totalPoints;
284 if (readStart < 1 || readEnd < 1 || readEnd < readStart || readStart > totalPoints || readEnd > totalPoints) {
287 throw std::invalid_argument(
"Invalid data range specfied.");
290 g_log.
information() <<
"Reading started on data line: " << readStart <<
"\n";
291 g_log.
information() <<
"Reading finished on data line: " << readEnd <<
"\n";
296 int pointsToRead = readEnd - readStart + 1;
298 HistogramData::Points columnOne;
299 HistogramData::Counts ydata;
300 HistogramData::PointStandardDeviations xError;
301 HistogramData::CountStandardDeviations errdata;
305 Progress prog(
this, 0.0, 1.0, readEnd);
311 assert(pointsToRead ==
static_cast<int>(columnOne.size()));
312 assert(pointsToRead ==
static_cast<int>(ydata.size()));
313 assert(pointsToRead ==
static_cast<int>(errdata.size()));
316 assert(pointsToRead ==
static_cast<int>(xError.size()));
322 localworkspace->getSpectrum(0).setDetectorID(
static_cast<detid_t>(1));
324 localworkspace->setPoints(0, columnOne);
325 localworkspace->setCounts(0, ydata);
326 localworkspace->setCountStandardDeviations(0, errdata);
328 localworkspace->setPointStandardDeviations(0, xError);
330 return localworkspace;
335 localworkspace->getSpectrum(
index).setSpectrumNo(
static_cast<int>(columnOne[
index]));
336 localworkspace->getSpectrum(
index).setDetectorID(
static_cast<detid_t>(
index + 1));
337 localworkspace->dataY(
index)[0] = ydata[
index];
338 localworkspace->dataE(
index)[0] = errdata[
index];
343 localworkspace->setPointStandardDeviations(0, 1, xError[
index]);
346 return localworkspace;
358 g_log.
information() <<
"file appears to contain 2D information, reading in 2D data mode\n";
363 const size_t nAxis1Values = outWrksp->getNumberHistograms();
366 auto toPass = Kernel::make_cow<HistogramData::HistogramX>(axis0Data);
367 for (
size_t i = 0; i < nAxis1Values; ++i) {
368 outWrksp->setX(i, toPass);
372 for (
double &
value : YOut) {
375 prog.
report(
"Loading Y data");
379 for (
size_t i = 0; i < nAxis1Values; ++i) {
381 for (
double &
value : EOut) {
384 prog.
report(
"Loading error estimates");
399 const std::string XUnit(
readUnit(initalLine));
401 std::string fileLine;
403 const std::string YUnit(
readUnit(fileLine));
406 const std::string intensityUnit(
readUnit(fileLine));
414 boost::trim(fileLine);
415 const auto nAxis0Boundaries = boost::lexical_cast<int>(fileLine);
416 axis0Data.resize(nAxis0Boundaries);
420 boost::trim(fileLine);
421 int nAxis1Boundaries;
423 nAxis1Boundaries = boost::lexical_cast<int>(fileLine);
424 }
catch (boost::bad_lexical_cast &) {
428 boost::trim(fileLine);
429 nAxis1Boundaries = boost::lexical_cast<int>(fileLine);
436 if (fileLine.size() < 5) {
441 if (wsDimensions.
count() < 2) {
444 const auto nAxis0Values = boost::lexical_cast<int>(wsDimensions[0]);
445 const auto nAxis1Values = boost::lexical_cast<int>(wsDimensions[1]);
447 Progress prog(
this, 0.05, 1.0, 2 * nAxis1Values);
451 for (
int i = 0; i < nAxis1Values; ++i) {
452 outWrksp->getSpectrum(i).setDetectorID(
static_cast<detid_t>(i + 1));
455 outWrksp->setYUnitLabel(intensityUnit);
457 auto axis1 = std::make_unique<Mantid::API::NumericAxis>(nAxis1Boundaries);
458 auto axis1Raw = axis1.get();
460 outWrksp->replaceAxis(1, std::move(axis1));
461 for (
int i = 0; i < nAxis1Boundaries; ++i) {
462 axis1Raw->setValue(i, axis1Data[i]);
465 outWrksp->setTitle(title);
479 output.resize(nEntries);
480 for (
int i = 0; i < nEntries; ++i) {
494 if (!isUnit(codes)) {
495 return "C++ no unit found";
498 if (codes.
count() < 1) {
499 return "C++ no unit found";
503 const std::string symbol(codes[0]);
505 auto itUnitsToken = codes.
cend() - 1;
506 const std::string unit(*itUnitsToken);
509 std::string theQuantity;
510 for (
auto current = codes.
cbegin() + 1; current != itUnitsToken; ++current) {
511 theQuantity += *current;
515 if (codes.
count() >= 3) {
521 if (unit.find(
'(') != 0 || unit.find(
')') != unit.size()) {
523 if (symbol == qCode && theQuantity ==
"q" &&
524 (unit ==
"(1/Angstrom)" || unit ==
"(Angstrom^-1)")) {
526 return "MomentumTransfer";
529 if (symbol ==
"0" && theQuantity !=
"q") {
532 return theQuantity +
" " + unit;
537 return "C++ no unit found";
546 for (
int i = 0; i < nlines; ++i) {
547 getline(strm, buried);
564 auto containsXerror =
false;
565 auto currentPutLocation = stream.tellg();
567 getline(stream, line);
569 std::string
x,
y, yerr, xerr;
570 std::istringstream datastr(line);
571 datastr >>
x >>
y >> yerr >> xerr;
573 containsXerror =
true;
576 stream.seekg(currentPutLocation, stream.beg);
577 return containsXerror;
double value
The value of the point.
std::map< DeltaEMode::Type, std::string > index
#define DECLARE_FILELOADER_ALGORITHM(classname)
DECLARE_FILELOADER_ALGORITHM should be used in place of the standard DECLARE_ALGORITHM macro when wri...
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.
@ Load
allowed here which will be passed to the algorithm
Helper class for reporting progress from algorithms.
A property class for workspaces.
Loads an RKH file into a Mantid 1D workspace.
int confidence(Kernel::FileDescriptor &descriptor) const override
Returns a confidence value that this algorithm can load a file.
const API::MatrixWorkspace_sptr read1D()
Read a data file that contains only one spectrum into a workspace.
std::unordered_set< std::string > m_RKHKeys
Store the units added as options for this algorithm.
const std::string readUnit(const std::string &line)
Convert the units specification line from the RKH file into a Mantid unit name.
void init() override
Initialise the algorithm.
void readNumEntrys(const int nEntries, MantidVec &output)
Read the specified number of entries from input file into the the array that is passed.
void binCenter(const MantidVec &oldBoundaries, MantidVec &toCenter) const
PAss a vector of bin boundaries and get a vector of bin centers.
void exec() override
Execute the algorithm.
bool hasXerror(std::ifstream &stream)
Check if we the data set stores an X-Error values.
std::unordered_set< std::string > m_unitKeys
Store the units known to the UnitFactory.
const API::MatrixWorkspace_sptr read2D(const std::string &firstLine)
Reads from the third line of the input file to the end assuming it contains 2D data.
void readLinesForRKH1D(std::istream &stream, int readStart, int readEnd, HistogramData::Points &x, HistogramData::Counts &y, HistogramData::CountStandardDeviations &ye, HistogramData::PointStandardDeviations &xe, API::Progress &prog, bool readXError=false)
Read data from the RKH file.
API::Progress read2DHeader(const std::string &initalLine, API::MatrixWorkspace_sptr &outWrksp, MantidVec &axis0Data)
Reads the header information from a file containing 2D data.
std::ifstream m_fileIn
the input stream for the file being loaded
bool is2D(const std::string &testLine)
Determines if the file is 1D or 2D based on the first after the workspace's title.
void skipLines(std::istream &strm, int nlines)
Remove lines from an input stream.
@ Q_CODE
this is the integer code the RKH file format associates
Records the filename and the description of failure.
Exception for when an item is not found in a collection.
Defines a wrapper around an open file.
static bool isAscii(const std::string &filename, const size_t nbytes=256)
Returns true if the file is considered ascii.
std::istream & data()
Access the open file stream.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void error(const std::string &msg)
Logs at error level.
void information(const std::string &msg)
Logs at information level.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
Iterator begin()
Iterator referring to first element in the container.
@ TOK_IGNORE_EMPTY
ignore empty tokens
@ TOK_TRIM
remove leading and trailing whitespace from tokens
Iterator end()
Iterator referring to the past-the-end element in the container.
ConstIterator cend() const
Const iterator referring to the past-the-end element in the container.
ConstIterator cbegin() const
Const iterator referring to first element in the container.
std::size_t count() const
Get the total number of tokens.
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
void MANTID_KERNEL_DLL convertToBinCentre(const std::vector< double > &bin_edges, std::vector< double > &bin_centres)
Convert an array of bin boundaries to bin center values.
int32_t detid_t
Typedef for a detector ID.
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
std::string to_string(const wide_integer< Bits, Signed > &n)
@ Output
An output workspace.