Skip to content

Commit

Permalink
Correct timedata inconsistencies/bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrivenaes committed Sep 6, 2022
1 parent c13e9ca commit e248e09
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 38 deletions.
7 changes: 5 additions & 2 deletions src/fmu/dataio/_filedata_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,15 @@ def _get_filestem(self):
stem += "--" + (str(self.time0)[0:10]).replace("-", "")

elif self.time0 and self.time1:
monitor = (str(self.time0)[0:10]).replace("-", "")
base = (str(self.time1)[0:10]).replace("-", "")
monitor = (str(self.time1)[0:10]).replace("-", "")
base = (str(self.time0)[0:10]).replace("-", "")
if monitor == base:
warn(
"The monitor date and base date are equal", UserWarning
) # TODO: consider add clocktimes in such cases?
if self.dataio.filename_timedata_reverse: # class variable
stem += "--" + base + "_" + monitor
else:
stem += "--" + monitor + "_" + base

stem = stem.replace(".", "_").replace(" ", "_")
Expand Down
69 changes: 47 additions & 22 deletions src/fmu/dataio/_objectdata_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,11 @@ def _derive_timedata(self):

def _derive_timedata_legacy(self):
"""Format input timedata to metadata. legacy version."""

tdata = self.dataio.timedata

tresult = dict()
if len(tdata) >= 1:
tresult["time"] = list()
if len(tdata) == 1:
elem = tdata[0]
tresult["time"] = list()
xfield = {"value": dt.strptime(str(elem[0]), "%Y%m%d").isoformat()}
Expand All @@ -494,20 +494,39 @@ def _derive_timedata_legacy(self):
xfield["label"] = elem[1]
tresult["time"].append(xfield)
if len(tdata) == 2:
elem = tdata[1]
xfield = {"value": dt.strptime(str(elem[0]), "%Y%m%d").isoformat()}
self.time1 = str(elem[0])
if len(elem) == 2:
xfield["label"] = elem[1]
self.time0 = str(elem[1])
tresult["time"].append(xfield)

elem1 = tdata[0]
xfield1 = {"value": dt.strptime(str(elem1[0]), "%Y%m%d").isoformat()}
if len(elem1) == 2:
xfield1["label"] = elem1[1]

elem2 = tdata[1]
xfield2 = {"value": dt.strptime(str(elem2[0]), "%Y%m%d").isoformat()}
if len(elem2) == 2:
xfield2["label"] = elem2[1]

if xfield1["value"] < xfield2["value"]:
tresult["time"].append(xfield1)
tresult["time"].append(xfield2)
else:
tresult["time"].append(xfield2)
tresult["time"].append(xfield1)

self.time0 = tresult["time"][0]["value"]
self.time1 = tresult["time"][1]["value"]

logger.info("Timedata: time0 is %s while time1 is %s", self.time0, self.time1)
return tresult

def _derive_timedata_newformat(self):
"""Format input timedata to metadata, new format."""
"""Format input timedata to metadata, new format.
When using two dates, input convention is [[newestdate, "monitor"], [oldestdate,
"base"]] but it is possible to turn around. But in the metadata the output t0
shall always be older than t1 so need to check, and by general rule the file
will be some--time1_time0 where time1 is the newest (unless a class variable is
set for those who wants it turned around).
"""
tdata = self.dataio.timedata
tresult = dict()

Expand All @@ -520,19 +539,25 @@ def _derive_timedata_newformat(self):
xfield["label"] = elem[1]
tresult["t0"] = xfield
if len(tdata) == 2:
elem = tdata[1]
xfield = {"value": dt.strptime(str(elem[0]), "%Y%m%d").isoformat()}
self.time1 = str(elem[0])
if len(elem) == 2:
xfield["label"] = elem[1]
tresult["t0"] = xfield
elem1 = tdata[0]
xfield1 = {"value": dt.strptime(str(elem1[0]), "%Y%m%d").isoformat()}
if len(elem1) == 2:
xfield1["label"] = elem1[1]

elem2 = tdata[1]
xfield2 = {"value": dt.strptime(str(elem2[0]), "%Y%m%d").isoformat()}
if len(elem2) == 2:
xfield2["label"] = elem2[1]

if xfield1["value"] < xfield2["value"]:
tresult["t0"] = xfield1
tresult["t1"] = xfield2
else:
tresult["t0"] = xfield2
tresult["t1"] = xfield1

elem = tdata[0]
xfield = {"value": dt.strptime(str(elem[0]), "%Y%m%d").isoformat()}
self.time0 = str(elem[0])
if len(elem) == 2:
xfield["label"] = elem[1]
tresult["t1"] = xfield
self.time0 = tresult["t0"]["value"]
self.time1 = tresult["t1"]["value"]

