Skip to content
Snippets Groups Projects
Unverified Commit 86c74d3e authored by Gagik Vardanyan's avatar Gagik Vardanyan Committed by GitHub
Browse files

Merge pull request #30964 from mantidproject/30889_old_log_warning

new warning message for loading old log files
parents 54a0611c f293a43d
No related branches found
No related tags found
No related merge requests found
...@@ -100,10 +100,14 @@ private: ...@@ -100,10 +100,14 @@ private:
/// Checks if the file is an ASCII file /// Checks if the file is an ASCII file
bool isAscii(const std::string &filename); 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 /// yyyy-mm-ddThh:mm:ss
bool isDateTimeString(const std::string &str) const; 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 /// Checks if a log file name was provided (e.g. through setPropertyValue). If
/// not it creates one based on provided path. /// not it creates one based on provided path.
std::string extractLogName(const std::vector<std::string> &logName); std::string extractLogName(const std::vector<std::string> &logName);
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <Poco/Path.h> #include <Poco/Path.h>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <fstream> // used to get ifstream #include <fstream> // used to get ifstream
#include <regex>
#include <sstream> #include <sstream>
#include <utility> #include <utility>
...@@ -143,6 +144,13 @@ void LoadLog::exec() { ...@@ -143,6 +144,13 @@ void LoadLog::exec() {
"More than one log name provided. Invalid ISIS log file."); "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")); int colNum = static_cast<int>(getProperty("NumberOfColumns"));
if (colNum == Mantid::EMPTY_INT()) { if (colNum == Mantid::EMPTY_INT()) {
...@@ -181,7 +189,7 @@ void LoadLog::loadTwoColumnLogFile(std::ifstream &logFileStream, ...@@ -181,7 +189,7 @@ void LoadLog::loadTwoColumnLogFile(std::ifstream &logFileStream,
std::string aLine; std::string aLine;
if (Mantid::Kernel::Strings::extractToEOL(logFileStream, aLine)) { if (Mantid::Kernel::Strings::extractToEOL(logFileStream, aLine)) {
if (!isDateTimeString(aLine)) { if (!isDateTimeString(aLine)) {
throw std::invalid_argument("File" + m_filename + throw std::invalid_argument("File " + m_filename +
" is not a standard ISIS log file. Expected " " is not a standard ISIS log file. Expected "
"to be a two column file."); "to be a two column file.");
} }
...@@ -239,7 +247,7 @@ void LoadLog::loadThreeColumnLogFile(std::ifstream &logFileStream, ...@@ -239,7 +247,7 @@ void LoadLog::loadThreeColumnLogFile(std::ifstream &logFileStream,
while (Mantid::Kernel::Strings::extractToEOL(logFileStream, str)) { while (Mantid::Kernel::Strings::extractToEOL(logFileStream, str)) {
if (!isDateTimeString(str) && !str.empty()) { if (!isDateTimeString(str) && !str.empty()) {
throw std::invalid_argument("File" + logFileName + throw std::invalid_argument("File " + logFileName +
" is not a standard ISIS log file. Expected " " is not a standard ISIS log file. Expected "
"to be a file starting with DateTime String " "to be a file starting with DateTime String "
"format."); "format.");
...@@ -267,7 +275,7 @@ void LoadLog::loadThreeColumnLogFile(std::ifstream &logFileStream, ...@@ -267,7 +275,7 @@ void LoadLog::loadThreeColumnLogFile(std::ifstream &logFileStream,
if (LoadLog::string != l_kind) { if (LoadLog::string != l_kind) {
throw std::invalid_argument( throw std::invalid_argument(
"ISIS log file contains unrecognised second column entries:" + "ISIS log file contains unrecognised second column entries: " +
logFileName); logFileName);
} }
...@@ -491,6 +499,27 @@ bool LoadLog::isDateTimeString(const std::string &str) const { ...@@ -491,6 +499,27 @@ bool LoadLog::isDateTimeString(const std::string &str) const {
return Types::Core::DateAndTimeHelpers::stringIsISO8601(str.substr(0, 19)); 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 logFileStream :: The file 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. * Read a line of a SNS-style text file.
* @param str :: The string to test * @param str :: The string to test
...@@ -532,7 +561,7 @@ int LoadLog::countNumberColumns(std::ifstream &logFileStream, ...@@ -532,7 +561,7 @@ int LoadLog::countNumberColumns(std::ifstream &logFileStream,
Mantid::Kernel::Strings::extractToEOL(logFileStream, str); Mantid::Kernel::Strings::extractToEOL(logFileStream, str);
if (!isDateTimeString(str)) { if (!isDateTimeString(str)) {
throw std::invalid_argument("File" + logFileName + throw std::invalid_argument("File " + logFileName +
" is not a standard ISIS log file. Expected to " " is not a standard ISIS log file. Expected to "
"be a file starting with DateTime String " "be a file starting with DateTime String "
"format."); "format.");
...@@ -548,7 +577,7 @@ int LoadLog::countNumberColumns(std::ifstream &logFileStream, ...@@ -548,7 +577,7 @@ int LoadLog::countNumberColumns(std::ifstream &logFileStream,
if (LoadLog::string != l_kind && LoadLog::number != l_kind) { if (LoadLog::string != l_kind && LoadLog::number != l_kind) {
throw std::invalid_argument( throw std::invalid_argument(
"ISIS log file contains unrecognised second column entries:" + "ISIS log file contains unrecognised second column entries: " +
logFileName); logFileName);
} }
......
...@@ -142,6 +142,35 @@ public: ...@@ -142,6 +142,35 @@ public:
AnalysisDataService::Instance().remove(outputSpace); AnalysisDataService::Instance().remove(outputSpace);
} }
void testOldLogFileWithSingleDigitDate() {
// Snippet from an old log file with unsupported format.
std::string oldLogText(
"Sat 8-FEB-2003 11:29:24 EXECUTING DOSANS\n"
"Sat 8-FEB-2003 11:29:25 LOAD INST.UPD\n"
"Sat 8-FEB-2003 11:29:25 CHANGE RUNTABLE "
"LOQ$DISK0:[LOQ]INST.UPD;15625\n"
"Sat 8-FEB-2003 11:29:27 CSET TRANS OUT\n"
"Sat 8-FEB-2003 11:29:27 COMND TRANS WR 05 2 1 16 4\n");
TS_ASSERT(oldLogFileGivesCorrectWarning(oldLogText));
}
void testOldLogFileWithDoubleDigitDate() {
// Snippet from an old log file with unsupported format.
std::string oldLogText("Fri 31-JAN-2003 11:28:15 EXECUTING DOTRANS\n"
"Fri 31-JAN-2003 11:28:41 STOPD A2 _LTA5007: "
" 2.7000 2.7000\n"
"Fri 31-JAN-2003 11:28:42 LOAD INST.UPD\n"
"Fri 31-JAN-2003 11:28:42 CHANGE RUNTABLE "
"LOQ$DISK0:[LOQ]INST.UPD;14965\n"
"Fri 31-JAN-2003 11:28:42 DEBUG 1 ack 001 suc 000\n"
"Fri 31-JAN-2003 11:28:42 DEBUG 1 res 001 suc 002 "
"H000000F0 H00000006\n"
"Fri 31-JAN-2003 11:28:43 CSET TRANS IN\n");
TS_ASSERT(oldLogFileGivesCorrectWarning(oldLogText));
}
void test_log_file_has_error() { void test_log_file_has_error() {
std::string logFileText("2007-11-16T13:25:48 i1 0 \n" std::string logFileText("2007-11-16T13:25:48 i1 0 \n"
"2007-11-16T13:29:36 str1 a\n" "2007-11-16T13:29:36 str1 a\n"
...@@ -312,4 +341,33 @@ private: ...@@ -312,4 +341,33 @@ private:
std::string inputFile; std::string inputFile;
std::string outputSpace; std::string outputSpace;
std::string inputSpace; std::string inputSpace;
bool oldLogFileGivesCorrectWarning(std::string &logFileText) {
ScopedFile oldLogFile(logFileText, "oldLogFile.log");
MatrixWorkspace_sptr ws =
WorkspaceFactory::Instance().create("Workspace2D", 1, 1, 1);
LoadLog loadAlg;
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 ""
try {
loadAlg.execute();
} catch (std::exception &ex) {
std::string errorMessage(ex.what());
if (errorMessage.find("old unsupported format") != std::string::npos) {
return true;
}
}
return false;
}
}; };
...@@ -22,6 +22,7 @@ Algorithms ...@@ -22,6 +22,7 @@ Algorithms
- :ref:`CompareWorkspaces <algm-CompareWorkspaces>` compares the positions of both source and sample (if extant) when property `checkInstrument` is set. - :ref:`CompareWorkspaces <algm-CompareWorkspaces>` compares the positions of both source and sample (if extant) when property `checkInstrument` is set.
- :ref:`SetGoniometer <algm-SetGoniometer>` can now set multiple goniometers from log values instead of just the time-avereged value. - :ref:`SetGoniometer <algm-SetGoniometer>` can now set multiple goniometers from log values instead of just the time-avereged value.
- Added the ability to specify the spectrum number in :ref:`FindPeaksAutomatic <algm-FindPeaksAutomatic>`. - Added the ability to specify the spectrum number in :ref:`FindPeaksAutomatic <algm-FindPeaksAutomatic>`.
- :ref:`LoadLog <algm-LoadLog>` will now detect old unsupported log files and set an appropriate explanatory string in the exception.
Data Objects Data Objects
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment