21#include "MantidTypes/Core/DateAndTimeHelpers.h"
23#include <Poco/DateTimeFormat.h>
24#include <Poco/DateTimeParser.h>
25#include <Poco/DirectoryIterator.h>
28#include <boost/algorithm/string.hpp>
34using Mantid::Types::Core::DateAndTime;
40using namespace Kernel;
41using API::FileProperty;
42using API::MatrixWorkspace;
44using API::WorkspaceProperty;
46using Types::Core::DateAndTime;
50template <
class MapClass,
class LoggerType>
51void addLogDataToRun(
Mantid::API::Run &run, MapClass &aMap, LoggerType &logger) {
52 for (
auto &itr : aMap) {
55 }
catch (std::invalid_argument &e) {
56 logger.warning() << e.what() <<
'\n';
57 }
catch (Exception::ExistsError &e) {
58 logger.warning() << e.what() <<
'\n';
73 "The name of the workspace to which the log data will be added.");
75 const std::vector<std::string> exts{
".txt",
".log"};
77 "The filename (including its full or relative path) of a SNS "
78 "text log file (not cvinfo), "
79 "an ISIS log file, or an ISIS raw file. "
80 "If a raw file is specified all log files associated with "
81 "that raw file are loaded into the specified workspace. The "
82 "file extension must "
83 "either be .raw or .s when specifying a raw file");
86 "For SNS-style log files only: the names of each column's log, separated "
88 "This must be one fewer than the number of columns in the file.");
91 "For SNS-style log files only: the units of each column's log, separated "
93 "This must be one fewer than the number of columns in the file. "
94 "Optional: leave blank for no units in any log.");
97 "Number of columns in the file. If not set Mantid will "
112 std::vector<std::string> names =
getProperty(
"Names");
114 std::ifstream logFileStream(
m_filename.c_str());
119 if (l_path.isDirectory()) {
135 if (names.size() > 1) {
136 throw std::invalid_argument(
"More than one log name provided. Invalid ISIS log file.");
141 throw std::invalid_argument(
"File " +
m_filename +
" cannot be read because it has an old unsupported format.");
144 int colNum =
static_cast<int>(
getProperty(
"NumberOfColumns"));
158 throw std::invalid_argument(
"The log file provided is invalid as it has "
159 "less than 2 or more than three columns.");
171 if (!logFileStream) {
172 throw std::invalid_argument(
"Unable to open file " +
m_filename);
179 throw std::invalid_argument(
"File " +
m_filename +
180 " is not a standard ISIS log file. Expected "
181 "to be a two column file.");
184 std::string DateAndTime;
185 std::stringstream ins(aLine);
190 std::string whatType;
195 throw std::invalid_argument(
"ISIS log file contains unrecognised second column entries: " +
m_filename);
203 }
catch (std::exception &) {
217 std::string propname;
218 std::map<std::string, std::unique_ptr<Kernel::TimeSeriesProperty<double>>> dMap;
219 std::map<std::string, std::unique_ptr<Kernel::TimeSeriesProperty<std::string>>> sMap;
221 bool isNumeric(
false);
223 if (!logFileStream) {
224 throw std::invalid_argument(
"Unable to open file " +
m_filename);
229 throw std::invalid_argument(
"File " + logFileName +
230 " is not a standard ISIS log file. Expected "
231 "to be a file starting with DateTime String "
240 std::stringstream line(str);
241 std::string timecolumn;
244 std::string blockcolumn;
249 g_log.
warning() <<
"Failed to parse line in log file: " << timecolumn <<
"\t" << blockcolumn;
254 throw std::invalid_argument(
"ISIS log file contains unrecognised second column entries: " + logFileName);
257 std::string valuecolumn;
268 std::istringstream istr(valuecolumn);
271 isNumeric = !istr.fail();
274 auto ditr = dMap.find(propname);
275 if (ditr != dMap.end()) {
276 auto prop = ditr->second.get();
278 prop->addValue(timecolumn, dvalue);
280 auto logd = std::make_unique<Kernel::TimeSeriesProperty<double>>(propname);
281 logd->addValue(timecolumn, dvalue);
282 dMap.emplace(propname, std::move(logd));
285 auto sitr = sMap.find(propname);
286 if (sitr != sMap.end()) {
287 auto prop = sitr->second.get();
289 prop->addValue(timecolumn, valuecolumn);
291 auto logs = std::make_unique<Kernel::TimeSeriesProperty<std::string>>(propname);
292 logs->addValue(timecolumn, valuecolumn);
293 sMap.emplace(propname, std::move(logs));
297 addLogDataToRun(run, dMap,
g_log);
298 addLogDataToRun(run, sMap,
g_log);
309 if (logName.empty()) {
310 return (Poco::Path(Poco::Path(
m_filename).getFileName()).getBaseName());
312 return (logName.front());
323 std::vector<std::string> names =
getProperty(
"Names");
324 std::vector<std::string> units =
getProperty(
"Units");
338 std::vector<double> cols;
341 if (!ret || cols.size() < 2)
344 auto numCols =
static_cast<size_t>(cols.size() - 1);
345 if (names.size() != numCols)
346 throw std::invalid_argument(
"The Names parameter should have one fewer "
347 "entry as the number of columns in a SNS-style "
349 if ((!units.empty()) && (units.size() != numCols))
350 throw std::invalid_argument(
"The Units parameter should have either 0 "
351 "entries or one fewer entry as the number of "
352 "columns in a SNS-style text log file.");
355 std::vector<TimeSeriesProperty<double> *> props;
356 for (
size_t i = 0; i < numCols; i++) {
358 if (units.size() == numCols)
359 p->setUnits(units[i]);
360 props.emplace_back(p);
369 if (cols.size() == numCols + 1) {
370 DateAndTime time(cols[0], 0.0);
371 for (
size_t i = 0; i < numCols; i++)
372 props[i]->addValue(time, cols[i + 1]);
374 throw std::runtime_error(
"Inconsistent number of columns while reading "
375 "SNS-style text file.");
377 throw std::runtime_error(
"Error while reading columns in SNS-style text file.");
380 for (
size_t i = 0; i < numCols; i++) {
381 std::string
name = props[i]->name();
382 if (localWorkspace->mutableRun().hasProperty(
name)) {
383 localWorkspace->mutableRun().removeLogData(
name);
386 localWorkspace->mutableRun().addLogData(props[i]);
404 const string lower(
"abcdefghijklmnopqrstuvwxyz");
405 const string upper(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
408 if (letters.find_first_of(s) != string::npos) {
412 const auto isNumber = [](
const std::string &str) {
419 }
catch (
const std::invalid_argument &) {
421 }
catch (
const std::out_of_range &) {
435 std::transform(strToConvert.begin(), strToConvert.end(), strToConvert.begin(), tolower);
445 FILE *file = fopen(filename.c_str(),
"rb");
447 size_t n = fread(data, 1,
sizeof(data), file);
449 char *pend = &data[
n];
454 for (
char *p = data; p < pend; ++p) {
455 auto ch =
static_cast<unsigned long>(*p);
469 return Types::Core::DateAndTimeHelpers::stringIsISO8601(str.substr(0, 19));
482 std::string firstLine;
485 logFileStream.seekg(0);
487 std::regex oldDateFormat(R
"([A-Z][a-z]{2} [ 1-3]\d-[A-Z]{3}-\d{4} \d{2}:\d{2}:\d{2})");
489 return std::regex_match(firstLine.substr(0, 24), oldDateFormat);
499 std::vector<std::string> strs;
501 boost::split(strs, input, boost::is_any_of(
"\t "));
504 for (
auto &str : strs) {
505 if (!Strings::convert<double>(str, val))
508 out.emplace_back(val);
520 if (!logFileStream) {
521 throw std::invalid_argument(
"Unable to open file " +
m_filename);
531 throw std::invalid_argument(
"File " + logFileName +
532 " is not a standard ISIS log file. Expected to "
533 "be a file starting with DateTime String "
537 std::stringstream line(str);
538 std::string timecolumn;
541 std::string blockcolumn;
546 throw std::invalid_argument(
"ISIS log file contains unrecognised second column entries: " + logFileName);
549 std::string valuecolumn;
554 logFileStream.seekg(0);
#define DECLARE_ALGORITHM(classname)
double lower
lower and upper bounds on the multiplier, if known
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
void addLogData(Kernel::Property *p)
Add a log entry.
This class stores information regarding an experimental run as a series of log entries.
A property class for workspaces.
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
bool LoadSNSText()
SNS text.
void init() override
Overwrites Algorithm method.
std::string m_filename
The name and path of an input file.
kind classify(const std::string &s) const
Takes as input a string and try to determine what type it is.
bool isAscii(const std::string &filename)
Checks if the file is an ASCII file.
LoadLog()
Default constructor.
bool isDateTimeString(const std::string &str) const
Check if first 19 characters of a string is date-time string according to yyyy-mm-ddThh:mm:ss.
std::string extractLogName(const std::vector< std::string > &logName)
Checks if a log file name was provided (e.g.
void exec() override
Overwrites Algorithm method.
bool SNSTextFormatColumns(const std::string &input, std::vector< double > &out) const
Check for SNS-style text file.
std::string stringToLower(std::string strToConvert)
Convert string to lower case.
int countNumberColumns(std::ifstream &logFileStream, const std::string &logFileName)
Returns the number of columns in the log file.
bool isOldDateTimeFormat(std::ifstream &logFileStream) const
Check whether the first 24 characters of a string are consistent with the date-time format used in ol...
void loadThreeColumnLogFile(std::ifstream &logFileStream, const std::string &logFileName, API::Run &run)
Create timeseries property from .log file and adds that to sample object.
void loadTwoColumnLogFile(std::ifstream &logFileStream, std::string logFileName, API::Run &run)
Loads two column log file data into local workspace.
kind
type returned by classify
Support for a property that holds an array of values.
Records the filename and the description of failure.
static Kernel::Property * createLogProperty(const std::string &logFName, const std::string &name)
Creates a TimeSeriesProperty of either double or string type depending on the log data Returns a poin...
void warning(const std::string &msg)
Logs at warning level.
void information(const std::string &msg)
Logs at information level.
Base class for properties.
A specialised Property class for holding a series of time-value pairs.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< Workspace2D > Workspace2D_sptr
shared pointer to Mantid::DataObjects::Workspace2D
MANTID_KERNEL_DLL std::istream & extractToEOL(std::istream &is, std::string &str)
Extract a line from input stream, discarding any EOL characters encountered.
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
@ InOut
Both an input & output workspace.