Skip to content

Commit

Permalink
Merge pull request #440 from joakim-hove/numpy_vector_report_only
Browse files Browse the repository at this point in the history
Add optional report_only argument to EclSum.numpy_vector
  • Loading branch information
joakim-hove authored Jul 13, 2018
2 parents 3b18522 + 51de6fe commit 6af6af7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
14 changes: 13 additions & 1 deletion python/ecl/summary/ecl_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def _make_time_vector(self, time_index):
time_points.append(t)
return time_points

def numpy_vector(self, key, time_index = None):
def numpy_vector(self, key, time_index=None, report_only=False):
"""Will return numpy vector of all the values corresponding to @key.
The optional argument @time_index can be used to limit the time points
Expand All @@ -450,10 +450,22 @@ def numpy_vector(self, key, time_index = None):
The function will raise KeyError if the requested key does not exist.
If many keys are needed it will be faster to use the pandas_frame()
function.
If you set the optional argument report_only to True the you will only
get values at the report dates. Observe that passing report_only=True
can not be combined with a value for time_index, that will give you a
ValueError exception.
"""
if key not in self:
raise KeyError("No such key:%s" % key)

if report_only:
if time_index is None:
time_index = self.dates
else:
raise ValueError("Can not suuply both time_index and report_only=True")

if time_index is None:
np_vector = numpy.zeros(len(self))
self._init_numpy_vector(key ,np_vector.ctypes.data_as(ctypes.POINTER(ctypes.c_double)))
Expand Down
7 changes: 6 additions & 1 deletion python/tests/ecl_tests/test_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ def test_numpy_vector(self):
dates = [datetime.datetime(2000,1,1)] + case.dates + [datetime.datetime(2020,1,1)]
fopr = case.numpy_vector("FOPR", time_index = dates)
fopt = case.numpy_vector("FOPT", time_index = dates)

self.assertEqual(len(fopt), len(dates))

self.assertEqual(fopr[0], 0)
self.assertEqual(fopr[-1], 0)
Expand All @@ -532,6 +532,11 @@ def test_numpy_vector(self):
self.assertEqual(fopt[0], case.first_value("FOPT"))
self.assertEqual(fopt[-1], case.last_value("FOPT"))

with self.assertRaises(ValueError):
v = case.numpy_vector("FOPR", time_index=dates, report_only=True)

v = case.numpy_vector("FOPR", report_only=True)
self.assertEqual(len(v), len(case.dates))


def test_pandas(self):
Expand Down

0 comments on commit 6af6af7

Please sign in to comment.