From b8e95829aaaf909a4c285680b2fd43508b82db82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Berland?= Date: Thu, 12 Aug 2021 15:23:44 +0200 Subject: [PATCH] Solve bug from inplace substitution in ecldiff2roff --- src/subscript/ecldiff2roff/ecldiff2roff.py | 15 +-- tests/test_ecldiff2roff.py | 105 ++++++++++++++++++++- 2 files changed, 109 insertions(+), 11 deletions(-) diff --git a/src/subscript/ecldiff2roff/ecldiff2roff.py b/src/subscript/ecldiff2roff/ecldiff2roff.py index 9407cc199..c96cf6eeb 100644 --- a/src/subscript/ecldiff2roff/ecldiff2roff.py +++ b/src/subscript/ecldiff2roff/ecldiff2roff.py @@ -1,11 +1,11 @@ import argparse import logging -import dateutil +from typing import List, Set, Tuple, Union +import dateutil.parser import xtgeo # type: ignore -import subscript -from typing import List, Tuple, Set, Union +import subscript logger = subscript.getLogger(__name__) @@ -163,7 +163,7 @@ def ecldiff2roff_main( for date_pair in diffdates: alldates = alldates.union(set(date_pair)) - ecl_grid = xtgeo.grid3d.Grid().from_file( + ecl_grid = xtgeo.grid_from_file( eclroot, fformat="eclipserun", restartprops=[prop], restartdates=alldates ) logger.info("Loaded UNRST data at %s dates from %s", len(alldates), eclroot) @@ -191,8 +191,9 @@ def ecldiff2roff_main( str(date_pair[0]), str(date_pair[1]), ) - # Inplace substraction, prop1 = prop1 - prop2 - prop1.values -= prop2.values + + diffprop = prop1.copy() + diffprop.values = prop1.values - prop2.values diffpropname = ( prop.lower() @@ -204,7 +205,7 @@ def ecldiff2roff_main( filename = outputfilebase + sep + diffpropname + ".roff" logger.info("Writing to file %s", filename) - prop1.to_file(filename, name=diffpropname) + diffprop.to_file(filename, name=diffpropname) def main(): diff --git a/tests/test_ecldiff2roff.py b/tests/test_ecldiff2roff.py index 9ce82f39e..4f14b9ff9 100644 --- a/tests/test_ecldiff2roff.py +++ b/tests/test_ecldiff2roff.py @@ -1,14 +1,14 @@ -import os import logging +import os import shutil import subprocess from pathlib import Path +import numpy as np import pytest - -from subscript.ecldiff2roff import ecldiff2roff - +import xtgeo from subscript import getLogger +from subscript.ecldiff2roff import ecldiff2roff logger = getLogger("subscript.ecldiff2roff.ecldiff2roff") logger.setLevel(logging.INFO) @@ -170,6 +170,103 @@ def test_mainfunction( assert Path(expected_file).exists() +def test_values_dateorder(reek_data): + """Verify the handling of date order in date pairs, that the + sign of values gets correct and it negated when dates are reversed""" + ecldiff2roff.ecldiff2roff_main( + "2_R001_REEK-0", "PRESSURE", [("20000101", "20000701")] + ) + pressure_diff1 = xtgeo.gridproperty_from_file( + "eclgrid--pressure--20000101_20000701.roff" + ) + + # This tools works such that a reduction in pressure gives a positive value when + # the dates "increase". This test assumes that the Reek dataset declines + # in pressure: + assert pressure_diff1.values.mean() > 0.0 + + # Also verify the actual mean value: + assert np.isclose(pressure_diff1.values.mean(), 29.6174076) + + # Check that when the dates are reversed, the difference is negated: + ecldiff2roff.ecldiff2roff_main( + "2_R001_REEK-0", "PRESSURE", [("20000701", "20000101")] + ) + pressure_diff_reverse = xtgeo.gridproperty_from_file( + "eclgrid--pressure--20000701_20000101.roff" + ) + assert np.isclose( + pressure_diff1.values.mean(), -pressure_diff_reverse.values.mean() + ) + + +def test_values_samedate(reek_data): + """Verify that the same date yields zero change""" + ecldiff2roff.ecldiff2roff_main( + "2_R001_REEK-0", "PRESSURE", [("20000101", "20000101")] + ) + pressure_diff = xtgeo.gridproperty_from_file( + "eclgrid--pressure--20000101_20000101.roff" + ) + assert np.isclose(pressure_diff.values.mean(), 0.0) + + +def test_values_multiple_datepairs(reek_data): + """Check that differences for multiple date pairs are handled correctly + + This was a bug in subscript up to v0.12.0""" + + # First establish some thruths: + ecldiff2roff.ecldiff2roff_main( + "2_R001_REEK-0", "PRESSURE", [("20000101", "20000701")] + ) + pressure_diff1_singlerun = xtgeo.gridproperty_from_file( + "eclgrid--pressure--20000101_20000701.roff" + ) + assert np.isclose(pressure_diff1_singlerun.values.mean(), 29.6174076) + + ecldiff2roff.ecldiff2roff_main( + "2_R001_REEK-0", "PRESSURE", [("20000701", "20010201")] + ) + pressure_diff2_singlerun = xtgeo.gridproperty_from_file( + "eclgrid--pressure--20000701_20010201.roff" + ) + assert np.isclose(pressure_diff2_singlerun.values.mean(), 12.570824) + + ecldiff2roff.ecldiff2roff_main( + "2_R001_REEK-0", "PRESSURE", [("20000101", "20010201")] + ) + pressure_diff3_singlerun = xtgeo.gridproperty_from_file( + "eclgrid--pressure--20000101_20010201.roff" + ) + assert np.isclose(pressure_diff3_singlerun.values.mean(), 42.18823213) + + # Then run with multiple datepairs: + ecldiff2roff.ecldiff2roff_main( + "2_R001_REEK-0", + "PRESSURE", + [("20000101", "20000701"), ("20000701", "20010201"), ("20000101", "20010201")], + ) + pressure_diff1 = xtgeo.gridproperty_from_file( + "eclgrid--pressure--20000101_20000701.roff" + ) + pressure_diff2 = xtgeo.gridproperty_from_file( + "eclgrid--pressure--20000701_20010201.roff" + ) + pressure_diff3 = xtgeo.gridproperty_from_file( + "eclgrid--pressure--20000101_20010201.roff" + ) + assert np.isclose( + pressure_diff1.values.mean(), pressure_diff1_singlerun.values.mean() + ) + assert np.isclose( + pressure_diff2.values.mean(), pressure_diff2_singlerun.values.mean() + ) + assert np.isclose( + pressure_diff3.values.mean(), pressure_diff3_singlerun.values.mean() + ) + + def test_errors(reek_data): """Test errors from the module""" # pylint: disable=unused-argument