From 93971e541c1798d6c22620c5910a2957fc29501f Mon Sep 17 00:00:00 2001 From: Rebecca Cox Date: Tue, 26 Sep 2017 13:14:00 +0200 Subject: [PATCH] #1922 Observed data: Update column parser to handle keywords --- .../FileInterface/RifRsmspecParserTools.cpp | 91 ++++------- .../FileInterface/RifRsmspecParserTools.h | 8 +- .../UnitTests/ObservedDataParser-Test.cpp | 149 ++++++++++++------ 3 files changed, 143 insertions(+), 105 deletions(-) diff --git a/ApplicationCode/FileInterface/RifRsmspecParserTools.cpp b/ApplicationCode/FileInterface/RifRsmspecParserTools.cpp index 2d10f900d7..c7cd76d225 100644 --- a/ApplicationCode/FileInterface/RifRsmspecParserTools.cpp +++ b/ApplicationCode/FileInterface/RifRsmspecParserTools.cpp @@ -26,12 +26,15 @@ #include #include +#include +#include + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- bool RifRsmspecParserTools::isLineSkippable(const std::string& line) { - if (line.size() == 0) + if (std::all_of(line.begin(), line.end(), isspace)) { return true; } @@ -59,17 +62,6 @@ bool RifRsmspecParserTools::isLineSkippable(const std::string& line) return false; } -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -std::vector RifRsmspecParserTools::splitLine(const std::string& line) -{ - std::istringstream iss(line); - std::vector words{ std::istream_iterator{iss}, - std::istream_iterator{} }; - return words; -} - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -85,7 +77,7 @@ bool RifRsmspecParserTools::isAComment(const std::string& word) //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -std::vector RifRsmspecParserTools::splitLineAndRemoveComments(const std::string& line) +std::vector RifRsmspecParserTools::splitLineAndRemoveComments(std::string line) { std::istringstream iss(line); std::vector words{ std::istream_iterator{iss}, @@ -103,44 +95,6 @@ std::vector RifRsmspecParserTools::splitLineAndRemoveComments(const return words; } -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -bool RifRsmspecParserTools::canBeAMnemonic(const std::string& word) -{ - if (word.size() < 1) return false; - - char firstLetter = word.at(0); - - if (firstLetter == 'A' || - firstLetter == 'B' || - firstLetter == 'C' || - firstLetter == 'F' || - firstLetter == 'G' || - firstLetter == 'N' || - firstLetter == 'R' || - firstLetter == 'S' || - firstLetter == 'W' ) - { - return true; - } - - if (word.size() < 2) return false; - - std::string firstTwoLetters; - firstTwoLetters.push_back(word.at(0)); - firstTwoLetters.push_back(word.at(1)); - - if (firstTwoLetters == "LB" || - firstTwoLetters == "LC" || - firstTwoLetters == "LW" ) - { - return true; - } - - return false; -} - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -265,6 +219,30 @@ RifEclipseSummaryAddress RifRsmspecParserTools::makeAndFillAddress(std::string q cellK); } +bool RifRsmspecParserTools::keywordParser(std::string line, std::string& origin, std::string& dateFormat, std::string& startDate) +{ + std::vector words = splitLineAndRemoveComments(line); + if (words.size() < 2) return false; + + if (words[0] == "ORIGIN") + { + origin = words[1]; + return true; + } + else if (words[0] == "STARTDATE") + { + words.erase(words.begin()); + startDate = std::accumulate(words.begin(), words.end(), std::string("")); + return true; + } + else if (words[0] == "DATEFORMAT") + { + dateFormat = words[1]; + return true; + } + return false; +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -277,10 +255,9 @@ std::vector RifRsmspecParserTools::columnInfoForTable(std::stringstr std::string startDate = ""; - while (isLineSkippable(line)) + while (isLineSkippable(line) || keywordParser(line, origin, dateFormat, startDate)) { if (!streamData.good()) return table; - std::getline(streamData, line); } @@ -297,6 +274,9 @@ std::vector RifRsmspecParserTools::columnInfoForTable(std::stringstr { ColumnInfo columnInfo; columnInfo.unitName = unit; + columnInfo.origin = origin; + columnInfo.dateFormat = dateFormat; + columnInfo.startDate = startDate; table.push_back(columnInfo); } @@ -370,10 +350,9 @@ void RifRsmspecParserTools::splitLineToDoubles(const std::string& line, std::vec std::istringstream iss(line); values.clear(); - while (iss.good()) + double d; + while (iss >> d) { - double d; - iss >> d; values.push_back(d); } } diff --git a/ApplicationCode/FileInterface/RifRsmspecParserTools.h b/ApplicationCode/FileInterface/RifRsmspecParserTools.h index b259b8a1ca..c3bb143d98 100644 --- a/ApplicationCode/FileInterface/RifRsmspecParserTools.h +++ b/ApplicationCode/FileInterface/RifRsmspecParserTools.h @@ -24,9 +24,9 @@ #include #include +#include #include #include -#include struct ColumnInfo { @@ -36,6 +36,7 @@ struct ColumnInfo std::string scaleFactor; std::vector values; std::string dateFormat; + std::string startDate; std::string origin; }; @@ -46,14 +47,13 @@ class RifRsmspecParserTools { public: static bool isLineSkippable(const std::string& line); - static std::vector splitLine(const std::string& line); static bool isAComment(const std::string& word); - static std::vector splitLineAndRemoveComments(const std::string& line); - static bool canBeAMnemonic(const std::string& word); + static std::vector splitLineAndRemoveComments(std::string line); static RifEclipseSummaryAddress::SummaryVarCategory identifyCategory(const std::string& word); static void splitLineToDoubles(const std::string& line, std::vector& values); static size_t findFirstNonEmptyEntryIndex(std::vector& list); static RifEclipseSummaryAddress makeAndFillAddress(std::string quantityName, std::vector< std::string > headerColumn); + static bool keywordParser(std::string line, std::string& origin, std::string& dateFormat, std::string& startDate); static std::vector columnInfoForTable(std::stringstream& data, std::string& line); static bool isANumber(const std::string& line); static std::vector headerReader(std::stringstream& streamData, std::string& line); diff --git a/ApplicationCode/UnitTests/ObservedDataParser-Test.cpp b/ApplicationCode/UnitTests/ObservedDataParser-Test.cpp index d5a946bdb4..ac75997f7f 100644 --- a/ApplicationCode/UnitTests/ObservedDataParser-Test.cpp +++ b/ApplicationCode/UnitTests/ObservedDataParser-Test.cpp @@ -200,6 +200,110 @@ TEST(RifColumnBasedAsciiParserTest, TestCellSeparatorComma) EXPECT_EQ(4.44, pwValues[3]); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +TEST(RifRsmspecParserToolsTest, TestSplitLineToDoubles) +{ + + QString data; + QTextStream out(&data); + + out << " 1 0.0 0.0 0.0 0.0 0.0\n"; + out << " 2 0.0 0.0 0.0 0.0 0.0\n"; + out << " 3 0.0 0.0 0.0 0.0 0.0 \n"; + out << " 4 0.0 0.0 0.0 0.0 0.0 --note\n"; + out << " 5 0.0 0.0 0.0 0.0 0.0\n"; + out << " 6 0.0 0.0 0.0 0.0 0.0\n"; + out << " 7 0.0 0.0 282 0.0 0.0 -- This is a test \n"; + out << " 8 0.0 0.0 279 0.0 0.0\n"; + out << " 9 0.0 0.0 0.0 0.0 0.0\n"; + out << " 10 0.0 0.0 0.0 0.0 0.0\n"; + + RifRsmspecParserTools parserTool; + std::stringstream streamData; + streamData.str(data.toStdString()); + std::string line; + + std::vector< std::vector > table; + + while (std::getline(streamData, line)) + { + std::vector values; + parserTool.splitLineToDoubles(line, values); + table.push_back(values); + } + + ASSERT_EQ(10, table.size()); + ASSERT_EQ(6, table[0].size()); + ASSERT_EQ(6, table[3].size()); + + EXPECT_EQ(1, table[0][0]); + EXPECT_EQ(0.0, table[5][2]); + EXPECT_EQ(279, table[7][3]); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +TEST(RifColumnBasedRsmspecParserTest, TestKeywords) +{ + + QString data; + QTextStream out(&data); + + out << "PAGE 1\n"; + out << "ORIGIN OP-1_TR\n"; + out << "STARTDATE 1 1 2000\n"; + out << "DATEFORMAT DD/MM/YY\n"; + out << "\n"; + out << "TIME YEARX WGT1 WGT2 WGT4 WR12 WR22 WR42 \n"; + out << "DAYS YEARS kg/Sm3 kg/Sm3 kg/Sm3 kg/Sm3 kg/Sm3 kg/Sm3 \n"; + out << "1 1 1.00E+03 1.00E+03 1.00E+03 1.00E+03 1.00E+03 1.00E+03 \n"; + out << " OP-1 OP-1 OP-1 OP-1 OP-1 OP-1 \n"; + out << "\n"; + out << "\n"; + out << "1386 2003.170 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 --comment \n"; + out << "1436 2003.307 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 --comment\n"; + out << "1574 2003.685 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12\n"; + out << "1636 2003.855 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12\n"; + out << "1709 2004.055 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12\n"; + out << "\n"; + out << "\n"; + out << "PAGE 2\n"; + out << "ORIGIN OP-2_TR \n"; + out << "STARTDATE 1 1 2000\n"; + out << "DATEFORMAT DD/MM/YY\n"; + out << "\n"; + out << "TIME YEARX WGT1 WGT2 WGT4 WR12 WR22 WR42 WR31\n"; + out << "DAYS YEARS kg/Sm3 kg/Sm3 kg/Sm3 kg/Sm3 kg/Sm3 kg/Sm3 kg/Sm3\n"; + out << "1 1 1.00E+03 1.00E+03 1.00E+03 1.00E+03 1.00E+03 1.00E+03 1.00E+00\n"; + out << " OP-2 OP-2 OP-2 OP-2 OP-2 OP-2 OP-2\n"; + out << "\n"; + out << "1436 2003.307 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12\n"; + out << "1508 2003.504 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12\n"; + out << "1574 2003.685 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12\n"; + out << "1636 2003.855 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12\n"; + out << "1709 2004.055 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12 1.0E-12\n"; + out << "\n"; + + RifColumnBasedRsmspecParser parser = RifColumnBasedRsmspecParser(data); + + std::vector< std::vector > tables = parser.tables(); + ASSERT_EQ(2, tables.size()); + EXPECT_EQ("112000", tables[0].at(0).startDate); + EXPECT_EQ("OP-1_TR", tables[0].at(0).origin); + EXPECT_EQ("DD/MM/YY", tables[0].at(0).dateFormat); + + EXPECT_EQ("112000", tables[1].at(0).startDate); + EXPECT_EQ("OP-2_TR", tables[1].at(0).origin); + EXPECT_EQ("DD/MM/YY", tables[1].at(0).dateFormat); + + ASSERT_EQ(8, tables.at(0).size()); + EXPECT_EQ(1.0E-12, tables.at(0).at(4).values[0]); +} + + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -271,51 +375,6 @@ TEST(RifColumnBasedRsmspecParserTest, TestTableValues) EXPECT_NE("P-9P", tables.at(1).at(0).summaryAddress.wellName()); } - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -TEST(RifRsmspecParserToolsTest, TestSplitLineToDoubles) -{ - - QString data; - QTextStream out(&data); - - out << " 1 0.0 0.0 0.0 0.0 0.0\n"; - out << " 2 0.0 0.0 0.0 0.0 0.0\n"; - out << " 3 0.0 0.0 0.0 0.0 0.0 \n"; - out << " 4 0.0 0.0 0.0 0.0 0.0 --note\n"; - out << " 5 0.0 0.0 0.0 0.0 0.0\n"; - out << " 6 0.0 0.0 0.0 0.0 0.0\n"; - out << " 7 0.0 0.0 282 0.0 0.0 -- This is a test \n"; - out << " 8 0.0 0.0 279 0.0 0.0\n"; - out << " 9 0.0 0.0 0.0 0.0 0.0\n"; - out << " 10 0.0 0.0 0.0 0.0 0.0\n"; - - RifRsmspecParserTools parserTool; - std::stringstream streamData; - streamData.str(data.toStdString()); - std::string line; - - std::vector< std::vector > table; - - while (std::getline(streamData, line)) - { - std::vector values; - parserTool.splitLineToDoubles(line, values); - table.push_back(values); - } - - ASSERT_EQ(10, table.size()); - ASSERT_EQ(6, table[0].size()); - - EXPECT_EQ(1, table[0][0]); - EXPECT_EQ(0.0, table[5][2]); - EXPECT_EQ(279, table[7][3]); -} - - - //-------------------------------------------------------------------------------------------------- /// //--------------------------------------------------------------------------------------------------