From bb5da52a2db9489a7cbe28ee95e63a76fe677f5f Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Thu, 5 Nov 2026 15:44:14 +0100 Subject: [PATCH] #15084: Add ensemble statistics as data source and Show Plot Data to Summary Table --- .../RimContextCommandBuilder.cpp | 1 + .../Summary/RimSummaryTable.cpp | 441 ++++++++++++++---- .../Summary/RimSummaryTable.h | 72 ++- 3 files changed, 417 insertions(+), 97 deletions(-) diff --git a/ApplicationLibCode/ProjectDataModel/RimContextCommandBuilder.cpp b/ApplicationLibCode/ProjectDataModel/RimContextCommandBuilder.cpp index 050011cc76..5e46c625f5 100644 --- a/ApplicationLibCode/ProjectDataModel/RimContextCommandBuilder.cpp +++ b/ApplicationLibCode/ProjectDataModel/RimContextCommandBuilder.cpp @@ -628,6 +628,7 @@ caf::CmdFeatureMenuBuilder RimContextCommandBuilder::commandsFromSelection() else if ( dynamic_cast( firstUiItem ) ) { menuBuilder << "RicDuplicateSummaryTableFeature"; + menuBuilder << "RicShowPlotDataFeature"; } else if ( dynamic_cast( firstUiItem ) ) { diff --git a/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryTable.cpp b/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryTable.cpp index e11d9ba011..68426f41b1 100644 --- a/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryTable.cpp +++ b/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryTable.cpp @@ -29,10 +29,14 @@ #include "RimEclipseResultCase.h" #include "RimEclipseView.h" +#include "RimEnsembleStatisticsCase.h" +#include "RimProject.h" #include "RimSummaryCase.h" #include "RimSummaryCaseMainCollection.h" +#include "RimSummaryEnsemble.h" #include "RimTools.h" +#include "RiuContextMenuLauncher.h" #include "RiuMatrixPlotWidget.h" #include "cafPdmUiCheckBoxAndTextEditor.h" @@ -43,6 +47,7 @@ #include #include +#include CAF_PDM_SOURCE_INIT( RimSummaryTable, "RimSummaryTable" ); @@ -58,6 +63,26 @@ void AppEnum::setUp() addItem( RimSummaryTable::RangeType::USER_DEFINED, "USER_DEFINED_MAX_MIN", "User Defined Range" ); setDefault( RimSummaryTable::RangeType::AUTOMATIC ); } + +template <> +void AppEnum::setUp() +{ + addItem( RimSummaryTable::DataSource::SINGLE_CASE, "SINGLE_CASE", "Single Case" ); + addItem( RimSummaryTable::DataSource::ENSEMBLE_STATISTICS, "ENSEMBLE_STATISTICS", "Ensemble Statistics" ); + setDefault( RimSummaryTable::DataSource::SINGLE_CASE ); +} + +template <> +void AppEnum::setUp() +{ + addItem( RimSummaryTable::EnsembleStatistic::MIN, "MIN", "Min" ); + addItem( RimSummaryTable::EnsembleStatistic::P90, "P90", "P90" ); + addItem( RimSummaryTable::EnsembleStatistic::P50, "P50", "P50" ); + addItem( RimSummaryTable::EnsembleStatistic::MEAN, "MEAN", "Mean" ); + addItem( RimSummaryTable::EnsembleStatistic::P10, "P10", "P10" ); + addItem( RimSummaryTable::EnsembleStatistic::MAX, "MAX", "Max" ); + setDefault( RimSummaryTable::EnsembleStatistic::MEAN ); +} } // namespace caf //-------------------------------------------------------------------------------------------------- @@ -71,8 +96,12 @@ RimSummaryTable::RimSummaryTable() CAF_PDM_InitField( &m_tableName, "TableName", QString( "Summary Table" ), "Name" ); CAF_PDM_InitField( &m_isAutomaticName, "AutomaticTableName", true, "Automatic Name" ); + CAF_PDM_InitFieldNoDefault( &m_dataSource, "DataSource", "Data Source" ); CAF_PDM_InitFieldNoDefault( &m_case, "SummaryCase", "Case" ); m_case.uiCapability()->setUiTreeChildrenHidden( true ); + CAF_PDM_InitFieldNoDefault( &m_ensemble, "SummaryEnsemble", "Ensemble" ); + m_ensemble.uiCapability()->setUiTreeChildrenHidden( true ); + CAF_PDM_InitFieldNoDefault( &m_ensembleStatistic, "EnsembleStatistic", "Statistic" ); CAF_PDM_InitFieldNoDefault( &m_vector, "Vector", "Vector" ); m_vector.uiCapability()->setUiEditorTypeName( caf::PdmUiComboBoxEditor::uiEditorTypeName() ); @@ -158,6 +187,8 @@ RimSummaryTable::RimSummaryTable() dockAsPlotWindow(); setShowWindow( true ); setDeletable( true ); + + m_ensembleStatisticsCase = std::make_unique(); } //-------------------------------------------------------------------------------------------------- @@ -185,14 +216,7 @@ void RimSummaryTable::setDefaultCaseAndCategoryAndVectorName() m_case = summaryCases.front(); - const auto summaryReader = summaryReaderWithAddresses(); - if ( !summaryReader ) return; - - const auto categoryVectors = getCategoryVectorFromSummaryReader( summaryReader, m_category() ); - if ( !categoryVectors.empty() ) - { - m_vector = *categoryVectors.begin(); - } + resetVectorToFirstAvailable(); m_tableName = createTableName(); initializeDateRange(); @@ -264,46 +288,32 @@ void RimSummaryTable::fieldChangedByUi( const caf::PdmFieldHandle* changedField, { RimPlotWindow::fieldChangedByUi( changedField, oldValue, newValue ); - if ( changedField == &m_case ) + if ( changedField == &m_case || changedField == &m_ensemble || changedField == &m_dataSource ) { - if ( m_case ) + clearEnsembleStatisticsCache(); + resetVectorToFirstAvailable(); + if ( changedField == &m_dataSource && m_dataSource() == DataSource::ENSEMBLE_STATISTICS && m_ensemble() == nullptr ) { - const auto summaryReader = summaryReaderWithAddresses(); - const auto categoryVectors = getCategoryVectorFromSummaryReader( summaryReader, m_category() ); - if ( summaryReader && !categoryVectors.empty() ) + for ( auto ensemble : RimProject::current()->summaryEnsembles() ) { - m_vector = *categoryVectors.begin(); - } - else - { - m_vector = QString( "" ); + if ( ensemble->isEnsemble() ) + { + m_ensemble = ensemble; + resetVectorToFirstAvailable(); + break; + } } } if ( m_isAutomaticName ) { m_tableName = createTableName(); } + if ( changedField != &m_case ) initializeDateRange(); onLoadDataAndUpdate(); } else if ( changedField == &m_category ) { - if ( m_case ) - { - const auto summaryReader = summaryReaderWithAddresses(); - const auto categoryVectors = getCategoryVectorFromSummaryReader( summaryReader, m_category() ); - if ( summaryReader && !categoryVectors.empty() ) - { - m_vector = *categoryVectors.begin(); - } - else - { - m_vector = QString( "" ); - } - } - else - { - m_vector = QString( " " ); - } + resetVectorToFirstAvailable(); if ( m_isAutomaticName ) { m_tableName = createTableName(); @@ -311,7 +321,7 @@ void RimSummaryTable::fieldChangedByUi( const caf::PdmFieldHandle* changedField, onLoadDataAndUpdate(); } else if ( changedField == &m_vector || changedField == &m_resamplingSelection || changedField == &m_upperThreshold || - changedField == &m_lowerThreshold ) + changedField == &m_lowerThreshold || changedField == &m_ensembleStatistic ) { if ( m_isAutomaticName ) { @@ -385,7 +395,16 @@ void RimSummaryTable::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering caf::PdmUiGroup& dataGroup = *uiOrdering.addNewGroup( "Plot Data" ); dataGroup.add( &m_tableName ); dataGroup.add( &m_isAutomaticName ); - dataGroup.add( &m_case ); + dataGroup.add( &m_dataSource ); + if ( isEnsembleStatistics() ) + { + dataGroup.add( &m_ensemble ); + dataGroup.add( &m_ensembleStatistic ); + } + else + { + dataGroup.add( &m_case ); + } dataGroup.add( &m_category ); dataGroup.add( &m_vector ); dataGroup.add( &m_resamplingSelection ); @@ -435,18 +454,24 @@ QList RimSummaryTable::calculateValueOptions( const caf: { options = RiaSummaryTools::optionsForAllSummaryCases(); } - else if ( fieldNeedingOptions == &m_vector && m_case ) + else if ( fieldNeedingOptions == &m_ensemble ) { - const auto summaryReader = summaryReaderWithAddresses(); - if ( summaryReader ) + for ( auto ensemble : RimProject::current()->summaryEnsembles() ) { - const auto categoryVectorsUnion = getCategoryVectorFromSummaryReader( summaryReader, m_category() ); - for ( const auto& vectorName : categoryVectorsUnion ) + if ( ensemble->isEnsemble() ) { - options.push_back( caf::PdmOptionItemInfo( vectorName, vectorName ) ); + options.push_back( caf::PdmOptionItemInfo( ensemble->name(), ensemble, false, ensemble->uiIconProvider() ) ); } } } + else if ( fieldNeedingOptions == &m_vector ) + { + const auto categoryVectorsUnion = getCategoryVectorFromAddresses( availableAddresses(), m_category() ); + for ( const auto& vectorName : categoryVectorsUnion ) + { + options.push_back( caf::PdmOptionItemInfo( vectorName, vectorName ) ); + } + } else if ( fieldNeedingOptions == &m_excludedRowsUiField ) { const auto vectorNames = RimSummaryTableTools::categoryNames( m_tableData.vectorDataCollection ); @@ -468,26 +493,16 @@ void RimSummaryTable::onLoadDataAndUpdate() createTableData(); setExcludedRowsUiSelectionsFromTableData(); - if ( m_matrixPlotWidget == nullptr || m_case == nullptr ) - { - return; - } - - const auto summaryReader = summaryReaderWithAddresses(); - if ( !summaryReader ) + if ( m_matrixPlotWidget == nullptr || !hasDataSource() ) { return; } // Exclude rows that are selected - const auto excludedRows = std::set( m_excludedRowsUiField().begin(), m_excludedRowsUiField().end() ); + const auto excludedRows = this->excludedRows(); // Convert to strings - std::vector timeStepStrings; - for ( const auto& timeStep : m_tableData.timeStepsUnion ) - { - timeStepStrings.push_back( RiaQDateTimeTools::fromTime_t( timeStep ).toString( dateFormatString() ) ); - } + const auto timeStepStrings = this->timeStepStrings(); // Clear matrix plot m_matrixPlotWidget->clearPlotData(); @@ -516,9 +531,10 @@ void RimSummaryTable::onLoadDataAndUpdate() } // Set titles and font sizes - const QString title = QString( "Summary Table - %1 [%2]
Date Resampling: %3
" ) + const QString title = QString( "Summary Table - %1 [%2]
%3Date Resampling: %4
" ) .arg( m_vector() ) .arg( m_tableData.unitName ) + .arg( isEnsembleStatistics() ? dataSourceDescription() + "
" : QString() ) .arg( m_resamplingSelection().uiText() ); m_matrixPlotWidget->setPlotTitle( title ); m_matrixPlotWidget->setRowTitle( QString( "%1s" ).arg( m_category().uiText() ) ); @@ -558,6 +574,9 @@ QWidget* RimSummaryTable::createViewWidget( QWidget* mainWindowParent ) m_matrixPlotWidget = new RiuMatrixPlotWidget( this, m_legendConfig, mainWindowParent ); m_matrixPlotWidget->setShowValueLabel( m_showValueLabels ); m_matrixPlotWidget->setUseInvalidValueColor( true ); + + new RiuContextMenuLauncher( m_matrixPlotWidget, { "RicShowPlotDataFeature" } ); + new RiuContextMenuLauncher( m_matrixPlotWidget->qwtPlot(), { "RicShowPlotDataFeature" } ); return m_matrixPlotWidget; } @@ -619,6 +638,10 @@ int RimSummaryTable::valueLabelFontSize() const //-------------------------------------------------------------------------------------------------- QString RimSummaryTable::createTableName() const { + if ( isEnsembleStatistics() ) + { + return QString( "Summary Table - %1 (%2)" ).arg( m_vector() ).arg( m_ensembleStatistic().uiText() ); + } return QString( "Summary Table - %1" ).arg( m_vector() ); } @@ -642,16 +665,13 @@ QString RimSummaryTable::dateFormatString() const //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -std::set RimSummaryTable::getSummaryAddressesFromReader( const RifSummaryReaderInterface* summaryReader, - RifEclipseSummaryAddressDefines::SummaryCategory category, - const QString& vector ) const +std::set RimSummaryTable::getSummaryAddressesFromAddresses( const std::set& allAddresses, + RifEclipseSummaryAddressDefines::SummaryCategory category, + const QString& vector ) const { - if ( !summaryReader ) return {}; - - std::set categoryAddresses; - const std::set allResultAddresses = summaryReader->allResultAddresses(); - const std::string selectedVector = vector.toStdString(); - for ( const auto& resAdr : allResultAddresses ) + std::set categoryAddresses; + const std::string selectedVector = vector.toStdString(); + for ( const auto& resAdr : allAddresses ) { if ( resAdr.category() != category || resAdr.vectorName() != selectedVector ) continue; categoryAddresses.emplace( resAdr ); @@ -662,10 +682,9 @@ std::set RimSummaryTable::getSummaryAddressesFromReade //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -std::set RimSummaryTable::getCategoryVectorFromSummaryReader( const RifSummaryReaderInterface* summaryReader, - RifEclipseSummaryAddressDefines::SummaryCategory category ) const +std::set RimSummaryTable::getCategoryVectorFromAddresses( const std::set& allAddresses, + RifEclipseSummaryAddressDefines::SummaryCategory category ) const { - if ( !summaryReader ) return {}; if ( category != RifEclipseSummaryAddressDefines::SummaryCategory::SUMMARY_WELL && category != RifEclipseSummaryAddressDefines::SummaryCategory::SUMMARY_GROUP && category != RifEclipseSummaryAddressDefines::SummaryCategory::SUMMARY_REGION ) @@ -673,9 +692,8 @@ std::set RimSummaryTable::getCategoryVectorFromSummaryReader( const Rif return {}; } - std::set categoryVectors; - const std::set allResultAddresses = summaryReader->allResultAddresses(); - for ( const auto& resAdr : allResultAddresses ) + std::set categoryVectors; + for ( const auto& resAdr : allAddresses ) { if ( resAdr.category() != category ) continue; const QString vectorName = QString::fromStdString( resAdr.vectorName() ); @@ -720,19 +738,159 @@ std::vector RimSummaryTable::getToplevelSummaryCases() const //-------------------------------------------------------------------------------------------------- void RimSummaryTable::initializeDateRange() { - auto summaryReader = summaryReaderWithAddresses(); - if ( !summaryReader ) return; + std::vector timeSteps; + if ( isEnsembleStatistics() ) + { + if ( !m_ensemble ) return; + const auto ensembleTimeSteps = m_ensemble->ensembleTimeSteps(); + timeSteps = std::vector( ensembleTimeSteps.begin(), ensembleTimeSteps.end() ); + } + else + { + auto summaryReader = summaryReaderWithAddresses(); + if ( !summaryReader ) return; - const std::set allResultAddresses = summaryReader->allResultAddresses(); - if ( allResultAddresses.empty() ) return; + const std::set allResultAddresses = summaryReader->allResultAddresses(); + if ( allResultAddresses.empty() ) return; - auto timeSteps = summaryReader->timeSteps( *allResultAddresses.begin() ); + timeSteps = summaryReader->timeSteps( *allResultAddresses.begin() ); + } if ( timeSteps.empty() ) return; m_startDate = RiaQDateTimeTools::fromTime_t( timeSteps.front() ); m_endDate = RiaQDateTimeTools::fromTime_t( timeSteps.back() ); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +std::set RimSummaryTable::availableAddresses() +{ + if ( isEnsembleStatistics() ) + { + if ( !m_ensemble ) return {}; + return m_ensemble->ensembleSummaryAddresses(); + } + + if ( auto summaryReader = summaryReaderWithAddresses() ) + { + return summaryReader->allResultAddresses(); + } + return {}; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool RimSummaryTable::isEnsembleStatistics() const +{ + return m_dataSource() == DataSource::ENSEMBLE_STATISTICS; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool RimSummaryTable::hasDataSource() const +{ + return isEnsembleStatistics() ? m_ensemble() != nullptr : m_case() != nullptr; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimSummaryTable::resetVectorToFirstAvailable() +{ + const auto categoryVectors = getCategoryVectorFromAddresses( availableAddresses(), m_category() ); + m_vector = categoryVectors.empty() ? QString( "" ) : *categoryVectors.begin(); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QString RimSummaryTable::dataSourceDescription() const +{ + if ( !isEnsembleStatistics() ) return m_case ? m_case->displayCaseName() : QString(); + + const QString ensembleName = m_ensemble ? m_ensemble->name() : QString(); + return QString( "Ensemble %1: %2" ).arg( ensembleName ).arg( m_ensembleStatistic().uiText() ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +std::vector RimSummaryTable::timeStepStrings() const +{ + std::vector strings; + for ( const auto& timeStep : m_tableData.timeStepsUnion ) + { + strings.push_back( RiaQDateTimeTools::fromTime_t( timeStep ).toString( dateFormatString() ) ); + } + return strings; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +std::set RimSummaryTable::excludedRows() const +{ + return std::set( m_excludedRowsUiField().begin(), m_excludedRowsUiField().end() ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QString RimSummaryTable::asciiDataForPlotExport() const +{ + const auto excludedRows = this->excludedRows(); + + // Build all cells first, so that each column can be padded to a common width. The padded cells are still + // separated by tabs, to keep the text both readable and easy to paste into a spreadsheet. + std::vector> rows; + + std::vector header = { m_category().uiText() }; + for ( const auto& timeStepString : timeStepStrings() ) + { + header.push_back( timeStepString ); + } + rows.push_back( header ); + + for ( const auto& vectorData : m_tableData.vectorDataCollection ) + { + if ( excludedRows.contains( vectorData.category ) ) continue; + + std::vector row = { vectorData.category }; + for ( const double value : vectorData.values ) + { + row.push_back( QString::number( value, 'g', 6 ) ); + } + rows.push_back( row ); + } + + std::vector columnWidths; + for ( const auto& row : rows ) + { + if ( columnWidths.size() < row.size() ) columnWidths.resize( row.size(), 0 ); + for ( size_t col = 0; col < row.size(); ++col ) + { + columnWidths[col] = std::max( columnWidths[col], row[col].size() ); + } + } + + QString text; + for ( const auto& row : rows ) + { + QStringList cells; + for ( size_t col = 0; col < row.size(); ++col ) + { + // Row names are left aligned, values and dates are right aligned + cells.push_back( col == 0 ? row[col].leftJustified( columnWidths[col] ) : row[col].rightJustified( columnWidths[col] ) ); + } + text += cells.join( "\t" ) + "\n"; + } + + return text; +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -752,6 +910,87 @@ RifSummaryReaderInterface* RimSummaryTable::summaryReaderWithAddresses() return nullptr; } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +const RimSummaryTable::EnsembleStatisticsData& RimSummaryTable::ensembleStatisticsData( const RifEclipseSummaryAddress& address ) +{ + // The cache is invalid if the ensemble or the number of realizations has changed + const size_t caseCount = m_ensemble ? m_ensemble->allSummaryCases().size() : 0; + if ( m_ensembleStatisticsCacheEnsemble != m_ensemble() || m_ensembleStatisticsCacheCaseCount != caseCount ) + { + clearEnsembleStatisticsCache(); + m_ensembleStatisticsCacheEnsemble = m_ensemble(); + m_ensembleStatisticsCacheCaseCount = caseCount; + } + + if ( auto it = m_ensembleStatisticsCache.find( address ); it != m_ensembleStatisticsCache.end() ) return it->second; + + EnsembleStatisticsData data; + if ( m_ensemble ) + { + m_ensembleStatisticsCase->calculate( m_ensemble->allSummaryCases(), address, false, { 0, 100 } ); + + // Percentiles use the switched (oil industry) convention: percentile 0 is the maximum, percentile 100 is the minimum + const auto statisticsAddress = [&address]( EnsembleStatistic statistic ) + { + auto adr = address; + switch ( statistic ) + { + case EnsembleStatistic::MIN: + adr.setStatisticsType( RifEclipseSummaryAddressDefines::StatisticsType::CUSTOM ); + adr.setPercentile( 100 ); + break; + case EnsembleStatistic::MAX: + adr.setStatisticsType( RifEclipseSummaryAddressDefines::StatisticsType::CUSTOM ); + adr.setPercentile( 0 ); + break; + case EnsembleStatistic::P10: + adr.setStatisticsType( RifEclipseSummaryAddressDefines::StatisticsType::P10 ); + break; + case EnsembleStatistic::P50: + adr.setStatisticsType( RifEclipseSummaryAddressDefines::StatisticsType::P50 ); + break; + case EnsembleStatistic::P90: + adr.setStatisticsType( RifEclipseSummaryAddressDefines::StatisticsType::P90 ); + break; + case EnsembleStatistic::MEAN: + adr.setStatisticsType( RifEclipseSummaryAddressDefines::StatisticsType::MEAN ); + break; + } + return adr; + }; + + data.timeSteps = m_ensembleStatisticsCase->timeSteps( address ); + + // The unit is read from the first realization, which only knows the plain (non-statistics) address + data.unitName = m_ensembleStatisticsCase->unitName( address ); + + for ( auto statistic : { EnsembleStatistic::MIN, + EnsembleStatistic::P90, + EnsembleStatistic::P50, + EnsembleStatistic::MEAN, + EnsembleStatistic::P10, + EnsembleStatistic::MAX } ) + { + auto [isOk, values] = m_ensembleStatisticsCase->values( statisticsAddress( statistic ) ); + data.values[statistic] = std::move( values ); + } + } + + return m_ensembleStatisticsCache.emplace( address, std::move( data ) ).first->second; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimSummaryTable::clearEnsembleStatisticsCache() +{ + m_ensembleStatisticsCache.clear(); + m_ensembleStatisticsCacheEnsemble = nullptr; + m_ensembleStatisticsCacheCaseCount = 0; +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -766,21 +1005,49 @@ void RimSummaryTable::createTableData() const auto lowerThreshold = m_tableData.lowerThresholdValue; const bool useThresholdFilter = upperThreshold || lowerThreshold; - const auto summaryReader = summaryReaderWithAddresses(); - if ( !summaryReader ) return; + // Get all summary addresses for selected category (group, region, well) + RifSummaryReaderInterface* summaryReader = nullptr; + if ( !isEnsembleStatistics() ) + { + summaryReader = summaryReaderWithAddresses(); + if ( !summaryReader ) return; + } + else if ( !m_ensemble ) + { + return; + } // Create time step value for vectors with no values above threshold const time_t invalidTimeStep = 0; - // Get all summary addresses for selected category (group, region, well) - const auto summaryAddresses = getSummaryAddressesFromReader( summaryReader, m_category(), m_vector() ); - QString unitName; + const auto summaryAddresses = getSummaryAddressesFromAddresses( availableAddresses(), m_category(), m_vector() ); + + // Time steps, values and unit name for one address. Ensemble statistics are calculated from all realizations. + const auto readTimeStepsAndValues = + [&]( const RifEclipseSummaryAddress& adr ) -> std::tuple, std::vector, std::string> + { + if ( summaryReader ) + { + auto [isOk, values] = summaryReader->values( adr ); + return { summaryReader->timeSteps( adr ), values, summaryReader->unitName( adr ) }; + } + + // Ensemble statistics for all statistic types are calculated once per address, and reused when only the selected + // statistic, the thresholds or other display settings change + const auto& statisticsData = ensembleStatisticsData( adr ); + const auto valuesIt = statisticsData.values.find( m_ensembleStatistic() ); + if ( valuesIt == statisticsData.values.end() ) return {}; + + return { statisticsData.timeSteps, valuesIt->second, statisticsData.unitName }; + }; + for ( const auto& adr : summaryAddresses ) { - auto [isOk, values] = summaryReader->values( adr ); - const std::vector timeSteps = summaryReader->timeSteps( adr ); - const QString vectorName = QString::fromStdString( adr.vectorName() ); - const QString categoryName = getCategoryNameFromAddress( adr ); + const auto [timeSteps, values, unitName] = readTimeStepsAndValues( adr ); + const QString vectorName = QString::fromStdString( adr.vectorName() ); + const QString categoryName = getCategoryNameFromAddress( adr ); + + if ( timeSteps.size() != values.size() ) continue; // Get resampled time steps and values auto [resampledTimeSteps, resampledValues] = @@ -866,7 +1133,7 @@ void RimSummaryTable::createTableData() // Set unit name if ( m_tableData.unitName.isEmpty() ) { - m_tableData.unitName = QString::fromStdString( summaryReader->unitName( adr ) ); + m_tableData.unitName = QString::fromStdString( unitName ); } } diff --git a/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryTable.h b/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryTable.h index 6cf007ff1e..52b70ddab4 100644 --- a/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryTable.h +++ b/ApplicationLibCode/ProjectDataModel/Summary/RimSummaryTable.h @@ -18,10 +18,12 @@ #pragma once +#include "RimPlainPlotDataProducer.h" #include "RimPlotWindow.h" #include "RiaDateTimeDefines.h" +#include "RifEclipseSummaryAddress.h" #include "RifEclipseSummaryAddressDefines.h" #include "RimRegularLegendConfig.h" @@ -34,8 +36,15 @@ #include #include +#include +#include +#include +#include + class RifSummaryReaderInterface; class RimSummaryCase; +class RimSummaryEnsemble; +class RimEnsembleStatisticsCase; class RimRegularLegendConfig; class RiuMatrixPlotWidget; class RifEclipseSummaryAddress; @@ -43,7 +52,7 @@ class RifEclipseSummaryAddress; //================================================================================================== /// //================================================================================================== -class RimSummaryTable : public RimPlotWindow +class RimSummaryTable : public RimPlotWindow, public RimPlainPlotDataProducer { CAF_PDM_HEADER_INIT; @@ -54,6 +63,22 @@ public: USER_DEFINED }; + enum class DataSource + { + SINGLE_CASE, + ENSEMBLE_STATISTICS + }; + + enum class EnsembleStatistic + { + MIN, + P90, + P50, + MEAN, + P10, + MAX + }; + public: RimSummaryTable(); ~RimSummaryTable() override; @@ -64,6 +89,7 @@ public: const QString& vectorName ); void setDescription( const QString& description ); QString description() const override; + QString asciiDataForPlotExport() const override; private: void cleanupBeforeClose(); @@ -98,25 +124,35 @@ private: std::pair createLegendMinMaxValues( const double maxTableValue ) const; QString dateFormatString() const; - std::set getSummaryAddressesFromReader( const RifSummaryReaderInterface* summaryReader, - RifEclipseSummaryAddressDefines::SummaryCategory category, - const QString& vector ) const; - std::set getCategoryVectorFromSummaryReader( const RifSummaryReaderInterface* summaryReader, - RifEclipseSummaryAddressDefines::SummaryCategory category ) const; + std::set getSummaryAddressesFromAddresses( const std::set& allAddresses, + RifEclipseSummaryAddressDefines::SummaryCategory category, + const QString& vector ) const; + std::set getCategoryVectorFromAddresses( const std::set& allAddresses, + RifEclipseSummaryAddressDefines::SummaryCategory category ) const; QString getCategoryNameFromAddress( const RifEclipseSummaryAddress& address ) const; std::vector getToplevelSummaryCases() const; void initializeDateRange(); - RifSummaryReaderInterface* summaryReaderWithAddresses(); + RifSummaryReaderInterface* summaryReaderWithAddresses(); + std::set availableAddresses(); + bool hasDataSource() const; + bool isEnsembleStatistics() const; + void resetVectorToFirstAvailable(); + QString dataSourceDescription() const; + std::vector timeStepStrings() const; + std::set excludedRows() const; private: // Matrix plot for visualizing table data QPointer m_matrixPlotWidget; - caf::PdmField m_tableName; - caf::PdmField m_isAutomaticName; - caf::PdmPtrField m_case; + caf::PdmField m_tableName; + caf::PdmField m_isAutomaticName; + caf::PdmField> m_dataSource; + caf::PdmPtrField m_case; + caf::PdmPtrField m_ensemble; + caf::PdmField> m_ensembleStatistic; caf::PdmField> m_category; caf::PdmField m_vector; @@ -148,6 +184,22 @@ private: TableData m_tableData; + std::unique_ptr m_ensembleStatisticsCase; + + struct EnsembleStatisticsData + { + std::vector timeSteps; + std::map> values; + std::string unitName; + }; + + std::map m_ensembleStatisticsCache; + const RimSummaryEnsemble* m_ensembleStatisticsCacheEnsemble = nullptr; + size_t m_ensembleStatisticsCacheCaseCount = 0; + + const EnsembleStatisticsData& ensembleStatisticsData( const RifEclipseSummaryAddress& address ); + void clearEnsembleStatisticsCache(); + void createTableData(); void setExcludedRowsUiSelectionsFromTableData(); };