mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#2018 CSV data. First working version of CSV import
This commit is contained in:
@@ -37,6 +37,8 @@ ${CEE_CURRENT_LIST_DIR}RifKeywordVectorUserData.h
|
||||
${CEE_CURRENT_LIST_DIR}RifDataSourceForRftPlt.h
|
||||
${CEE_CURRENT_LIST_DIR}RifDataSourceForRftPltQMetaType.h
|
||||
${CEE_CURRENT_LIST_DIR}RifEclipseUserDataKeywordTools.h
|
||||
${CEE_CURRENT_LIST_DIR}RifCsvUserData.h
|
||||
${CEE_CURRENT_LIST_DIR}RifCsvUserDataParser.h
|
||||
|
||||
# HDF5 file reader is directly included in ResInsight main CmakeList.txt
|
||||
#${CEE_CURRENT_LIST_DIR}RifHdf5Reader.h
|
||||
@@ -79,7 +81,8 @@ ${CEE_CURRENT_LIST_DIR}RifColumnBasedUserData.cpp
|
||||
${CEE_CURRENT_LIST_DIR}RifKeywordVectorUserData.cpp
|
||||
${CEE_CURRENT_LIST_DIR}RifDataSourceForRftPlt.cpp
|
||||
${CEE_CURRENT_LIST_DIR}RifEclipseUserDataKeywordTools.cpp
|
||||
|
||||
${CEE_CURRENT_LIST_DIR}RifCsvUserData.cpp
|
||||
${CEE_CURRENT_LIST_DIR}RifCsvUserDataParser.cpp
|
||||
|
||||
# HDF5 file reader is directly included in ResInsight main CmakeList.txt
|
||||
#${CEE_CURRENT_LIST_DIR}RifHdf5Reader.cpp
|
||||
|
||||
@@ -159,7 +159,7 @@ void RifColumnBasedUserData::buildTimeStepsAndMappings()
|
||||
for (size_t columIndex = 0; columIndex < tableData.columnInfos().size(); columIndex++)
|
||||
{
|
||||
const ColumnInfo& ci = tableData.columnInfos()[columIndex];
|
||||
if (!ci.isStringData)
|
||||
if (ci.dataType == ColumnInfo::NUMERIC)
|
||||
{
|
||||
RifEclipseSummaryAddress sumAddress = ci.summaryAddress;
|
||||
|
||||
@@ -264,7 +264,7 @@ std::vector<time_t> RifColumnBasedUserData::createTimeSteps(const TableData& tab
|
||||
const ColumnInfo& ci = tableData.columnInfos()[dateColumnIndex];
|
||||
|
||||
QString dateFormat;
|
||||
for (auto s : ci.stringValues)
|
||||
for (auto s : ci.textValues)
|
||||
{
|
||||
QDateTime dt = RiaDateStringParser::parseDateString(s);
|
||||
|
||||
|
||||
@@ -84,7 +84,7 @@ void RifColumnBasedUserDataParser::parseTableData(const QString& data)
|
||||
if (isFixedWidth)
|
||||
{
|
||||
auto columnInfos = RifEclipseUserDataParserTools::columnInfoForFixedColumnWidth(streamData);
|
||||
table = TableData("", "", "", columnInfos);
|
||||
table = TableData("", "", columnInfos);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -131,9 +131,9 @@ void RifColumnBasedUserDataParser::parseTableData(const QString& data)
|
||||
|
||||
for (int i = 0; i < columnCount; i++)
|
||||
{
|
||||
if (columnInfos[i].isStringData)
|
||||
if (columnInfos[i].dataType == ColumnInfo::TEXT)
|
||||
{
|
||||
columnInfos[i].stringValues.push_back(entries[i].toStdString());
|
||||
columnInfos[i].textValues.push_back(entries[i].toStdString());
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
187
ApplicationCode/FileInterface/RifCsvUserData.cpp
Normal file
187
ApplicationCode/FileInterface/RifCsvUserData.cpp
Normal file
@@ -0,0 +1,187 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2017- Statoil ASA
|
||||
//
|
||||
// ResInsight is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "RifCsvUserData.h"
|
||||
|
||||
#include "RiaDateStringParser.h"
|
||||
#include "RiaLogging.h"
|
||||
#include "RiaQDateTimeTools.h"
|
||||
|
||||
#include "RifCsvUserDataParser.h"
|
||||
#include "RifEclipseUserDataKeywordTools.h"
|
||||
#include "RifEclipseUserDataParserTools.h"
|
||||
|
||||
#include "cafUtils.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RifCsvUserData::RifCsvUserData()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RifCsvUserData::~RifCsvUserData()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RifCsvUserData::parse(const QString& data, const AsciiDataParseOptions& parseOptions, QString* errorText)
|
||||
{
|
||||
m_allResultAddresses.clear();
|
||||
m_timeSteps.clear();
|
||||
m_mapFromAddressToTimeStepIndex.clear();
|
||||
m_mapFromAddressToResultIndex.clear();
|
||||
|
||||
m_parser = std::unique_ptr<RifCsvUserDataParser>(new RifCsvUserDataParser(errorText));
|
||||
m_parser->parse(data, parseOptions);
|
||||
if (!m_parser)
|
||||
{
|
||||
RiaLogging::error(QString("Failed to parse file"));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
buildTimeStepsAndMappings();
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RifCsvUserData::values(const RifEclipseSummaryAddress& resultAddress, std::vector<double>* values) const
|
||||
{
|
||||
auto search = m_mapFromAddressToResultIndex.find(resultAddress);
|
||||
if (search != m_mapFromAddressToResultIndex.end())
|
||||
{
|
||||
size_t columnIndex = search->second;
|
||||
|
||||
const ColumnInfo* ci = m_parser->columnInfo(columnIndex);
|
||||
if (!ci) return false;
|
||||
|
||||
values->clear();
|
||||
values->reserve(ci->values.size());
|
||||
for (double val : ci->values)
|
||||
{
|
||||
values->push_back(val);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
const std::vector<time_t>& RifCsvUserData::timeSteps(const RifEclipseSummaryAddress& resultAddress) const
|
||||
{
|
||||
auto search = m_mapFromAddressToTimeStepIndex.find(resultAddress);
|
||||
if (search != m_mapFromAddressToTimeStepIndex.end())
|
||||
{
|
||||
return m_timeSteps;
|
||||
}
|
||||
|
||||
static std::vector<time_t> emptyVector;
|
||||
|
||||
return emptyVector;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
std::string RifCsvUserData::unitName(const RifEclipseSummaryAddress& resultAddress) const
|
||||
{
|
||||
auto search = m_mapFromAddressToResultIndex.find(resultAddress);
|
||||
if (search != m_mapFromAddressToResultIndex.end())
|
||||
{
|
||||
size_t columnIndex = search->second;
|
||||
|
||||
const ColumnInfo* ci = m_parser->columnInfo(columnIndex);
|
||||
if (ci)
|
||||
{
|
||||
return ci->unitName;
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RifCsvUserData::buildTimeStepsAndMappings()
|
||||
{
|
||||
auto tableData = m_parser->tableData();
|
||||
|
||||
std::vector<time_t> timeStepsForTable = createTimeSteps(tableData);
|
||||
|
||||
if (timeStepsForTable.empty())
|
||||
{
|
||||
RiaLogging::warning(QString("Failed to find time data for table in file"));
|
||||
RiaLogging::warning(QString("No data for this table is imported"));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
m_timeSteps = timeStepsForTable;
|
||||
|
||||
|
||||
for (size_t columnIndex = 0; columnIndex < tableData.columnInfos().size(); columnIndex++)
|
||||
{
|
||||
const ColumnInfo& ci = tableData.columnInfos()[columnIndex];
|
||||
if (ci.dataType == ColumnInfo::NUMERIC)
|
||||
{
|
||||
RifEclipseSummaryAddress sumAddress = ci.summaryAddress;
|
||||
|
||||
m_allResultAddresses.push_back(sumAddress);
|
||||
|
||||
m_mapFromAddressToTimeStepIndex[sumAddress] = m_timeSteps.size() - 1;
|
||||
m_mapFromAddressToResultIndex[sumAddress] = columnIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
std::vector<time_t> RifCsvUserData::createTimeSteps(const TableData& tableData)
|
||||
{
|
||||
std::vector<time_t> tsVector;
|
||||
|
||||
const ColumnInfo& col = tableData.columnInfos()[0];
|
||||
|
||||
tsVector.reserve(col.dateTimeValues.size());
|
||||
for (const QDateTime& qdt : col.dateTimeValues)
|
||||
{
|
||||
tsVector.push_back(qdt.toTime_t());
|
||||
}
|
||||
|
||||
return tsVector;
|
||||
}
|
||||
65
ApplicationCode/FileInterface/RifCsvUserData.h
Normal file
65
ApplicationCode/FileInterface/RifCsvUserData.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2017- Statoil ASA
|
||||
//
|
||||
// ResInsight is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "RifSummaryReaderInterface.h"
|
||||
|
||||
#include "../Commands/SummaryPlotCommands/RicPasteAsciiDataToSummaryPlotFeatureUi.h"
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
class QString;
|
||||
|
||||
class RifCsvUserDataParser;
|
||||
class RifEclipseSummaryAddress;
|
||||
class TableData;
|
||||
|
||||
//==================================================================================================
|
||||
//
|
||||
//
|
||||
//==================================================================================================
|
||||
class RifCsvUserData : public RifSummaryReaderInterface
|
||||
{
|
||||
public:
|
||||
RifCsvUserData();
|
||||
~RifCsvUserData();
|
||||
|
||||
bool parse(const QString& data, const AsciiDataParseOptions& parseOptions, QString* errorText = nullptr);
|
||||
|
||||
virtual const std::vector<time_t>& timeSteps(const RifEclipseSummaryAddress& resultAddress) const override;
|
||||
|
||||
virtual bool values(const RifEclipseSummaryAddress& resultAddress,
|
||||
std::vector<double>* values) const override;
|
||||
|
||||
std::string unitName(const RifEclipseSummaryAddress& resultAddress) const override;
|
||||
|
||||
private:
|
||||
void buildTimeStepsAndMappings();
|
||||
static std::vector<time_t> createTimeSteps(const TableData& table);
|
||||
|
||||
private:
|
||||
std::unique_ptr<RifCsvUserDataParser> m_parser;
|
||||
std::vector<time_t> m_timeSteps;
|
||||
|
||||
std::map<RifEclipseSummaryAddress, size_t > m_mapFromAddressToTimeStepIndex;
|
||||
std::map<RifEclipseSummaryAddress, size_t > m_mapFromAddressToResultIndex;
|
||||
};
|
||||
276
ApplicationCode/FileInterface/RifCsvUserDataParser.cpp
Normal file
276
ApplicationCode/FileInterface/RifCsvUserDataParser.cpp
Normal file
@@ -0,0 +1,276 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2017- Statoil ASA
|
||||
//
|
||||
// ResInsight is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "RifCsvUserDataParser.h"
|
||||
|
||||
#include "RifEclipseUserDataKeywordTools.h"
|
||||
#include "RifEclipseUserDataParserTools.h"
|
||||
|
||||
#include "RiaDateStringParser.h"
|
||||
#include "RiaLogging.h"
|
||||
#include "RiaStdStringTools.h"
|
||||
#include "RiaQDateTimeTools.h"
|
||||
|
||||
#include "cvfAssert.h"
|
||||
|
||||
#include <QString>
|
||||
#include <QTextStream>
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RifCsvUserDataParser::RifCsvUserDataParser(QString* errorText)
|
||||
: m_errorText(errorText)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RifCsvUserDataParser::parse(const QString& data, const AsciiDataParseOptions& parseOptions)
|
||||
{
|
||||
return parseData(data, parseOptions);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
const TableData& RifCsvUserDataParser::tableData() const
|
||||
{
|
||||
return m_tableData;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
const ColumnInfo* RifCsvUserDataParser::columnInfo(size_t columnIndex) const
|
||||
{
|
||||
if (columnIndex >= m_tableData.columnInfos().size()) return nullptr;
|
||||
|
||||
return &(m_tableData.columnInfos()[columnIndex]);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RifCsvUserDataParser::parseData(const QString& data, const AsciiDataParseOptions& parseOptions)
|
||||
{
|
||||
enum { HEADER_ROW, FIRST_DATA_ROW, DATA_ROW } parseState = HEADER_ROW;
|
||||
int colCount = -1;
|
||||
std::vector<ColumnInfo> cols;
|
||||
|
||||
QTextStream dataStream(const_cast<QString*>(&data));
|
||||
while (!dataStream.atEnd())
|
||||
{
|
||||
QString line = dataStream.readLine();
|
||||
if(line.trimmed().isEmpty()) continue;
|
||||
|
||||
QStringList lineColumns = splitLineAndTrim(line, parseOptions.cellSeparator);
|
||||
|
||||
if (parseState == HEADER_ROW)
|
||||
{
|
||||
colCount = lineColumns.size();
|
||||
|
||||
for (int iCol = 0; iCol < colCount; iCol++)
|
||||
{
|
||||
std::string colName = lineColumns[iCol].toStdString();
|
||||
RifEclipseSummaryAddress addr = RifEclipseSummaryAddress::importedAddress(colName);
|
||||
ColumnInfo col = ColumnInfo::createColumnInfoFromCsvData(addr, "");
|
||||
cols.push_back(col);
|
||||
}
|
||||
|
||||
parseState = FIRST_DATA_ROW;
|
||||
}
|
||||
else if(lineColumns.size() != colCount)
|
||||
{
|
||||
m_errorText->append("CSV file has invalid content (Column count mismatch)");
|
||||
return false;
|
||||
}
|
||||
else if(parseState == FIRST_DATA_ROW)
|
||||
{
|
||||
for (int iCol = 0; iCol < colCount; iCol++)
|
||||
{
|
||||
std::string colData = lineColumns[iCol].toStdString();
|
||||
ColumnInfo& col = cols[iCol];
|
||||
|
||||
// Check if text column
|
||||
if (RiaStdStringTools::isNumber(colData))
|
||||
{
|
||||
col.dataType = ColumnInfo::NUMERIC;
|
||||
}
|
||||
else if (tryParseDateTime(colData, parseOptions.dateTimeFormat()).isValid() ||
|
||||
tryParseDateTime(colData, parseOptions.dateFormat_).isValid())
|
||||
{
|
||||
col.dataType = ColumnInfo::DATETIME;
|
||||
}
|
||||
else
|
||||
{
|
||||
col.dataType = ColumnInfo::TEXT;
|
||||
}
|
||||
}
|
||||
|
||||
parseState = DATA_ROW;
|
||||
}
|
||||
|
||||
if (parseState == DATA_ROW)
|
||||
{
|
||||
for (int iCol = 0; iCol < colCount; iCol++)
|
||||
{
|
||||
std::string colData = lineColumns[iCol].toStdString();
|
||||
ColumnInfo& col = cols[iCol];
|
||||
|
||||
try
|
||||
{
|
||||
if (col.dataType == ColumnInfo::NUMERIC)
|
||||
{
|
||||
col.values.push_back(RiaStdStringTools::toDouble(colData));
|
||||
}
|
||||
else if (col.dataType == ColumnInfo::TEXT)
|
||||
{
|
||||
col.textValues.push_back(colData);
|
||||
}
|
||||
else if (col.dataType == ColumnInfo::DATETIME)
|
||||
{
|
||||
QDateTime dt = tryParseDateTime(colData, parseOptions.dateTimeFormat());
|
||||
if (!dt.isValid())
|
||||
{
|
||||
dt = tryParseDateTime(colData, parseOptions.dateFormat_);
|
||||
}
|
||||
if (!dt.isValid()) throw 0;
|
||||
col.dateTimeValues.push_back(dt);
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
m_errorText->append("CSV file has invalid content (Column type mismatch)");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TableData td("", "", cols);
|
||||
m_tableData = td;
|
||||
return true;
|
||||
|
||||
//std::string stdData = data.toStdString();
|
||||
//bool isFixedWidth = RifEclipseUserDataParserTools::isFixedWidthHeader(stdData);
|
||||
|
||||
//std::stringstream streamData;
|
||||
//streamData.str(stdData);
|
||||
|
||||
//std::vector<TableData> rawTables;
|
||||
|
||||
//do
|
||||
//{
|
||||
// std::vector<std::string> errorStrings;
|
||||
|
||||
// TableData table;
|
||||
|
||||
// if (isFixedWidth)
|
||||
// {
|
||||
// auto columnInfos = RifEclipseUserDataParserTools::columnInfoForFixedColumnWidth(streamData);
|
||||
// table = TableData("", "", "", columnInfos);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// table = RifEclipseUserDataParserTools::tableDataFromText(streamData, &errorStrings);
|
||||
// }
|
||||
|
||||
// if (m_errorText)
|
||||
// {
|
||||
// for (auto s : errorStrings)
|
||||
// {
|
||||
// QString errorText = QString("\n%1").arg(QString::fromStdString(s));
|
||||
// m_errorText->append(errorText);
|
||||
// }
|
||||
// }
|
||||
|
||||
// std::vector<ColumnInfo>& columnInfos = table.columnInfos();
|
||||
// int columnCount = static_cast<int>(columnInfos.size());
|
||||
// if (columnCount == 0) break;
|
||||
|
||||
// int stepTypeIndex = -1;
|
||||
// for (size_t i = 0; i < columnInfos.size(); i++)
|
||||
// {
|
||||
// if (RifEclipseUserDataKeywordTools::isStepType(columnInfos[i].summaryAddress.quantityName()))
|
||||
// {
|
||||
// stepTypeIndex = static_cast<int>(i);
|
||||
// }
|
||||
// }
|
||||
|
||||
// std::string line;
|
||||
// std::getline(streamData, line);
|
||||
|
||||
// do
|
||||
// {
|
||||
// QString qLine = QString::fromStdString(line);
|
||||
// QStringList entries = qLine.split(" ", QString::SkipEmptyParts);
|
||||
|
||||
// if (stepTypeIndex > -1 &&
|
||||
// (unsigned int)entries.size() < columnInfos.size())
|
||||
// {
|
||||
// entries.insert(stepTypeIndex, " ");
|
||||
// }
|
||||
|
||||
// if (entries.size() < columnCount) break;
|
||||
|
||||
// for (int i = 0; i < columnCount; i++)
|
||||
// {
|
||||
// if (columnInfos[i].dataType == ColumnInfo::NUMERIC)
|
||||
// {
|
||||
// double entry = entries[i].toDouble();
|
||||
// columnInfos[i].values.push_back(entry);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// columnInfos[i].textValues.push_back(entries[i].toStdString());
|
||||
// }
|
||||
// }
|
||||
// } while (std::getline(streamData, line));
|
||||
|
||||
// rawTables.push_back(table);
|
||||
|
||||
//} while (streamData.good());
|
||||
|
||||
//m_tableDatas = RifEclipseUserDataParserTools::mergeEqualTimeSteps(rawTables);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QStringList RifCsvUserDataParser::splitLineAndTrim(const QString& line, const QString& separator)
|
||||
{
|
||||
QStringList cols = line.split(separator);
|
||||
for (QString& col : cols)
|
||||
{
|
||||
col = col.trimmed();
|
||||
}
|
||||
return cols;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QDateTime RifCsvUserDataParser::tryParseDateTime(const std::string& colData, const QString& format)
|
||||
{
|
||||
return RiaQDateTimeTools::fromString(QString::fromStdString(colData), format);
|
||||
}
|
||||
57
ApplicationCode/FileInterface/RifCsvUserDataParser.h
Normal file
57
ApplicationCode/FileInterface/RifCsvUserDataParser.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2017- Statoil ASA
|
||||
//
|
||||
// ResInsight is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "RifEclipseSummaryAddress.h"
|
||||
#include "RifEclipseUserDataParserTools.h"
|
||||
|
||||
#include "../Commands/SummaryPlotCommands/RicPasteAsciiDataToSummaryPlotFeatureUi.h"
|
||||
|
||||
#include <QString>
|
||||
#include <QPointer>
|
||||
#include <QStringList>
|
||||
#include <QDateTime>
|
||||
|
||||
#include <vector>
|
||||
|
||||
class ColumnInfo;
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
//==================================================================================================
|
||||
class RifCsvUserDataParser
|
||||
{
|
||||
public:
|
||||
RifCsvUserDataParser(QString* errorText = nullptr);
|
||||
|
||||
|
||||
bool parse(const QString& data, const AsciiDataParseOptions& parseOptions);
|
||||
const TableData& tableData() const;
|
||||
|
||||
const ColumnInfo* columnInfo(size_t columnIndex) const;
|
||||
|
||||
private:
|
||||
bool parseData(const QString& data, const AsciiDataParseOptions& parseOptions);
|
||||
QStringList splitLineAndTrim(const QString& list, const QString& separator);
|
||||
QDateTime tryParseDateTime(const std::string& colData, const QString& format);
|
||||
|
||||
private:
|
||||
TableData m_tableData;
|
||||
QString* m_errorText;
|
||||
};
|
||||
@@ -121,10 +121,10 @@ RifEclipseSummaryAddress RifEclipseSummaryAddress::calculatedCurveAddress(const
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RifEclipseSummaryAddress RifEclipseSummaryAddress::miscAddress(const std::string& quantityName)
|
||||
RifEclipseSummaryAddress RifEclipseSummaryAddress::importedAddress(const std::string& quantityName)
|
||||
{
|
||||
RifEclipseSummaryAddress fieldAddr;
|
||||
fieldAddr.m_variableCategory = SUMMARY_MISC;
|
||||
fieldAddr.m_variableCategory = SUMMARY_IMPORTED;
|
||||
fieldAddr.m_quantityName = quantityName;
|
||||
|
||||
return fieldAddr;
|
||||
|
||||
@@ -46,7 +46,8 @@ public:
|
||||
SUMMARY_WELL_SEGMENT,
|
||||
SUMMARY_BLOCK,
|
||||
SUMMARY_BLOCK_LGR,
|
||||
SUMMARY_CALCULATED
|
||||
SUMMARY_CALCULATED,
|
||||
SUMMARY_IMPORTED
|
||||
};
|
||||
|
||||
enum SummaryIdentifierType
|
||||
@@ -106,7 +107,7 @@ public:
|
||||
|
||||
static RifEclipseSummaryAddress fieldVarAddress(const std::string& fieldVarName);
|
||||
static RifEclipseSummaryAddress calculatedCurveAddress(const std::string& curveName);
|
||||
static RifEclipseSummaryAddress miscAddress(const std::string& quantityName);
|
||||
static RifEclipseSummaryAddress importedAddress(const std::string& quantityName);
|
||||
|
||||
// Access methods
|
||||
|
||||
|
||||
@@ -155,7 +155,7 @@ RifEclipseSummaryAddress RifEclipseUserDataKeywordTools::makeAndFillAddress(cons
|
||||
|
||||
if (category == RifEclipseSummaryAddress::SUMMARY_INVALID)
|
||||
{
|
||||
return RifEclipseSummaryAddress::miscAddress(quantityName);
|
||||
return RifEclipseSummaryAddress::importedAddress(quantityName);
|
||||
}
|
||||
|
||||
int regionNumber = -1;
|
||||
|
||||
@@ -494,13 +494,13 @@ TableData RifEclipseUserDataParserTools::tableDataFromText(std::stringstream& st
|
||||
|
||||
RifEclipseSummaryAddress adr = RifEclipseUserDataKeywordTools::makeAndFillAddress(quantity, columnHeader);
|
||||
|
||||
ColumnInfo ci = ColumnInfo::createColumnInfo(quantity, unit, adr);
|
||||
ColumnInfo ci = ColumnInfo::createColumnInfoFromRsmData(quantity, unit, adr);
|
||||
|
||||
columnInfos.push_back(ci);
|
||||
}
|
||||
}
|
||||
|
||||
return TableData(origin, dateFormat, startDate, columnInfos);
|
||||
return TableData(origin, startDate, columnInfos);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@@ -774,7 +774,7 @@ std::vector<ColumnInfo> RifEclipseUserDataParserTools::columnInfoFromColumnHeade
|
||||
|
||||
RifEclipseSummaryAddress adr = RifEclipseUserDataKeywordTools::makeAndFillAddress(quantity, restOfHeader);
|
||||
|
||||
ColumnInfo ci = ColumnInfo::createColumnInfo(quantity, unit, adr);
|
||||
ColumnInfo ci = ColumnInfo::createColumnInfoFromRsmData(quantity, unit, adr);
|
||||
|
||||
table.push_back(ci);
|
||||
}
|
||||
@@ -818,9 +818,9 @@ std::vector<TableData> RifEclipseUserDataParserTools::mergeEqualTimeSteps(const
|
||||
{
|
||||
if (c.summaryAddress.quantityName() == "DATE")
|
||||
{
|
||||
if (c.stringValues.size() > 0)
|
||||
if (c.itemCount() > 0)
|
||||
{
|
||||
firstTableStartTime = RiaDateStringParser::parseDateString(c.stringValues[0]);
|
||||
firstTableStartTime = RiaDateStringParser::parseDateString(c.textValues[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -848,9 +848,9 @@ std::vector<TableData> RifEclipseUserDataParserTools::mergeEqualTimeSteps(const
|
||||
{
|
||||
if (c.summaryAddress.quantityName() == "DATE")
|
||||
{
|
||||
if (c.stringValues.size() > 0)
|
||||
if (c.itemCount() > 0)
|
||||
{
|
||||
tableFirstTime = RiaDateStringParser::parseDateString(c.stringValues[0]);
|
||||
tableFirstTime = RiaDateStringParser::parseDateString(c.textValues[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -918,38 +918,67 @@ bool RifEclipseUserDataParserTools::isScalingText(const std::string& word)
|
||||
return word.find_first_of('*') != std::string::npos;
|
||||
}
|
||||
|
||||
////--------------------------------------------------------------------------------------------------
|
||||
/////
|
||||
////--------------------------------------------------------------------------------------------------
|
||||
//ColumnInfo::~ColumnInfo()
|
||||
//{
|
||||
// if (values)
|
||||
// {
|
||||
// delete values;
|
||||
// }
|
||||
// if (textValues)
|
||||
// {
|
||||
// delete textValues;
|
||||
// }
|
||||
// if (dateTimeValues)
|
||||
// {
|
||||
// delete dateTimeValues;
|
||||
// }
|
||||
//}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
size_t ColumnInfo::itemCount() const
|
||||
{
|
||||
if (isStringData)
|
||||
switch (dataType)
|
||||
{
|
||||
return stringValues.size();
|
||||
case NUMERIC: return values.size();
|
||||
case TEXT: return textValues.size();
|
||||
case DATETIME: return dateTimeValues.size();
|
||||
default: return 0;
|
||||
}
|
||||
else
|
||||
return values.size();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
ColumnInfo ColumnInfo::createColumnInfo(const std::string& quantity, const std::string& unit, const RifEclipseSummaryAddress& adr)
|
||||
ColumnInfo ColumnInfo::createColumnInfoFromRsmData(const std::string& quantity, const std::string& unit, const RifEclipseSummaryAddress& adr)
|
||||
{
|
||||
ColumnInfo ci(adr, unit);
|
||||
|
||||
if (RifEclipseUserDataKeywordTools::isDate(quantity))
|
||||
{
|
||||
ci.isStringData = true;
|
||||
ci.dataType = TEXT;
|
||||
}
|
||||
else if (RifEclipseUserDataKeywordTools::isStepType(quantity))
|
||||
{
|
||||
ci.isStringData = true;
|
||||
ci.dataType = TEXT;
|
||||
}
|
||||
|
||||
return ci;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
ColumnInfo ColumnInfo::createColumnInfoFromCsvData(const RifEclipseSummaryAddress& addr, const std::string& unit)
|
||||
{
|
||||
ColumnInfo col(addr, unit);
|
||||
return col;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@@ -961,9 +990,9 @@ QDateTime TableData::findFirstDate() const
|
||||
{
|
||||
if (RifEclipseUserDataKeywordTools::isDate(ci.summaryAddress.quantityName()))
|
||||
{
|
||||
if (ci.stringValues.size() > 0)
|
||||
if (ci.itemCount() > 0)
|
||||
{
|
||||
std::string firstDateString = ci.stringValues[0];
|
||||
std::string firstDateString = ci.textValues[0];
|
||||
|
||||
QDateTime candidate = RiaDateStringParser::parseDateString(firstDateString);
|
||||
if (candidate.isValid())
|
||||
|
||||
@@ -34,9 +34,17 @@
|
||||
class ColumnInfo
|
||||
{
|
||||
public:
|
||||
enum DataType
|
||||
{
|
||||
NONE,
|
||||
NUMERIC,
|
||||
TEXT,
|
||||
DATETIME
|
||||
};
|
||||
|
||||
ColumnInfo()
|
||||
: scaleFactor(1.0),
|
||||
isStringData(false)
|
||||
dataType(NONE)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -44,23 +52,28 @@ public:
|
||||
: summaryAddress(adr),
|
||||
scaleFactor(1.0),
|
||||
unitName(unit),
|
||||
isStringData(false)
|
||||
dataType(NONE)
|
||||
{
|
||||
}
|
||||
|
||||
size_t itemCount() const;
|
||||
size_t itemCount() const;
|
||||
|
||||
public:
|
||||
static ColumnInfo createColumnInfo(const std::string& quantity, const std::string& unit, const RifEclipseSummaryAddress& adr);
|
||||
static ColumnInfo createColumnInfoFromRsmData(const std::string& quantity, const std::string& unit, const RifEclipseSummaryAddress& adr);
|
||||
static ColumnInfo createColumnInfoFromCsvData(const RifEclipseSummaryAddress& addr, const std::string& unit);
|
||||
|
||||
RifEclipseSummaryAddress summaryAddress;
|
||||
std::string unitName;
|
||||
double scaleFactor;
|
||||
DataType dataType;
|
||||
|
||||
// Data containers
|
||||
std::vector<double> values;
|
||||
bool isStringData;
|
||||
std::vector<std::string> stringValues;
|
||||
std::vector<std::string > textValues;
|
||||
std::vector<QDateTime> dateTimeValues;
|
||||
};
|
||||
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
//==================================================================================================
|
||||
@@ -71,11 +84,9 @@ public:
|
||||
{}
|
||||
|
||||
TableData(const std::string& origin,
|
||||
const std::string& dateFormat,
|
||||
const std::string& startDate,
|
||||
const std::vector<ColumnInfo>& columnInfos)
|
||||
: m_origin(origin),
|
||||
m_dateFormat(dateFormat),
|
||||
m_startDate(startDate),
|
||||
m_columnInfos(columnInfos)
|
||||
{
|
||||
@@ -91,11 +102,6 @@ public:
|
||||
return m_startDate;
|
||||
}
|
||||
|
||||
std::string dateFormat() const
|
||||
{
|
||||
return m_dateFormat;
|
||||
}
|
||||
|
||||
std::vector<ColumnInfo>& columnInfos()
|
||||
{
|
||||
return m_columnInfos;
|
||||
@@ -110,7 +116,6 @@ public:
|
||||
|
||||
private:
|
||||
std::string m_origin;
|
||||
std::string m_dateFormat;
|
||||
std::string m_startDate;
|
||||
|
||||
std::vector<ColumnInfo> m_columnInfos;
|
||||
|
||||
Reference in New Issue
Block a user