#1922 Observed data: Update column parser to handle keywords

This commit is contained in:
Rebecca Cox 2017-09-26 13:14:00 +02:00
parent 745bed2573
commit 93971e541c
3 changed files with 143 additions and 105 deletions

View File

@ -26,12 +26,15 @@
#include <QStringList>
#include <QTextStream>
#include <algorithm>
#include <numeric>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
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<std::string> RifRsmspecParserTools::splitLine(const std::string& line)
{
std::istringstream iss(line);
std::vector<std::string> words{ std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>{} };
return words;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -85,7 +77,7 @@ bool RifRsmspecParserTools::isAComment(const std::string& word)
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<std::string> RifRsmspecParserTools::splitLineAndRemoveComments(const std::string& line)
std::vector<std::string> RifRsmspecParserTools::splitLineAndRemoveComments(std::string line)
{
std::istringstream iss(line);
std::vector<std::string> words{ std::istream_iterator<std::string>{iss},
@ -103,44 +95,6 @@ std::vector<std::string> 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<std::string> 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<ColumnInfo> 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<ColumnInfo> 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);
}
}

View File

@ -24,9 +24,9 @@
#include <QLocale>
#include <QString>
#include <sstream>
#include <string>
#include <vector>
#include <sstream>
struct ColumnInfo
{
@ -36,6 +36,7 @@ struct ColumnInfo
std::string scaleFactor;
std::vector<double> 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<std::string> splitLine(const std::string& line);
static bool isAComment(const std::string& word);
static std::vector<std::string> splitLineAndRemoveComments(const std::string& line);
static bool canBeAMnemonic(const std::string& word);
static std::vector<std::string> splitLineAndRemoveComments(std::string line);
static RifEclipseSummaryAddress::SummaryVarCategory identifyCategory(const std::string& word);
static void splitLineToDoubles(const std::string& line, std::vector<double>& values);
static size_t findFirstNonEmptyEntryIndex(std::vector<std::string>& 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<ColumnInfo> columnInfoForTable(std::stringstream& data, std::string& line);
static bool isANumber(const std::string& line);
static std::vector<std::string> headerReader(std::stringstream& streamData, std::string& line);

View File

@ -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<double> > table;
while (std::getline(streamData, line))
{
std::vector<double> 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<ColumnInfo> > 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<double> > table;
while (std::getline(streamData, line))
{
std::vector<double> 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]);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------