2018-03-20 05:25:54 -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 "RifSummaryCaseRestartSelector.h"
|
|
|
|
|
|
|
|
#include "RiaApplication.h"
|
|
|
|
#include "RiaPreferences.h"
|
2018-04-09 03:53:51 -05:00
|
|
|
#include "RiaFilePathTools.h"
|
2018-04-17 04:01:24 -05:00
|
|
|
#include "RiaLogging.h"
|
2018-03-20 05:25:54 -05:00
|
|
|
|
|
|
|
#include "RicSummaryCaseRestartDialog.h"
|
|
|
|
|
2018-04-13 04:11:05 -05:00
|
|
|
#include "RifEclipseSummaryTools.h"
|
|
|
|
|
2018-03-20 05:25:54 -05:00
|
|
|
#include <string>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
|
2018-03-20 08:32:24 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
/// Internal function
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
|
|
bool vectorContains(const std::vector<T>& vector, T item)
|
|
|
|
{
|
|
|
|
for (const auto& i : vector)
|
|
|
|
{
|
|
|
|
if (i == item) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-09 06:21:48 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
/// INternal function
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2018-04-13 08:44:28 -05:00
|
|
|
RicSummaryCaseRestartDialog::ImportOptions mapReadOption(RiaPreferences::SummaryRestartFilesImportMode mode)
|
2018-04-09 06:21:48 -05:00
|
|
|
{
|
|
|
|
return
|
2018-04-13 08:44:28 -05:00
|
|
|
mode == RiaPreferences::SummaryRestartFilesImportMode::NOT_IMPORT ? RicSummaryCaseRestartDialog::ImportOptions::NOT_IMPORT :
|
|
|
|
mode == RiaPreferences::SummaryRestartFilesImportMode::SEPARATE_CASES ? RicSummaryCaseRestartDialog::ImportOptions::SEPARATE_CASES :
|
|
|
|
RicSummaryCaseRestartDialog::ImportOptions::IMPORT_ALL;
|
2018-04-09 06:21:48 -05:00
|
|
|
}
|
|
|
|
|
2018-03-20 05:25:54 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RifSummaryCaseRestartSelector::RifSummaryCaseRestartSelector()
|
|
|
|
{
|
2018-04-13 04:11:05 -05:00
|
|
|
RiaPreferences* prefs = RiaApplication::instance()->preferences();
|
|
|
|
m_showDialog = prefs->summaryRestartFilesShowImportDialog();
|
2018-04-13 08:44:28 -05:00
|
|
|
m_defaultSummaryImportMode = mapReadOption(prefs->summaryImportMode());
|
|
|
|
m_defaultGridImportMode = mapReadOption(prefs->gridImportMode());
|
2018-04-13 04:11:05 -05:00
|
|
|
|
2018-04-20 01:27:42 -05:00
|
|
|
//m_buildGridFileList = false;
|
2018-04-13 08:44:28 -05:00
|
|
|
m_gridFiles.clear();
|
2018-03-20 05:25:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RifSummaryCaseRestartSelector::~RifSummaryCaseRestartSelector()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2018-04-20 01:27:42 -05:00
|
|
|
bool RifSummaryCaseRestartSelector::getFilesToImportFromSummaryFiles(const QStringList& initialSummaryFiles)
|
2018-03-20 05:25:54 -05:00
|
|
|
{
|
2018-04-20 01:27:42 -05:00
|
|
|
std::vector<std::pair<QString, QString>> files;
|
|
|
|
for (QString f : initialSummaryFiles)
|
|
|
|
{
|
|
|
|
files.push_back(std::make_pair(f, ""));
|
|
|
|
}
|
|
|
|
return getFilesToImport(files);
|
2018-04-13 04:11:05 -05:00
|
|
|
}
|
2018-03-20 05:25:54 -05:00
|
|
|
|
2018-04-13 04:11:05 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2018-04-20 01:27:42 -05:00
|
|
|
bool RifSummaryCaseRestartSelector::getFilesToImportFromGridFiles(const QStringList& initialGridFiles)
|
2018-04-13 04:11:05 -05:00
|
|
|
{
|
2018-04-20 01:27:42 -05:00
|
|
|
std::vector<std::pair<QString, QString>> files;
|
|
|
|
for (QString f : initialGridFiles)
|
|
|
|
{
|
|
|
|
files.push_back(std::make_pair(getSummaryFileFromGridFile(f), f));
|
|
|
|
}
|
|
|
|
return getFilesToImport(files);
|
2018-04-13 04:11:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2018-04-20 01:27:42 -05:00
|
|
|
bool RifSummaryCaseRestartSelector::getFilesToImport(const std::vector<std::pair<QString /*sum*/, QString /*grid*/>>& initialFiles)
|
2018-04-13 04:11:05 -05:00
|
|
|
{
|
2018-03-20 05:25:54 -05:00
|
|
|
std::vector<RifSummaryCaseFileInfo> fileInfos;
|
2018-04-13 04:11:05 -05:00
|
|
|
if (m_showDialog)
|
2018-03-20 05:25:54 -05:00
|
|
|
{
|
2018-04-20 01:27:42 -05:00
|
|
|
bool enableApplyToAllField = initialFiles.size() > 1;
|
|
|
|
return getFilesToImportByAskingUser(initialFiles, enableApplyToAllField);
|
2018-03-20 05:25:54 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-20 01:27:42 -05:00
|
|
|
return getFilesToImportUsingPrefs(initialFiles);
|
2018-03-20 05:25:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2018-04-20 01:27:42 -05:00
|
|
|
bool RifSummaryCaseRestartSelector::getFilesToImportByAskingUser(const std::vector<std::pair<QString /*sum*/, QString /*grid*/>>& initialFiles,
|
2018-04-13 04:11:05 -05:00
|
|
|
bool enableApplyToAllField)
|
2018-03-20 05:25:54 -05:00
|
|
|
{
|
|
|
|
RicSummaryCaseRestartDialogResult lastResult;
|
|
|
|
|
2018-04-20 01:27:42 -05:00
|
|
|
for (const std::pair<QString, QString>& initialFile : initialFiles)
|
2018-03-20 05:25:54 -05:00
|
|
|
{
|
2018-04-20 01:27:42 -05:00
|
|
|
RicSummaryCaseRestartDialogResult result = RicSummaryCaseRestartDialog::openDialog(initialFile,
|
2018-04-13 08:44:28 -05:00
|
|
|
enableApplyToAllField,
|
|
|
|
m_defaultSummaryImportMode,
|
|
|
|
m_defaultGridImportMode,
|
|
|
|
&lastResult);
|
2018-03-20 05:25:54 -05:00
|
|
|
if (result.ok)
|
|
|
|
{
|
2018-04-13 04:11:05 -05:00
|
|
|
for (const QString& file : result.summaryFiles)
|
2018-03-20 05:25:54 -05:00
|
|
|
{
|
2018-04-13 08:44:28 -05:00
|
|
|
RifSummaryCaseFileInfo fi(file, result.summaryImportOption == RicSummaryCaseRestartDialog::IMPORT_ALL);
|
2018-04-20 01:27:42 -05:00
|
|
|
if (!vectorContains(m_summaryFileInfos, fi))
|
2018-03-20 05:25:54 -05:00
|
|
|
{
|
2018-04-20 01:27:42 -05:00
|
|
|
m_summaryFileInfos.push_back(fi);
|
2018-03-20 05:25:54 -05:00
|
|
|
}
|
|
|
|
}
|
2018-03-21 08:47:07 -05:00
|
|
|
lastResult = result;
|
2018-04-13 04:11:05 -05:00
|
|
|
|
2018-04-13 08:44:28 -05:00
|
|
|
for (const QString& gridFile : result.gridFiles)
|
2018-04-13 04:11:05 -05:00
|
|
|
{
|
2018-04-13 08:44:28 -05:00
|
|
|
m_gridFiles.push_back(gridFile);
|
2018-04-13 04:11:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Cancel pressed, cancel everything
|
2018-04-20 01:27:42 -05:00
|
|
|
m_summaryFileInfos.clear();
|
2018-04-13 08:44:28 -05:00
|
|
|
m_gridFiles.clear();
|
2018-04-20 01:27:42 -05:00
|
|
|
return false;
|
2018-03-20 05:25:54 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-20 01:27:42 -05:00
|
|
|
return true;
|
2018-03-20 05:25:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2018-04-20 01:27:42 -05:00
|
|
|
bool RifSummaryCaseRestartSelector::getFilesToImportUsingPrefs(const std::vector<std::pair<QString /*sum*/, QString /*grid*/>>& initialFiles)
|
2018-03-20 05:25:54 -05:00
|
|
|
{
|
2018-04-09 03:53:51 -05:00
|
|
|
std::vector<RifSummaryCaseFileInfo> filesToImport;
|
2018-03-20 05:25:54 -05:00
|
|
|
RicSummaryCaseRestartDialogResult lastResult;
|
|
|
|
|
2018-04-13 08:44:28 -05:00
|
|
|
m_gridFiles.clear();
|
2018-04-13 04:11:05 -05:00
|
|
|
|
2018-04-20 01:27:42 -05:00
|
|
|
for (const std::pair<QString, QString>& initialFile : initialFiles)
|
2018-03-20 05:25:54 -05:00
|
|
|
{
|
2018-04-20 01:27:42 -05:00
|
|
|
QString initialSummaryFile = RiaFilePathTools::toInternalSeparator(initialFile.first);
|
|
|
|
QString initialGridFile = RiaFilePathTools::toInternalSeparator(initialFile.second);
|
|
|
|
bool handleGridFile = !initialGridFile.isEmpty();
|
2018-04-09 03:53:51 -05:00
|
|
|
|
2018-04-13 08:44:28 -05:00
|
|
|
if (m_defaultSummaryImportMode == RicSummaryCaseRestartDialog::IMPORT_ALL)
|
2018-03-20 05:25:54 -05:00
|
|
|
{
|
2018-04-20 01:27:42 -05:00
|
|
|
filesToImport.push_back(RifSummaryCaseFileInfo(initialSummaryFile, true));
|
2018-03-20 05:25:54 -05:00
|
|
|
}
|
2018-04-13 08:44:28 -05:00
|
|
|
else if (m_defaultSummaryImportMode == RicSummaryCaseRestartDialog::NOT_IMPORT)
|
2018-03-20 05:25:54 -05:00
|
|
|
{
|
2018-04-20 01:27:42 -05:00
|
|
|
filesToImport.push_back(RifSummaryCaseFileInfo(initialSummaryFile, false));
|
2018-03-20 05:25:54 -05:00
|
|
|
}
|
2018-04-13 08:44:28 -05:00
|
|
|
else if (m_defaultSummaryImportMode == RicSummaryCaseRestartDialog::SEPARATE_CASES)
|
2018-03-20 05:25:54 -05:00
|
|
|
{
|
2018-04-20 01:27:42 -05:00
|
|
|
filesToImport.push_back(RifSummaryCaseFileInfo(initialSummaryFile, false));
|
2018-03-20 05:25:54 -05:00
|
|
|
|
|
|
|
RifReaderEclipseSummary reader;
|
2018-04-17 04:01:24 -05:00
|
|
|
bool hasWarnings = false;
|
2018-04-20 01:27:42 -05:00
|
|
|
std::vector<RifRestartFileInfo> restartFileInfos = reader.getRestartFiles(initialSummaryFile, &hasWarnings);
|
2018-04-09 03:53:51 -05:00
|
|
|
for (const auto& rfi : restartFileInfos)
|
|
|
|
{
|
|
|
|
RifSummaryCaseFileInfo fi(rfi.fileName, false);
|
|
|
|
if (!vectorContains(filesToImport, fi))
|
|
|
|
{
|
|
|
|
filesToImport.push_back(fi);
|
2018-04-13 08:44:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 01:27:42 -05:00
|
|
|
if (handleGridFile)
|
2018-04-13 08:44:28 -05:00
|
|
|
{
|
2018-04-20 01:27:42 -05:00
|
|
|
m_gridFiles.push_back(initialGridFile);
|
2018-04-13 04:11:05 -05:00
|
|
|
|
2018-04-13 08:44:28 -05:00
|
|
|
if (m_defaultGridImportMode == RicSummaryCaseRestartDialog::SEPARATE_CASES)
|
|
|
|
{
|
|
|
|
RifReaderEclipseSummary reader;
|
2018-04-17 04:01:24 -05:00
|
|
|
bool hasWarnings = false;
|
2018-04-20 01:27:42 -05:00
|
|
|
std::vector<RifRestartFileInfo> restartFileInfos = reader.getRestartFiles(initialSummaryFile, &hasWarnings);
|
2018-04-13 08:44:28 -05:00
|
|
|
for (const auto& rfi : restartFileInfos)
|
|
|
|
{
|
|
|
|
RifSummaryCaseFileInfo fi(RifEclipseSummaryTools::findGridCaseFileFromSummaryHeaderFile(rfi.fileName), false);
|
|
|
|
if (!m_gridFiles.contains(fi.fileName) && QFileInfo(fi.fileName).exists())
|
2018-04-13 04:11:05 -05:00
|
|
|
{
|
2018-04-13 08:44:28 -05:00
|
|
|
m_gridFiles.push_back(fi.fileName);
|
2018-04-13 04:11:05 -05:00
|
|
|
}
|
2018-04-09 03:53:51 -05:00
|
|
|
}
|
2018-04-17 04:01:24 -05:00
|
|
|
|
|
|
|
if (hasWarnings)
|
|
|
|
{
|
|
|
|
for (const QString& warning : reader.warnings()) RiaLogging::error(warning);
|
|
|
|
}
|
2018-04-09 03:53:51 -05:00
|
|
|
}
|
2018-03-20 05:25:54 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-20 01:27:42 -05:00
|
|
|
return true;
|
2018-03-20 05:25:54 -05:00
|
|
|
}
|
2018-04-13 04:11:05 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QStringList RifSummaryCaseRestartSelector::getSummaryFilesFromGridFiles(const QStringList& gridFiles)
|
|
|
|
{
|
|
|
|
QStringList summaryFiles;
|
|
|
|
|
|
|
|
// Find summary header file names from eclipse case file names
|
|
|
|
for (const auto& gridFile : gridFiles)
|
|
|
|
{
|
2018-04-20 01:27:42 -05:00
|
|
|
summaryFiles.push_back(getSummaryFileFromGridFile(gridFile));
|
|
|
|
}
|
|
|
|
return summaryFiles;
|
|
|
|
}
|
2018-04-13 04:11:05 -05:00
|
|
|
|
2018-04-20 01:27:42 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
QString RifSummaryCaseRestartSelector::getSummaryFileFromGridFile(const QString& gridFile)
|
|
|
|
{
|
|
|
|
// Find summary header file names from eclipse case file names
|
|
|
|
if (!gridFile.isEmpty())
|
|
|
|
{
|
|
|
|
QString summaryHeaderFile;
|
|
|
|
bool formatted;
|
2018-04-13 04:11:05 -05:00
|
|
|
|
2018-04-20 01:27:42 -05:00
|
|
|
RifEclipseSummaryTools::findSummaryHeaderFile(gridFile, &summaryHeaderFile, &formatted);
|
|
|
|
|
|
|
|
if (!summaryHeaderFile.isEmpty())
|
|
|
|
{
|
|
|
|
return summaryHeaderFile;
|
2018-04-13 04:11:05 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-20 01:27:42 -05:00
|
|
|
return "";
|
2018-04-13 04:11:05 -05:00
|
|
|
}
|