Commit 4405512a authored by Tom Hampson's avatar Tom Hampson
Browse files

Determine whether a log file is old by examinging date-time format.

parent 53f207dc
......@@ -100,10 +100,14 @@ private:
/// Checks if the file is an ASCII file
bool isAscii(const std::string &filename);
/// Check if first 19 characters of a string is data-time string according to
/// Check if first 19 characters of a string is date-time string according to
/// yyyy-mm-ddThh:mm:ss
bool isDateTimeString(const std::string &str) const;
/// Check whether the first 24 characters of a string are consistent with
/// the date-time format used in older unsupported log files.
bool isOldDateTimeFormat(std::ifstream &logFileStream) const;
/// Checks if a log file name was provided (e.g. through setPropertyValue). If
/// not it creates one based on provided path.
std::string extractLogName(const std::vector<std::string> &logName);
......
......@@ -29,6 +29,7 @@
#include <fstream> // used to get ifstream
#include <sstream>
#include <utility>
#include <regex>
using Mantid::Types::Core::DateAndTime;
......@@ -143,6 +144,13 @@ void LoadLog::exec() {
"More than one log name provided. Invalid ISIS log file.");
}
// If it's an old log file (pre-2007), then it is not currently supported.
if (isOldDateTimeFormat(logFileStream)) {
throw std::invalid_argument(
"File " + m_filename +
" cannot be read because it has an old unsupported format.");
}
int colNum = static_cast<int>(getProperty("NumberOfColumns"));
if (colNum == Mantid::EMPTY_INT()) {
......@@ -491,6 +499,27 @@ bool LoadLog::isDateTimeString(const std::string &str) const {
return Types::Core::DateAndTimeHelpers::stringIsISO8601(str.substr(0, 19));
}
/**
* Check whether the string is consistent with the old log file
* date-time format, for example:
* Fri 31-JAN-2003 11:28:15
* Wed 9-FEB-2005 09:47:01
* @param str :: The string to test
* @return true if the format matches the old log file format.
*/
bool LoadLog::isOldDateTimeFormat(std::ifstream &logFileStream) const {
// extract first line of file
std::string firstLine;
Mantid::Kernel::Strings::extractToEOL(logFileStream, firstLine);
// reset file back to the beginning
logFileStream.seekg(0);
std::regex oldDateFormat(
R"([A-Z][a-z]{2} [ 1-3]\d-[A-Z]{3}-\d{4} \d{2}:\d{2}:\d{2})");
return std::regex_match(firstLine.substr(0, 24), oldDateFormat);
}
/**
* Read a line of a SNS-style text file.
* @param str :: The string to test
......
......@@ -171,8 +171,6 @@ public:
TS_ASSERT(oldLogFileGivesCorrectWarning(oldLogText));
}
void test_log_file_has_error() {
std::string logFileText("2007-11-16T13:25:48 i1 0 \n"
"2007-11-16T13:29:36 str1 a\n"
......@@ -355,6 +353,8 @@ private:
loadAlg.initialize();
loadAlg.setPropertyValue("Filename", oldLogFile.getFileName());
loadAlg.setProperty("Workspace", ws);
// We want to see what the exception message is.
loadAlg.setRethrows(true);
// LoadLog algorithm should throw exception with an error message that
// contains ""
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment