Skip to content

Commit

Permalink
Support ISO-strings with dates for get_smry() (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
berland authored Dec 20, 2019
1 parent f37c40e commit 085916c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
30 changes: 19 additions & 11 deletions src/fmu/ensemble/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
import glob
from datetime import datetime
import dateutil

import six
import pandas as pd
Expand Down Expand Up @@ -951,23 +952,22 @@ def _get_smry_dates(eclsumsdates, freq, normalize, start_date, end_date):
in will have length 1, if not, it can be larger.
"""
import dateutil.parser
from .realization import normalize_dates

if not eclsumsdates:
return []

if start_date:
if isinstance(start_date, str):
start_date = dateutil.parser.parse(start_date).date()
start_date = dateutil.parser.isoparse(start_date).date()
elif isinstance(start_date, datetime.date):
pass
else:
raise TypeError("start_date had unknown type")

if end_date:
if isinstance(end_date, str):
end_date = dateutil.parser.parse(end_date).date()
end_date = dateutil.parser.isoparse(end_date).date()
elif isinstance(end_date, datetime.date):
pass
else:
Expand Down Expand Up @@ -1346,8 +1346,10 @@ def get_smry(
Args:
time_index: list of DateTime if interpolation is wanted
default is None, which returns the raw Eclipse report times
If a string is supplied, that string is attempted used
via get_smry_dates() in order to obtain a time index.
If a string with an ISO-8601 date is supplied, that date
is used directly, otherwise the string is assumed to indicate
a wanted frequencey for dates, daily, weekly, monthly, yearly,
that will be send to get_smry_dates()
column_keys: list of column key wildcards
cache_eclsum: boolean for whether to cache the EclSum
objects. Defaults to True. Set to False if
Expand All @@ -1368,12 +1370,18 @@ def get_smry(
no realizations, empty DataFrame is returned.
"""
if isinstance(time_index, str):
time_index = self.get_smry_dates(
time_index,
start_date=start_date,
end_date=end_date,
include_restart=include_restart,
)
# Try interpreting as ISO-date:
try:
parseddate = dateutil.parser.isoparse(time_index)
time_index = [parseddate]
# But this should fail when a frequency string is supplied:
except ValueError:
time_index = self.get_smry_dates(
time_index,
start_date=start_date,
end_date=end_date,
include_restart=include_restart,
)
dflist = []
for index, realization in self._realizations.items():
dframe = realization.get_smry(
Expand Down
20 changes: 13 additions & 7 deletions src/fmu/ensemble/realization.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,8 @@ def get_smry(
'yearly', 'monthly', 'daily', 'last' or 'raw', the latter will
return the simulated report steps (also default).
If a list of DateTime is supplied, data will be resampled
to these.
to these. If a date in ISO-8601 format is supplied, that is
used as a single date.
column_keys: list of column key wildcards. None means everything.
cache_eclsum: boolean for whether to keep the loaded EclSum
object in memory after data has been loaded.
Expand All @@ -1016,12 +1017,17 @@ def get_smry(
if isinstance(time_index, str) and time_index == "raw":
time_index_arg = None
elif isinstance(time_index, str):
time_index_arg = self.get_smry_dates(
freq=time_index,
start_date=start_date,
end_date=end_date,
include_restart=include_restart,
)
try:
parseddate = dateutil.parser.isoparse(time_index)
time_index_arg = [parseddate]
except ValueError:

time_index_arg = self.get_smry_dates(
freq=time_index,
start_date=start_date,
end_date=end_date,
include_restart=include_restart,
)
else:
time_index_arg = time_index

Expand Down
5 changes: 5 additions & 0 deletions tests/test_ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ def test_ensemble_ecl():
assert len(reekensemble.load_smry(column_keys=["FOPR"], time_index="last")) == 5
assert isinstance(reekensemble.get_df("unsmry--last.csv"), pd.DataFrame)

# Give ISO-dates directly:
assert (
len(reekensemble.get_smry(column_keys=["FOPR"], time_index="2001-01-02")) == 5
)

# Eclipse well names list
assert len(reekensemble.get_wellnames("OP*")) == 5
assert len(reekensemble.get_wellnames(None)) == 8
Expand Down
5 changes: 5 additions & 0 deletions tests/test_realization.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,11 @@ def test_singlereal_ecl(tmp="TMP"):
assert len(monthly) == 38
assert len(real.get_smry_dates(freq="daily")) == 1098

# Try ISO-date for time_index:
singledata = real.get_smry(time_index="2000-05-05", column_keys="FOPT")
assert "FOPT" in singledata
assert "2000-05-05" in singledata.index

# start and end should be included:
assert (
len(
Expand Down

0 comments on commit 085916c

Please sign in to comment.