2018-03-26 06:11:36 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// 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 "RifCaseRealizationParametersReader.h"
|
|
|
|
#include "RifFileParseTools.h"
|
|
|
|
|
|
|
|
#include "RiaLogging.h"
|
|
|
|
#include "RiaStdStringTools.h"
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
/// Constants
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
#define PARAMETERS_FILE_NAME "parameters.txt"
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RifCaseRealizationParametersReader::RifCaseRealizationParametersReader(const QString& fileName)
|
|
|
|
{
|
2018-05-03 02:53:25 -05:00
|
|
|
m_parameters = std::shared_ptr<RigCaseRealizationParameters>(new RigCaseRealizationParameters());
|
2018-03-26 06:11:36 -05:00
|
|
|
m_fileName = fileName;
|
|
|
|
m_file = nullptr;
|
|
|
|
m_textStream = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-04-06 08:52:16 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RifCaseRealizationParametersReader::RifCaseRealizationParametersReader()
|
|
|
|
{
|
2018-05-03 02:53:25 -05:00
|
|
|
m_parameters = std::shared_ptr<RigCaseRealizationParameters>(new RigCaseRealizationParameters());
|
2018-04-06 08:52:16 -05:00
|
|
|
m_fileName = "";
|
|
|
|
m_file = nullptr;
|
|
|
|
m_textStream = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-26 06:11:36 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RifCaseRealizationParametersReader::~RifCaseRealizationParametersReader()
|
|
|
|
{
|
|
|
|
if (m_textStream)
|
|
|
|
{
|
|
|
|
delete m_textStream;
|
|
|
|
}
|
|
|
|
closeFile();
|
|
|
|
}
|
|
|
|
|
2018-04-06 08:52:16 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RifCaseRealizationParametersReader::setFileName(const QString& fileName)
|
|
|
|
{
|
|
|
|
m_fileName = fileName;
|
|
|
|
}
|
|
|
|
|
2018-03-26 06:11:36 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RifCaseRealizationParametersReader::parse()
|
|
|
|
{
|
|
|
|
int lineNo = 0;
|
|
|
|
QTextStream* dataStream = openDataStream();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while (!dataStream->atEnd())
|
|
|
|
{
|
|
|
|
QString line = dataStream->readLine();
|
|
|
|
|
|
|
|
lineNo++;
|
|
|
|
QStringList cols = RifFileParseTools::splitLineAndTrim(line, " ");
|
|
|
|
|
|
|
|
if (cols.size() != 2)
|
|
|
|
{
|
2018-04-19 07:00:00 -05:00
|
|
|
throw FileParseException(QString("RifEnsembleParametersReader: Invalid file format in line %1").arg(lineNo));
|
2018-03-26 06:11:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
QString& name = cols[0];
|
|
|
|
QString& strValue = cols[1];
|
|
|
|
|
2018-05-03 02:53:25 -05:00
|
|
|
if (RiaStdStringTools::startsWithAlphabetic(strValue.toStdString()))
|
2018-03-26 06:11:36 -05:00
|
|
|
{
|
2018-05-03 02:53:25 -05:00
|
|
|
m_parameters->addParameter(name, strValue);
|
2018-03-26 06:11:36 -05:00
|
|
|
}
|
2018-05-03 02:53:25 -05:00
|
|
|
else
|
2018-03-26 06:11:36 -05:00
|
|
|
{
|
2018-05-03 02:53:25 -05:00
|
|
|
if (!RiaStdStringTools::isNumber(strValue.toStdString(), QLocale::c().decimalPoint().toAscii()))
|
|
|
|
{
|
|
|
|
throw FileParseException(QString("RifEnsembleParametersReader: Invalid number format in line %1").arg(lineNo));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parseOk = true;
|
|
|
|
double value = QLocale::c().toDouble(strValue, &parseOk);
|
|
|
|
if (!parseOk)
|
|
|
|
{
|
|
|
|
throw FileParseException(QString("RifEnsembleParametersReader: Invalid number format in line %1").arg(lineNo));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_parameters->addParameter(name, value);
|
2018-03-26 06:11:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closeDataStream();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
closeDataStream();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QTextStream* RifCaseRealizationParametersReader::openDataStream()
|
|
|
|
{
|
|
|
|
openFile();
|
|
|
|
|
|
|
|
m_textStream = new QTextStream(m_file);
|
|
|
|
return m_textStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RifCaseRealizationParametersReader::closeDataStream()
|
|
|
|
{
|
|
|
|
if (m_textStream)
|
|
|
|
{
|
|
|
|
delete m_textStream;
|
|
|
|
m_textStream = nullptr;
|
|
|
|
}
|
|
|
|
closeFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RifCaseRealizationParametersReader::openFile()
|
|
|
|
{
|
|
|
|
if (!m_file)
|
|
|
|
{
|
|
|
|
m_file = new QFile(m_fileName);
|
|
|
|
if (!m_file->open(QIODevice::ReadOnly | QIODevice::Text))
|
|
|
|
{
|
|
|
|
closeFile();
|
|
|
|
//delete m_file;
|
|
|
|
//m_file = nullptr;
|
|
|
|
throw FileParseException(QString("Failed to open %1").arg(m_fileName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RifCaseRealizationParametersReader::closeFile()
|
|
|
|
{
|
|
|
|
if (m_file)
|
|
|
|
{
|
|
|
|
m_file->close();
|
|
|
|
delete m_file;
|
|
|
|
m_file = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2018-05-03 02:53:25 -05:00
|
|
|
const std::shared_ptr<RigCaseRealizationParameters> RifCaseRealizationParametersReader::parameters() const
|
2018-03-26 06:11:36 -05:00
|
|
|
{
|
|
|
|
return m_parameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QString RifCaseRealizationParametersFileLocator::locate(const QString& modelPath)
|
|
|
|
{
|
|
|
|
int MAX_LEVELS_UP = 2;
|
|
|
|
int dirLevel = 0;
|
|
|
|
|
|
|
|
QDir qdir(modelPath);
|
|
|
|
|
|
|
|
const QFileInfo dir(modelPath);
|
|
|
|
if (dir.isFile()) qdir.cdUp();
|
|
|
|
else if (!dir.isDir()) return "";
|
|
|
|
|
|
|
|
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 qdir.absoluteFilePath(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qdir.cdUp();
|
|
|
|
|
|
|
|
} while (dirLevel++ < MAX_LEVELS_UP);
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|