logger.info("Timedata: time0 is %s while time1 is %s", self.time0, self.time1)
return tresult
Expand Down
63 changes: 63 additions & 0 deletions src/fmu/dataio/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,66 @@ def generate_description(desc: Optional[Union[str, list]] = None) -> Union[list,
return desc
else:
raise ValueError("Description of wrong type, must be list of strings or string")

def parse_timedata(datablock: dict, isoformat=True):
"""The time section under datablock has variants to parse.
Formats::
"time": {
"t0": {
"value": "2022-08-02T00:00:00",
"label": "base"
}
}
# with or without t1
# or legacy format:
"time": [
{
"value": "2030-01-01T00:00:00",
"label": "moni"
},
{
"value": "2010-02-03T00:00:00",
"label": "base"
}
],
In addition, need to parse the dates on isoformat string format to YYYMMDD
Args:
datablock: The data block section from a metadata record
Returns
(t0, t1) where t0 is e.g. "20220907" as string objects and/or None if not
isoformat, while t0 is on form "2030-01-23T00:00:00" if isoformat is True
"""
date0 = None
date1 = None
if "time" not in datablock:
return (None, None)

if isinstance(datablock["time"], list):
date0 = datablock["time"][0]["value"]

if len(datablock["time"] == 2):
date1 = datablock["time"][1]["value"]

elif isinstance(datablock["time"], dict):
date0 = datablock["time"]["t0"].get("value")
if "t1" in datablock["time"]:
date1 = datablock["time"]["t1"].get("value")

if not isoformat:
if date0:
tdate0 = datetime.strptime(date0, "%Y-%m-%dT%H:%M:%S")
date0 = tdate0.datetime.strftime("%Y%m%d")

if date1:
tdate1 = datetime.strptime(date1, "%Y-%m-%dT%H:%M:%S")
date1 = tdate1.datetime.strftime("%Y%m%d")

return (date0, date1)
6 changes: 4 additions & 2 deletions src/fmu/dataio/dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,11 @@ class ExportData:
data:
t0:
value: 2018010T00:00:00 description: base
value: 2018010T00:00:00
description: base
t1:
value: 202020101T00:00:00 description: monitor
value: 202020101T00:00:00
description: monitor
The output files will be on the form: somename--t1_t0.ext
Expand Down
13 changes: 7 additions & 6 deletions tests/test_units/test_filedataprovider_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
"name",
"tag",
"parent",
"2022-01-02",
"2020-01-01",
"2022-01-02",
"parent--name--tag--20220102_20200101",
),
(
"name",
"",
"",
"2022-01-02",
"2020-01-01",
"2022-01-02",
"name--20220102_20200101",
),
(
Expand All @@ -47,8 +47,8 @@
"name",
"",
"",
20220102,
20210101,
20220102,
"name--20220102_20210101",
),
],
Expand All @@ -66,6 +66,7 @@ def test_get_filestem(
"""Testing the private _get_filestem method."""
objdata = _ObjectDataProvider(regsurf, edataobj1)
objdata.name = name
# time0 is always the oldest
objdata.time0 = time0
objdata.time1 = time1

Expand All @@ -89,8 +90,8 @@ def test_get_filestem(
"",
"tag",
"parent",
"2022-01-02",
"2020-01-01",
"2022-01-02",
"'name' entry is missing",
),
(
Expand Down Expand Up @@ -189,8 +190,8 @@ def test_filedata_provider(regsurf, edataobj1, tmp_path):
fdata.derive_filedata()

print(fdata.relative_path)
assert fdata.relative_path == "share/results/efolder/parent--name--tag--t1_t2.ext"
absdata = str(tmp_path / "share/results/efolder/parent--name--tag--t1_t2.ext")
assert fdata.relative_path == "share/results/efolder/parent--name--tag--t2_t1.ext"
absdata = str(tmp_path / "share/results/efolder/parent--name--tag--t2_t1.ext")
assert fdata.absolute_path == absdata


Expand Down
15 changes: 9 additions & 6 deletions tests/test_units/test_rms_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_regsurf_metadata_with_timedata(rmssetup, rmsglobalconfig, regsurf):
assert meta1["data"]["time"]["t0"]["label"] == "one"
assert meta1["data"]["time"].get("t1", None) is None

logger.info(prettyprint_dict(meta1))
logger.debug(prettyprint_dict(meta1))


@inside_rms
Expand All @@ -145,12 +145,13 @@ def test_regsurf_metadata_with_timedata_legacy(rmssetup, rmsglobalconfig, regsur
timedata=[[20300101, "moni"], [20100203, "base"]],
verbosity="INFO",
)
logger.info(prettyprint_dict(meta1))
logger.debug(prettyprint_dict(meta1))
assert "topvolantis--20300101_20100203" in meta1["file"]["relative_path"]

assert meta1["data"]["time"][1]["value"] == "2010-02-03T00:00:00"
assert meta1["data"]["time"][1]["label"] == "base"
assert meta1["data"]["time"][0]["value"] == "2030-01-01T00:00:00"
assert meta1["data"]["time"][0]["label"] == "moni"
assert meta1["data"]["time"][0]["value"] == "2010-02-03T00:00:00"
assert meta1["data"]["time"][0]["label"] == "base"
assert meta1["data"]["time"][1]["value"] == "2030-01-01T00:00:00"
assert meta1["data"]["time"][1]["label"] == "moni"

meta1 = edata.generate_metadata(
regsurf,
Expand All @@ -159,6 +160,8 @@ def test_regsurf_metadata_with_timedata_legacy(rmssetup, rmsglobalconfig, regsur
verbosity="INFO",
)

logger.debug(prettyprint_dict(meta1))

assert meta1["data"]["time"][0]["value"] == "2030-01-23T00:00:00"
assert meta1["data"]["time"][0]["label"] == "one"

Expand Down

0 comments on commit e248e09

Please sign in to comment.