Skip to content

Commit

Permalink
Solve bug from inplace substitution in ecldiff2roff
Browse files Browse the repository at this point in the history
  • Loading branch information
berland committed Aug 17, 2021
1 parent fa7807d commit b8e9582
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 11 deletions.
15 changes: 8 additions & 7 deletions src/subscript/ecldiff2roff/ecldiff2roff.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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():
Expand Down
105 changes: 101 additions & 4 deletions tests/test_ecldiff2roff.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b8e9582

Please sign in to comment.