diff --git a/src/fmu/ensemble/ensemble.py b/src/fmu/ensemble/ensemble.py index 9fcd4b8e..1f9d9b92 100644 --- a/src/fmu/ensemble/ensemble.py +++ b/src/fmu/ensemble/ensemble.py @@ -10,6 +10,7 @@ import os import glob from datetime import datetime +import dateutil import six import pandas as pd @@ -951,7 +952,6 @@ 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: @@ -959,7 +959,7 @@ def _get_smry_dates(eclsumsdates, freq, normalize, start_date, end_date): 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: @@ -967,7 +967,7 @@ def _get_smry_dates(eclsumsdates, freq, normalize, start_date, end_date): 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: @@ -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 @@ -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( diff --git a/src/fmu/ensemble/realization.py b/src/fmu/ensemble/realization.py index 7f8968f2..4d2e4a31 100644 --- a/src/fmu/ensemble/realization.py +++ b/src/fmu/ensemble/realization.py @@ -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. @@ -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 diff --git a/tests/test_ensemble.py b/tests/test_ensemble.py index f3df423e..01baf9ef 100644 --- a/tests/test_ensemble.py +++ b/tests/test_ensemble.py @@ -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 diff --git a/tests/test_realization.py b/tests/test_realization.py index 0639b7d7..91935634 100644 --- a/tests/test_realization.py +++ b/tests/test_realization.py @@ -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(