#3478 Export Cell Result to File : Move code to RicEclipseCellResultToFileImpl

This commit is contained in:
Magne Sjaastad
2018-10-10 15:12:54 +02:00
parent 911de7c973
commit 4290d8d836
9 changed files with 228 additions and 149 deletions

View File

@@ -21,6 +21,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RicExportVisibleWellPathsFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicExportWellPathsUi.h
${CMAKE_CURRENT_LIST_DIR}/RicExportLgrFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicExportLgrUi.h
${CMAKE_CURRENT_LIST_DIR}/RicEclipseCellResultToFileImpl.h
)
set (SOURCE_GROUP_SOURCE_FILES
@@ -46,6 +47,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RicExportVisibleWellPathsFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicExportWellPathsUi.cpp
${CMAKE_CURRENT_LIST_DIR}/RicExportLgrFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicExportLgrUi.cpp
${CMAKE_CURRENT_LIST_DIR}/RicEclipseCellResultToFileImpl.cpp
)
list(APPEND CODE_HEADER_FILES

View File

@@ -0,0 +1,157 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2018- Equinor 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 "RicEclipseCellResultToFileImpl.h"
#include "RiaLogging.h"
#include "RigCaseCellResultsData.h"
#include "RigEclipseCaseData.h"
#include "RigMainGrid.h"
#include "RigResultAccessor.h"
#include "RigResultAccessorFactory.h"
#include "RimEclipseResultDefinition.h"
#include "cafProgressInfo.h"
#include <QFile>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicEclipseCellResultToFileImpl::writePropertyToTextFile(const QString& fileName,
RigEclipseCaseData* eclipseCase,
size_t timeStep,
const QString& resultName,
const QString& eclipseKeyword)
{
CVF_TIGHT_ASSERT(eclipseCase);
if (!eclipseCase) return false;
cvf::ref<RigResultAccessor> resultAccessor =
RigResultAccessorFactory::createFromUiResultName(eclipseCase, 0, RiaDefines::MATRIX_MODEL, timeStep, resultName);
if (resultAccessor.isNull())
{
return false;
}
const double undefinedValue = 0.0;
return writeResultToTextFile(
fileName, eclipseCase, resultAccessor.p(), eclipseKeyword, undefinedValue, "writePropertyToTextFile");
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicEclipseCellResultToFileImpl::writeBinaryResultToTextFile(const QString& fileName,
RigEclipseCaseData* eclipseCase,
size_t timeStep,
RimEclipseResultDefinition* resultDefinition,
const QString& eclipseKeyword,
const double undefinedValue,
const QString& logPrefix)
{
CVF_TIGHT_ASSERT(eclipseCase);
cvf::ref<RigResultAccessor> resultAccessor =
RigResultAccessorFactory::createFromResultDefinition(eclipseCase, 0, timeStep, resultDefinition);
if (resultAccessor.isNull())
{
return false;
}
return writeResultToTextFile(fileName, eclipseCase, resultAccessor.p(), eclipseKeyword, undefinedValue, logPrefix);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicEclipseCellResultToFileImpl::writeResultToTextFile(const QString& fileName,
RigEclipseCaseData* eclipseCase,
RigResultAccessor* resultAccessor,
const QString& eclipseKeyword,
const double undefinedValue,
const QString& logPrefix)
{
if (!resultAccessor)
{
RiaLogging::error(logPrefix + QString(" : : Could not access result data for '%1'").arg(fileName));
return false;
}
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
RiaLogging::error(logPrefix + QString(" : Could not open file '%1'. Do the folder exist?").arg(fileName));
return false;
}
std::vector<double> resultData;
for (size_t i = 0; i < eclipseCase->mainGrid()->cellCount(); i++)
{
double resultValue = resultAccessor->cellScalar(i);
if (resultValue == HUGE_VAL)
{
resultValue = undefinedValue;
}
resultData.push_back(resultValue);
}
writeDataToTextFile(&file, eclipseKeyword, resultData);
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicEclipseCellResultToFileImpl::writeDataToTextFile(QFile* file,
const QString& eclipseKeyword,
const std::vector<double>& resultData)
{
QTextStream textstream(file);
textstream << "\n";
textstream << "-- Exported from ResInsight"
<< "\n";
textstream << eclipseKeyword << "\n" << right << qSetFieldWidth(16);
caf::ProgressInfo pi(resultData.size(), QString("Writing data to file %1").arg(file->fileName()));
size_t progressSteps = resultData.size() / 20;
size_t i;
for (i = 0; i < resultData.size(); i++)
{
textstream << resultData[i];
if ((i + 1) % 5 == 0)
{
textstream << "\n";
}
if (i % progressSteps == 0)
{
pi.setProgress(i);
}
}
textstream << "\n"
<< "/"
<< "\n";
}

View File

@@ -0,0 +1,59 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2018- Equinor 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 <vector>
class QFile;
class RigEclipseCaseData;
class RimEclipseResultDefinition;
class RigResultAccessor;
//==================================================================================================
///
//==================================================================================================
class RicEclipseCellResultToFileImpl
{
public:
static bool writePropertyToTextFile(const QString& fileName,
RigEclipseCaseData* eclipseCase,
size_t timeStep,
const QString& resultName,
const QString& eclipseKeyword);
static bool writeBinaryResultToTextFile(const QString& fileName,
RigEclipseCaseData* eclipseCase,
size_t timeStep,
RimEclipseResultDefinition* resultDefinition,
const QString& eclipseKeyword,
const double undefinedValue,
const QString& logPrefix);
static bool writeResultToTextFile(const QString& fileName,
RigEclipseCaseData* eclipseCase,
RigResultAccessor* resultAccessor,
const QString& eclipseKeyword,
const double undefinedValue,
const QString& logPrefix);
static void writeDataToTextFile(QFile* file, const QString& eclipseKeyword, const std::vector<double>& resultData);
};

View File

@@ -22,8 +22,7 @@
#include "RiaApplication.h"
#include "RicExportFeatureImpl.h"
#include "RifEclipseInputFileTools.h"
#include "RicEclipseCellResultToFileImpl.h"
#include "RimEclipseInputCase.h"
#include "RimEclipseInputProperty.h"
@@ -106,7 +105,7 @@ void RicSaveEclipseInputPropertyFeature::onActionTriggered(bool isChecked)
if (propertyDialog.exec() == QDialog::Accepted)
{
bool isOk = RifEclipseInputFileTools::writePropertyToTextFile(exportSettings.fileName, inputReservoir->eclipseCaseData(), 0, inputProperty->resultName, exportSettings.eclipseKeyword);
bool isOk = RicEclipseCellResultToFileImpl::writePropertyToTextFile(exportSettings.fileName, inputReservoir->eclipseCaseData(), 0, inputProperty->resultName, exportSettings.eclipseKeyword);
if (isOk)
{
inputProperty->fileName = exportSettings.fileName;

View File

@@ -22,11 +22,9 @@
#include "RiaLogging.h"
#include "RicExportFeatureImpl.h"
#include "RicEclipseCellResultToFileImpl.h"
#include "RicSaveEclipseInputVisibleCellsUi.h"
#include "RifEclipseInputFileTools.h"
#include "RimEclipseCase.h"
#include "RimEclipseCellColors.h"
#include "RimEclipseView.h"
@@ -99,7 +97,8 @@ void RicSaveEclipseInputVisibleCellsFeature::executeCommand(RimEclipseView* view
RiaLogging::error(QString("%1: Unable to open file '%2' for writing.").arg(logPrefix).arg(exportSettings.exportFilename));
return;
}
RifEclipseInputFileTools::writeDataToTextFile(&exportFile, exportSettings.exportKeyword().text(), values);
RicEclipseCellResultToFileImpl::writeDataToTextFile(&exportFile, exportSettings.exportKeyword().text(), values);
}
//--------------------------------------------------------------------------------------------------

View File

@@ -23,9 +23,7 @@
#include "RiaLogging.h"
#include "RicExportFeatureImpl.h"
#include "RifEclipseInputFileTools.h"
#include "RifReaderInterface.h"
#include "RicEclipseCellResultToFileImpl.h"
#include "RigCaseCellResultsData.h"
@@ -99,7 +97,7 @@ void RicSaveEclipseResultAsInputPropertyExec::redo()
{
size_t timeStep = m_cellColors->reservoirView()->currentTimeStep();
bool isOk = RifEclipseInputFileTools::writeBinaryResultToTextFile(exportSettings.fileName, m_cellColors->reservoirView()->eclipseCase()->eclipseCaseData(), timeStep, m_cellColors, exportSettings.eclipseKeyword, exportSettings.undefinedValue, "saveEclipseResultAsInputPropertyExec");
bool isOk = RicEclipseCellResultToFileImpl::writeBinaryResultToTextFile(exportSettings.fileName, m_cellColors->reservoirView()->eclipseCase()->eclipseCaseData(), timeStep, m_cellColors, exportSettings.eclipseKeyword, exportSettings.undefinedValue, "saveEclipseResultAsInputPropertyExec");
if (!isOk)
{
RiaLogging::error("Failed to exported current result to " + exportSettings.fileName);