diff --git a/ApplicationLibCode/ProjectDataModel/ContourMap/RimContourMapProjection.cpp b/ApplicationLibCode/ProjectDataModel/ContourMap/RimContourMapProjection.cpp index a078e47b19..b53cba7d66 100644 --- a/ApplicationLibCode/ProjectDataModel/ContourMap/RimContourMapProjection.cpp +++ b/ApplicationLibCode/ProjectDataModel/ContourMap/RimContourMapProjection.cpp @@ -57,7 +57,7 @@ void RimContourMapProjection::ResultAggregation::setUp() addItem( RigContourMapCalculator::GEOMETRIC_MEAN, "GEOM_VALUE", "Geometric Mean" ); addItem( RigContourMapCalculator::SUM, "SUM", "Sum" ); - addItem( RigContourMapCalculator::TOP_VALUE, "TOP_VALUE", "Top Value" ); + addItem( RigContourMapCalculator::TOP_VALUE, "TOP_VALUE", "Top Value" ); addItem( RigContourMapCalculator::MIN_VALUE, "MIN_VALUE", "Min Value" ); addItem( RigContourMapCalculator::MAX_VALUE, "MAX_VALUE", "Max Value" ); diff --git a/ApplicationLibCode/ReservoirDataModel/ContourMap/RigContourMapCalculator.cpp b/ApplicationLibCode/ReservoirDataModel/ContourMap/RigContourMapCalculator.cpp index 65918234a7..748b42e6e9 100644 --- a/ApplicationLibCode/ReservoirDataModel/ContourMap/RigContourMapCalculator.cpp +++ b/ApplicationLibCode/ReservoirDataModel/ContourMap/RigContourMapCalculator.cpp @@ -79,14 +79,38 @@ double RigContourMapCalculator::calculateTopValue( const RigContourMapProjection const std::vector>& matchingCells, const std::vector& gridCellValues ) { + std::vector cellIndices; + cellIndices.reserve( matchingCells.size() ); for ( auto [cellIdx, weight] : matchingCells ) { - double cellValue = gridCellValues[contourMapProjection.gridResultIndex( cellIdx )]; + cellIndices.push_back( cellIdx ); + } + + // Sort the cells based on k layer as first criteria, then on cell index + std::sort( cellIndices.begin(), + cellIndices.end(), + [&]( size_t a, size_t b ) + { + if ( contourMapProjection.kLayer( a ) == contourMapProjection.kLayer( b ) ) + { + return a < b; + } + return contourMapProjection.kLayer( a ) < contourMapProjection.kLayer( b ); + } ); + + // Find the first cell with a valid value + for ( auto cellIdx : cellIndices ) + { + auto gridResultIndex = contourMapProjection.gridResultIndex( cellIdx ); + if ( gridResultIndex >= gridCellValues.size() ) continue; + + double cellValue = gridCellValues[gridResultIndex]; if ( std::abs( cellValue ) != std::numeric_limits::infinity() ) { return cellValue; } } + return std::numeric_limits::infinity(); }