diff --git a/ApplicationCode/CommandFileInterface/RicfExportProperty.cpp b/ApplicationCode/CommandFileInterface/RicfExportProperty.cpp index 694f5ef124..1b81580649 100644 --- a/ApplicationCode/CommandFileInterface/RicfExportProperty.cpp +++ b/ApplicationCode/CommandFileInterface/RicfExportProperty.cpp @@ -20,6 +20,8 @@ #include "RicfCommandFileExecutor.h" +#include "../Commands/ExportCommands/RicEclipseCellResultToFileImpl.h" + #include "RiaApplication.h" #include "RiaLogging.h" @@ -36,8 +38,6 @@ #include "RimEclipseView.h" #include "RimEclipseCellColors.h" -#include "RifEclipseInputFileTools.h" - #include "cafUtils.h" #include @@ -118,7 +118,7 @@ void RicfExportProperty::execute() auto resultAccessor = findResult(view, timeStepIndex, propertyType, propertyName); if (!resultAccessor.isNull()) { - RifEclipseInputFileTools::writeResultToTextFile(filePath, eclipseCase->eclipseCaseData(), resultAccessor, eclipseKeyword, m_undefinedValue, "exportProperty"); + RicEclipseCellResultToFileImpl::writeResultToTextFile(filePath, eclipseCase->eclipseCaseData(), resultAccessor.p(), eclipseKeyword, m_undefinedValue, "exportProperty"); } else { diff --git a/ApplicationCode/Commands/ExportCommands/CMakeLists_files.cmake b/ApplicationCode/Commands/ExportCommands/CMakeLists_files.cmake index 4644281548..6f4e1bfaac 100644 --- a/ApplicationCode/Commands/ExportCommands/CMakeLists_files.cmake +++ b/ApplicationCode/Commands/ExportCommands/CMakeLists_files.cmake @@ -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 diff --git a/ApplicationCode/Commands/ExportCommands/RicEclipseCellResultToFileImpl.cpp b/ApplicationCode/Commands/ExportCommands/RicEclipseCellResultToFileImpl.cpp new file mode 100644 index 0000000000..fe18d8145f --- /dev/null +++ b/ApplicationCode/Commands/ExportCommands/RicEclipseCellResultToFileImpl.cpp @@ -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 +// 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 + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +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 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 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 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& 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"; +} diff --git a/ApplicationCode/Commands/ExportCommands/RicEclipseCellResultToFileImpl.h b/ApplicationCode/Commands/ExportCommands/RicEclipseCellResultToFileImpl.h new file mode 100644 index 0000000000..905ead2751 --- /dev/null +++ b/ApplicationCode/Commands/ExportCommands/RicEclipseCellResultToFileImpl.h @@ -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 +// for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#pragma once + +#include + +#include + +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& resultData); +}; diff --git a/ApplicationCode/Commands/ExportCommands/RicSaveEclipseInputPropertyFeature.cpp b/ApplicationCode/Commands/ExportCommands/RicSaveEclipseInputPropertyFeature.cpp index 521fb5cee2..ed45866776 100644 --- a/ApplicationCode/Commands/ExportCommands/RicSaveEclipseInputPropertyFeature.cpp +++ b/ApplicationCode/Commands/ExportCommands/RicSaveEclipseInputPropertyFeature.cpp @@ -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; diff --git a/ApplicationCode/Commands/ExportCommands/RicSaveEclipseInputVisibleCellsFeature.cpp b/ApplicationCode/Commands/ExportCommands/RicSaveEclipseInputVisibleCellsFeature.cpp index ef3b36a4bd..43b1eefa7d 100644 --- a/ApplicationCode/Commands/ExportCommands/RicSaveEclipseInputVisibleCellsFeature.cpp +++ b/ApplicationCode/Commands/ExportCommands/RicSaveEclipseInputVisibleCellsFeature.cpp @@ -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); } //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationCode/Commands/ExportCommands/RicSaveEclipseResultAsInputPropertyExec.cpp b/ApplicationCode/Commands/ExportCommands/RicSaveEclipseResultAsInputPropertyExec.cpp index adc4ecca52..0093f2abfe 100644 --- a/ApplicationCode/Commands/ExportCommands/RicSaveEclipseResultAsInputPropertyExec.cpp +++ b/ApplicationCode/Commands/ExportCommands/RicSaveEclipseResultAsInputPropertyExec.cpp @@ -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); diff --git a/ApplicationCode/FileInterface/RifEclipseInputFileTools.cpp b/ApplicationCode/FileInterface/RifEclipseInputFileTools.cpp index d8f5b17190..ee7cbd90c1 100644 --- a/ApplicationCode/FileInterface/RifEclipseInputFileTools.cpp +++ b/ApplicationCode/FileInterface/RifEclipseInputFileTools.cpp @@ -28,7 +28,6 @@ #include "RigCaseCellResultsData.h" #include "RigEclipseCaseData.h" #include "RigMainGrid.h" -#include "RigResultAccessorFactory.h" #include "cafProgressInfo.h" @@ -471,133 +470,6 @@ const std::vector& RifEclipseInputFileTools::invalidPropertyDataKeyword return keywords; } -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -bool RifEclipseInputFileTools::writePropertyToTextFile(const QString& fileName, RigEclipseCaseData* eclipseCase, size_t timeStep, const QString& resultName, const QString& eclipseKeyWord) -{ - CVF_ASSERT(eclipseCase); - - size_t resultIndex = eclipseCase->results(RiaDefines::MATRIX_MODEL)->findScalarResultIndex(resultName); - if (resultIndex == cvf::UNDEFINED_SIZE_T) - { - return false; - } - - QFile file(fileName); - if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) - { - return false; - } - - std::vector< std::vector >& resultData = eclipseCase->results(RiaDefines::MATRIX_MODEL)->cellScalarResults(resultIndex); - if (resultData.size() == 0) - { - return false; - } - - std::vector& singleTimeStepData = resultData[timeStep]; - writeDataToTextFile(&file, eclipseKeyWord, singleTimeStepData); - - return true; -} - -//-------------------------------------------------------------------------------------------------- -/// Create and write a result vector with values for all cells. -/// undefinedValue is used for cells with no result -//-------------------------------------------------------------------------------------------------- -bool RifEclipseInputFileTools::writeBinaryResultToTextFile(const QString& fileName, - RigEclipseCaseData* eclipseCase, - size_t timeStep, - RimEclipseResultDefinition* resultDefinition, - const QString& eclipseKeyWord, - const double undefinedValue, - const QString& logPrefix) -{ - CVF_ASSERT(eclipseCase); - - cvf::ref resultAccessor = RigResultAccessorFactory::createFromResultDefinition(eclipseCase, eclipseCase->mainGrid()->gridIndex(), timeStep, resultDefinition); - if (resultAccessor.isNull()) - { - return false; - } - - return writeResultToTextFile(fileName, eclipseCase, resultAccessor, eclipseKeyWord, undefinedValue, logPrefix); -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -bool RifEclipseInputFileTools::writeResultToTextFile(const QString& fileName, - RigEclipseCaseData* eclipseCase, - cvf::ref resultAccessor, - const QString& eclipseKeyWord, - const double undefinedValue, - const QString& logPrefix) -{ - QFile file(fileName); - if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) - { - RiaLogging::error(QString("Could not open file '%1'. Do the folder exist?"). arg(fileName)); - return false; - } - - std::vector resultData; - size_t i, j, k; - for (k = 0; k < eclipseCase->mainGrid()->cellCountK(); k++) - { - for (j = 0; j < eclipseCase->mainGrid()->cellCountJ(); j++) - { - for (i = 0; i < eclipseCase->mainGrid()->cellCountI(); i++) - { - double resultValue = resultAccessor->cellScalar(eclipseCase->mainGrid()->cellIndexFromIJK(i, j, k)); - if (resultValue == HUGE_VAL) - { - resultValue = undefinedValue; - } - - resultData.push_back(resultValue); - } - } - } - - writeDataToTextFile(&file, eclipseKeyWord, resultData); - return true; -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void RifEclipseInputFileTools::writeDataToTextFile(QFile* file, const QString& eclipseKeyWord, const std::vector& resultData) -{ - QTextStream out(file); - out << "\n"; - out << "-- Exported from ResInsight" << "\n"; - out << 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++) - { - out << resultData[i]; - - if ( (i + 1) % 5 == 0) - { - out << "\n"; - } - - if (i % progressSteps == 0) - { - pi.setProgress(i); - } - } - - out << "\n" << "/" << "\n"; -} - - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationCode/FileInterface/RifEclipseInputFileTools.h b/ApplicationCode/FileInterface/RifEclipseInputFileTools.h index 5feb8da67d..638e5deeda 100644 --- a/ApplicationCode/FileInterface/RifEclipseInputFileTools.h +++ b/ApplicationCode/FileInterface/RifEclipseInputFileTools.h @@ -36,8 +36,6 @@ class RigEclipseCaseData; class QFile; -class RimEclipseResultDefinition; -class RigResultAccessor; //-------------------------------------------------------------------------------------------------- /// Structure used to cache file position of keywords @@ -76,10 +74,6 @@ public: static void parseAndReadPathAliasKeyword(const QString &fileName, std::vector< std::pair >* pathAliasDefinitions); - 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, cvf::ref resultAccessor, const QString& eclipseKeyWord, const double undefinedValue, const QString& logPrefix); - static bool readFaultsAndParseIncludeStatementsRecursively( QFile& file, qint64 startPos, const std::vector< std::pair >& pathAliasDefinitions, @@ -89,7 +83,6 @@ public: const QString& faultIncludeFileAbsolutePathPrefix); static cvf::StructGridInterface::FaceEnum faceEnumFromText(const QString& faceString); - static void writeDataToTextFile(QFile* file, const QString& eclipseKeyWord, const std::vector& resultData); private: static bool readDataFromKeyword(ecl_kw_type* eclipseKeywordData, RigEclipseCaseData* caseData, const QString& resultName);