mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#2610 Add RifEnsambleParametersReader and RifFileParseTools
This commit is contained in:
@@ -39,6 +39,8 @@ ${CMAKE_CURRENT_LIST_DIR}/RifElementPropertyTableReader.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifElementPropertyReader.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifStimPlanXmlReader.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifSummaryCaseRestartSelector.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifEnsambleParametersReader.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifFileParseTools.h
|
||||
|
||||
# HDF5 file reader is directly included in ResInsight main CmakeList.txt
|
||||
#${CMAKE_CURRENT_LIST_DIR}/RifHdf5Reader.h
|
||||
@@ -82,6 +84,8 @@ ${CMAKE_CURRENT_LIST_DIR}/RifElementPropertyTableReader.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifElementPropertyReader.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifStimPlanXmlReader.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifSummaryCaseRestartSelector.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifEnsambleParametersReader.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifFileParseTools.cpp
|
||||
|
||||
# HDF5 file reader is directly included in ResInsight main CmakeList.txt
|
||||
#${CMAKE_CURRENT_LIST_DIR}/RifHdf5Reader.cpp
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#include "RifEclipseUserDataKeywordTools.h"
|
||||
#include "RifEclipseUserDataParserTools.h"
|
||||
#include "RifFileParseTools.h"
|
||||
|
||||
#include "RiaDateStringParser.h"
|
||||
#include "RiaLogging.h"
|
||||
@@ -136,7 +137,7 @@ QString RifCsvUserDataParser::previewText(int lineCount, const AsciiDataParseOpt
|
||||
|
||||
outStream << "<tr>";
|
||||
int iCol = 0;
|
||||
QStringList cols = splitLineAndTrim(line, parseOptions.cellSeparator);
|
||||
QStringList cols = RifFileParseTools::splitLineAndTrim(line, parseOptions.cellSeparator);
|
||||
for (const QString& cellData : cols)
|
||||
{
|
||||
if (cellData == parseOptions.timeSeriesColumnName && header)
|
||||
@@ -187,7 +188,7 @@ bool RifCsvUserDataParser::parseColumnInfo(QTextStream* dataStream, const AsciiD
|
||||
QString line = dataStream->readLine();
|
||||
if (line.trimmed().isEmpty()) continue;
|
||||
|
||||
QStringList lineColumns = splitLineAndTrim(line, parseOptions.cellSeparator);
|
||||
QStringList lineColumns = RifFileParseTools::splitLineAndTrim(line, parseOptions.cellSeparator);
|
||||
|
||||
int colCount = lineColumns.size();
|
||||
|
||||
@@ -230,7 +231,7 @@ bool RifCsvUserDataParser::parseData(const AsciiDataParseOptions& parseOptions)
|
||||
QString line = dataStream->readLine();
|
||||
if(line.trimmed().isEmpty()) continue;
|
||||
|
||||
QStringList lineColumns = splitLineAndTrim(line, parseOptions.cellSeparator);
|
||||
QStringList lineColumns = RifFileParseTools::splitLineAndTrim(line, parseOptions.cellSeparator);
|
||||
|
||||
if(lineColumns.size() != colCount)
|
||||
{
|
||||
@@ -339,19 +340,6 @@ bool RifCsvUserDataParser::parseData(const AsciiDataParseOptions& parseOptions)
|
||||
return !errors;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QStringList RifCsvUserDataParser::splitLineAndTrim(const QString& line, const QString& separator)
|
||||
{
|
||||
QStringList cols = line.split(separator);
|
||||
for (QString& col : cols)
|
||||
{
|
||||
col = col.trimmed();
|
||||
}
|
||||
return cols;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@@ -386,9 +374,9 @@ QString RifCsvUserDataParser::tryDetermineCellSeparator()
|
||||
|
||||
for (const QString& line : lines)
|
||||
{
|
||||
totColumnCountTab += splitLineAndTrim(line, "\t").size();
|
||||
totColumnCountSemicolon += splitLineAndTrim(line, ";").size();
|
||||
totColumnCountComma += splitLineAndTrim(line, ",").size();
|
||||
totColumnCountTab += RifFileParseTools::splitLineAndTrim(line, "\t").size();
|
||||
totColumnCountSemicolon += RifFileParseTools::splitLineAndTrim(line, ";").size();
|
||||
totColumnCountComma += RifFileParseTools::splitLineAndTrim(line, ",").size();
|
||||
}
|
||||
|
||||
double avgColumnCountTab = (double)totColumnCountTab / lines.size();
|
||||
@@ -421,7 +409,7 @@ QString RifCsvUserDataParser::tryDetermineDecimalSeparator(const QString& cellSe
|
||||
QString line = dataStream->readLine();
|
||||
if (line.isEmpty()) continue;
|
||||
|
||||
for (const QString& cellData : splitLineAndTrim(line, cellSeparator))
|
||||
for (const QString& cellData : RifFileParseTools::splitLineAndTrim(line, cellSeparator))
|
||||
{
|
||||
bool parseOk;
|
||||
QLocale locale;
|
||||
|
||||
@@ -66,7 +66,6 @@ private:
|
||||
const AsciiDataParseOptions& parseOptions,
|
||||
std::vector<Column>* columnInfoList);
|
||||
bool parseData(const AsciiDataParseOptions& parseOptions);
|
||||
static QStringList splitLineAndTrim(const QString& line, const QString& separator);
|
||||
static QDateTime tryParseDateTime(const std::string& colData, const QString& format);
|
||||
|
||||
private:
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "RifElementPropertyTableReader.h"
|
||||
#include "RifFileParseTools.h"
|
||||
|
||||
#include "RiaLogging.h"
|
||||
#include "RiuMainWindow.h"
|
||||
@@ -36,7 +37,6 @@
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
static QFile* openFile(const QString &fileName);
|
||||
static void closeFile(QFile *file);
|
||||
static QStringList splitLineAndTrim(const QString& line, const QString& separator);
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
@@ -70,7 +70,7 @@ RifElementPropertyMetadata RifElementPropertyTableReader::readMetadata(const QSt
|
||||
|
||||
if (!metadataBlockFound) continue;
|
||||
|
||||
QStringList cols = splitLineAndTrim(line, ",");
|
||||
QStringList cols = RifFileParseTools::splitLineAndTrim(line, ",");
|
||||
|
||||
metadata.fileName = fileName;
|
||||
for (QString s : cols)
|
||||
@@ -119,7 +119,7 @@ void RifElementPropertyTableReader::readData(const RifElementPropertyMetadata *m
|
||||
while (!stream.atEnd())
|
||||
{
|
||||
QString line = stream.readLine();
|
||||
QStringList cols = splitLineAndTrim(line, ",");
|
||||
QStringList cols = RifFileParseTools::splitLineAndTrim(line, ",");
|
||||
lineNo++;
|
||||
|
||||
if (!dataBlockFound)
|
||||
@@ -201,16 +201,3 @@ void closeFile(QFile *file)
|
||||
delete file;
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QStringList splitLineAndTrim(const QString& line, const QString& separator)
|
||||
{
|
||||
QStringList cols = line.split(separator);
|
||||
for (QString& col : cols)
|
||||
{
|
||||
col = col.trimmed();
|
||||
}
|
||||
return cols;
|
||||
}
|
||||
|
||||
@@ -40,15 +40,6 @@ public:
|
||||
static void readData(const RifElementPropertyMetadata *metadata, RifElementPropertyTable *table);
|
||||
};
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
//==================================================================================================
|
||||
class FileParseException
|
||||
{
|
||||
public:
|
||||
FileParseException(const QString &message) : message(message) {}
|
||||
QString message;
|
||||
};
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
|
||||
212
ApplicationCode/FileInterface/RifEnsambleParametersReader.cpp
Normal file
212
ApplicationCode/FileInterface/RifEnsambleParametersReader.cpp
Normal file
@@ -0,0 +1,212 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 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 "RifEnsambleParametersReader.h"
|
||||
#include "RifFileParseTools.h"
|
||||
|
||||
#include "RiaLogging.h"
|
||||
#include "RiaStdStringTools.h"
|
||||
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QDir>
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// Constants
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
#define PARAMETERS_FILE_NAME "parameters.txt"
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RifEnsambleParametersReader::RifEnsambleParametersReader(const QString& fileName)
|
||||
{
|
||||
m_fileName = fileName;
|
||||
m_file = nullptr;
|
||||
m_textStream = nullptr;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RifEnsambleParametersReader::~RifEnsambleParametersReader()
|
||||
{
|
||||
if (m_textStream)
|
||||
{
|
||||
delete m_textStream;
|
||||
}
|
||||
closeFile();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RifEnsambleParametersReader::parse()
|
||||
{
|
||||
bool errors = false;
|
||||
QTextStream* dataStream = openDataStream();
|
||||
int lineNo = 0;
|
||||
|
||||
try
|
||||
{
|
||||
while (!dataStream->atEnd() && !errors)
|
||||
{
|
||||
QString line = dataStream->readLine();
|
||||
|
||||
lineNo++;
|
||||
QStringList cols = RifFileParseTools::splitLineAndTrim(line, " ");
|
||||
|
||||
if (cols.size() != 2)
|
||||
{
|
||||
throw FileParseException(QString("RifEnsambleParametersReader: Invalid file format in line %1").arg(lineNo));
|
||||
}
|
||||
|
||||
QString& name = cols[0];
|
||||
QString& strValue = cols[1];
|
||||
|
||||
if (!RiaStdStringTools::isNumber(strValue.toStdString(), QLocale::c().decimalPoint().toAscii()))
|
||||
{
|
||||
throw FileParseException(QString("RifEnsambleParametersReader: Invalid number format in line %1").arg(lineNo));
|
||||
}
|
||||
|
||||
bool parseOk = true;
|
||||
double value = QLocale::c().toDouble(strValue, &parseOk);
|
||||
if (!parseOk)
|
||||
{
|
||||
throw FileParseException(QString("RifEnsambleParametersReader: Invalid number format in line %1").arg(lineNo));
|
||||
}
|
||||
|
||||
m_parameters.addParameter(name, value);
|
||||
}
|
||||
|
||||
closeDataStream();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
closeDataStream();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QTextStream* RifEnsambleParametersReader::openDataStream()
|
||||
{
|
||||
if (!openFile()) return nullptr;
|
||||
|
||||
m_textStream = new QTextStream(m_file);
|
||||
return m_textStream;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RifEnsambleParametersReader::closeDataStream()
|
||||
{
|
||||
if (m_textStream)
|
||||
{
|
||||
delete m_textStream;
|
||||
m_textStream = nullptr;
|
||||
}
|
||||
closeFile();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RifEnsambleParametersReader::openFile()
|
||||
{
|
||||
if (!m_file)
|
||||
{
|
||||
m_file = new QFile(m_fileName);
|
||||
if (!m_file->open(QIODevice::ReadOnly | QIODevice::Text))
|
||||
{
|
||||
RiaLogging::error(QString("Failed to open %1").arg(m_fileName));
|
||||
|
||||
delete m_file;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RifEnsambleParametersReader::closeFile()
|
||||
{
|
||||
if (m_file)
|
||||
{
|
||||
m_file->close();
|
||||
delete m_file;
|
||||
m_file = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
const RifEnsambleParameters& RifEnsambleParametersReader::parameters() const
|
||||
{
|
||||
return m_parameters;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RifEnsambleParameters::addParameter(const QString& name, double value)
|
||||
{
|
||||
m_parameters[name] = value;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
std::map<QString, double> RifEnsambleParameters::parameters() const
|
||||
{
|
||||
return m_parameters;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RifEnsambleParametersFileLocator::locate(const QString& modelPath)
|
||||
{
|
||||
int MAX_LEVELS_UP = 2;
|
||||
|
||||
int dirLevel = 0;
|
||||
QDir qdir(modelPath);
|
||||
|
||||
do
|
||||
{
|
||||
QStringList files = qdir.entryList(QDir::Files | QDir::NoDotAndDotDot);
|
||||
for (const QString& file : files)
|
||||
{
|
||||
if (QString::compare(file, PARAMETERS_FILE_NAME, Qt::CaseInsensitive) == 0)
|
||||
{
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
} while (dirLevel++ == MAX_LEVELS_UP);
|
||||
|
||||
return "";
|
||||
}
|
||||
84
ApplicationCode/FileInterface/RifEnsambleParametersReader.h
Normal file
84
ApplicationCode/FileInterface/RifEnsambleParametersReader.h
Normal file
@@ -0,0 +1,84 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 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 "RiaPreferences.h"
|
||||
|
||||
#include "RifSummaryCaseRestartSelector.h"
|
||||
|
||||
#include <QString>
|
||||
#include <QTextStream>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
class QStringList;
|
||||
class QTextStream;
|
||||
class QFile;
|
||||
|
||||
//==================================================================================================
|
||||
//
|
||||
//
|
||||
//==================================================================================================
|
||||
class RifEnsambleParameters
|
||||
{
|
||||
public:
|
||||
void addParameter(const QString& name, double value);
|
||||
std::map<QString, double> parameters() const;
|
||||
|
||||
private:
|
||||
std::map<QString, double> m_parameters;
|
||||
};
|
||||
|
||||
//==================================================================================================
|
||||
//
|
||||
//
|
||||
//==================================================================================================
|
||||
class RifEnsambleParametersReader
|
||||
{
|
||||
public:
|
||||
RifEnsambleParametersReader(const QString& fileName);
|
||||
~RifEnsambleParametersReader();
|
||||
|
||||
void parse();
|
||||
const RifEnsambleParameters& parameters() const;
|
||||
|
||||
private:
|
||||
QTextStream* openDataStream();
|
||||
void closeDataStream();
|
||||
bool openFile();
|
||||
void closeFile();
|
||||
private:
|
||||
RifEnsambleParameters m_parameters;
|
||||
|
||||
QString m_fileName;
|
||||
QFile* m_file;
|
||||
QTextStream* m_textStream;
|
||||
};
|
||||
|
||||
//==================================================================================================
|
||||
//
|
||||
//
|
||||
//==================================================================================================
|
||||
class RifEnsambleParametersFileLocator
|
||||
{
|
||||
public:
|
||||
static QString locate(const QString& modelPath);
|
||||
};
|
||||
33
ApplicationCode/FileInterface/RifFileParseTools.cpp
Normal file
33
ApplicationCode/FileInterface/RifFileParseTools.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 "RifFileParseTools.h"
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QStringList RifFileParseTools::splitLineAndTrim(const QString& line, const QString& separator)
|
||||
{
|
||||
QStringList cols = line.split(separator);
|
||||
for (QString& col : cols)
|
||||
{
|
||||
col = col.trimmed();
|
||||
}
|
||||
return cols;
|
||||
}
|
||||
42
ApplicationCode/FileInterface/RifFileParseTools.h
Normal file
42
ApplicationCode/FileInterface/RifFileParseTools.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 <QString>
|
||||
#include <QStringList>
|
||||
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
//==================================================================================================
|
||||
class RifFileParseTools
|
||||
{
|
||||
public:
|
||||
static QStringList splitLineAndTrim(const QString& line, const QString& separator);
|
||||
};
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
//==================================================================================================
|
||||
class FileParseException
|
||||
{
|
||||
public:
|
||||
FileParseException(const QString &message) : message(message) {}
|
||||
QString message;
|
||||
};
|
||||
Reference in New Issue
Block a user