diff --git a/ApplicationLibCode/CommandFileInterface/RicfComputeCaseGroupStatistics.cpp b/ApplicationLibCode/CommandFileInterface/RicfComputeCaseGroupStatistics.cpp index 1b95370216..92ed84e56e 100644 --- a/ApplicationLibCode/CommandFileInterface/RicfComputeCaseGroupStatistics.cpp +++ b/ApplicationLibCode/CommandFileInterface/RicfComputeCaseGroupStatistics.cpp @@ -18,19 +18,18 @@ #include "RicfComputeCaseGroupStatistics.h" -#include "Rim3dView.h" -#include "RimCaseCollection.h" -#include "RimEclipseCase.h" -#include "RimEclipseCaseCollection.h" -#include "RimEclipseStatisticsCase.h" -#include "RimIdenticalGridCaseGroup.h" -#include "RimOilField.h" -#include "RimProject.h" - #include "RiaLogging.h" +#include "RicfCommandForwarding.h" + +#include "RimEclipseStatisticsCase.h" +#include "RimProject.h" +#include "RimcEclipseStatisticsCase.h" + #include "cafPdmFieldScriptingCapability.h" +#include + CAF_PDM_SOURCE_INIT( RicfComputeCaseGroupStatistics, "computeCaseGroupStatistics" ); //-------------------------------------------------------------------------------------------------- @@ -47,58 +46,46 @@ RicfComputeCaseGroupStatistics::RicfComputeCaseGroupStatistics() //-------------------------------------------------------------------------------------------------- caf::PdmScriptResponse RicfComputeCaseGroupStatistics::execute() { + const QString commandName = classKeyword(); + caf::PdmScriptResponse response; - std::vector caseIds = m_caseIds.v(); + // Collect the statistics cases: explicit case ids, plus all statistics cases when a group id is given. + // Note: legacy behavior is to include statistics cases from all groups when caseGroupId >= 0. + std::vector statsCases; + + for ( int caseId : m_caseIds() ) + { + auto statsCase = RicfForwarding::findStatisticsCase( caseId ); + if ( !statsCase ) + { + QString warning = QString( "%1: %2" ).arg( commandName ).arg( statsCase.error() ); + RiaLogging::warning( warning.toStdString() ); + response.updateStatus( caf::PdmScriptResponse::COMMAND_WARNING, warning ); + continue; + } + statsCases.push_back( statsCase.value() ); + } if ( m_groupId() >= 0 ) { - for ( RimIdenticalGridCaseGroup* group : RimProject::current()->activeOilField()->analysisModels()->caseGroups ) + for ( RimEclipseStatisticsCase* statsCase : RimProject::current()->descendantsIncludingThisOfType() ) { - for ( RimEclipseCase* c : group->statisticsCaseCollection()->reservoirs ) + if ( statsCase && std::find( statsCases.begin(), statsCases.end(), statsCase ) == statsCases.end() ) { - caseIds.push_back( c->caseId() ); + statsCases.push_back( statsCase ); } } } - for ( int caseId : caseIds ) + for ( RimEclipseStatisticsCase* statsCase : statsCases ) { - bool foundCase = false; - for ( RimIdenticalGridCaseGroup* group : RimProject::current()->activeOilField()->analysisModels()->caseGroups ) - { - for ( RimEclipseCase* c : group->statisticsCaseCollection()->reservoirs ) - { - if ( c->caseId() == caseId ) - { - RimEclipseStatisticsCase* statsCase = dynamic_cast( c ); - if ( statsCase ) - { - statsCase->computeStatisticsAndUpdateViews(); - } - else - { - QString warning = QString( "computeCaseGroupStatistics: Found case with ID %1, but it is not a " - "statistics case, cannot compute statistics." ) - .arg( caseId ); - RiaLogging::warning( warning.toStdString() ); - response.updateStatus( caf::PdmScriptResponse::COMMAND_WARNING, warning ); - } - foundCase = true; - break; - } - } + RimcEclipseStatisticsCase_computeStatistics method( statsCase ); + method.setUpdateViews( true ); - if ( foundCase ) break; - } - - if ( !foundCase ) - { - QString warning = QString( "computeCaseGroupStatistics: Could not find statistics case with ID %1." ).arg( caseId ); - - RiaLogging::warning( warning.toStdString() ); - response.updateStatus( caf::PdmScriptResponse::COMMAND_WARNING, warning ); - } + auto result = method.execute(); + if ( !result ) return RicfForwarding::errorResponse( result.error(), commandName ); } + return response; } diff --git a/ApplicationLibCode/ProjectDataModelCommands/RimcEclipseStatisticsCase.cpp b/ApplicationLibCode/ProjectDataModelCommands/RimcEclipseStatisticsCase.cpp index 1d3459990a..79aecce54b 100644 --- a/ApplicationLibCode/ProjectDataModelCommands/RimcEclipseStatisticsCase.cpp +++ b/ApplicationLibCode/ProjectDataModelCommands/RimcEclipseStatisticsCase.cpp @@ -70,6 +70,23 @@ CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimEclipseStatisticsCase, RimcEclipseStatisti RimcEclipseStatisticsCase_computeStatistics::RimcEclipseStatisticsCase_computeStatistics( caf::PdmObjectHandle* self ) : caf::PdmVoidObjectMethod( self ) { + CAF_PDM_InitObject( "Compute Statistics", "", "", "Compute statistics for the selected source properties" ); + + CAF_PDM_InitScriptableField( &m_updateViews, + "UpdateViews", + false, + "Update Views", + "", + "", + "Update the 3D views of the case after computing, and create a view if none exists" ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimcEclipseStatisticsCase_computeStatistics::setUpdateViews( bool updateViews ) +{ + m_updateViews = updateViews; } //-------------------------------------------------------------------------------------------------- @@ -78,7 +95,16 @@ RimcEclipseStatisticsCase_computeStatistics::RimcEclipseStatisticsCase_computeSt std::expected RimcEclipseStatisticsCase_computeStatistics::execute() { auto eclipseCase = self(); - eclipseCase->computeStatistics(); + if ( !eclipseCase ) return std::unexpected( "No statistics case is available." ); + + if ( m_updateViews() ) + { + eclipseCase->computeStatisticsAndUpdateViews(); + } + else + { + eclipseCase->computeStatistics(); + } return nullptr; } diff --git a/ApplicationLibCode/ProjectDataModelCommands/RimcEclipseStatisticsCase.h b/ApplicationLibCode/ProjectDataModelCommands/RimcEclipseStatisticsCase.h index 38f7d036a7..5035a2aaa3 100644 --- a/ApplicationLibCode/ProjectDataModelCommands/RimcEclipseStatisticsCase.h +++ b/ApplicationLibCode/ProjectDataModelCommands/RimcEclipseStatisticsCase.h @@ -53,7 +53,12 @@ class RimcEclipseStatisticsCase_computeStatistics : public caf::PdmVoidObjectMet public: RimcEclipseStatisticsCase_computeStatistics( caf::PdmObjectHandle* self ); + void setUpdateViews( bool updateViews ); + std::expected execute() override; + +private: + caf::PdmField m_updateViews; }; //================================================================================================== diff --git a/GrpcInterface/Python/rips/gridcasegroup.py b/GrpcInterface/Python/rips/gridcasegroup.py index 0e4261b390..6dffc0a51a 100644 --- a/GrpcInterface/Python/rips/gridcasegroup.py +++ b/GrpcInterface/Python/rips/gridcasegroup.py @@ -6,7 +6,7 @@ from .pdmobject import add_method from .view import View as View from .case import Case as Case -import Commands_pb2 +import warnings from .resinsight_classes import GridCaseGroup from .resinsight_classes import EclipseView from .resinsight_classes import RimStatisticalCalculation @@ -60,14 +60,18 @@ def view(self, view_id): def compute_statistics(self, case_ids=None): """Compute statistics for the given case ids + Deprecated: use RimStatisticalCalculation.compute_statistics(update_views=True) on each + statistics case (see statistics_cases()) instead. + Arguments: case_ids(list of integers): List of case ids. If this is None all cases in group are included """ - if case_ids is None: - case_ids = [] - return self._execute_command( - computeCaseGroupStatistics=Commands_pb2.ComputeCaseGroupStatRequest( - caseIds=case_ids, caseGroupId=self.group_id - ) + warnings.warn( + "GridCaseGroup.compute_statistics() is deprecated, use compute_statistics() on each statistics case instead", + DeprecationWarning, + stacklevel=3, ) + for statistics_case in self.statistics_cases(): + if case_ids is None or statistics_case.id in case_ids: + statistics_case.compute_statistics(update_views=True)