diff --git a/src/fmu/dataio/_filedata_provider.py b/src/fmu/dataio/_filedata_provider.py index d298bbc75..cc9426e00 100644 --- a/src/fmu/dataio/_filedata_provider.py +++ b/src/fmu/dataio/_filedata_provider.py @@ -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(" ", "_") diff --git a/src/fmu/dataio/_objectdata_provider.py b/src/fmu/dataio/_objectdata_provider.py index 5217bdce1..1f31577ac 100644 --- a/src/fmu/dataio/_objectdata_provider.py +++ b/src/fmu/dataio/_objectdata_provider.py @@ -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()} @@ -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() @@ -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 diff --git a/src/fmu/dataio/_utils.py b/src/fmu/dataio/_utils.py index 7dd8a74db..4c72d73ad 100644 --- a/src/fmu/dataio/_utils.py +++ b/src/fmu/dataio/_utils.py @@ -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) diff --git a/src/fmu/dataio/dataio.py b/src/fmu/dataio/dataio.py index 5bbe41b15..9d9d9e559 100644 --- a/src/fmu/dataio/dataio.py +++ b/src/fmu/dataio/dataio.py @@ -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 diff --git a/tests/test_units/test_filedataprovider_class.py b/tests/test_units/test_filedataprovider_class.py index d9de12d1a..0f0f4375e 100644 --- a/tests/test_units/test_filedataprovider_class.py +++ b/tests/test_units/test_filedataprovider_class.py @@ -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", ), ( @@ -47,8 +47,8 @@ "name", "", "", - 20220102, 20210101, + 20220102, "name--20220102_20210101", ), ], @@ -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 @@ -89,8 +90,8 @@ def test_get_filestem( "", "tag", "parent", - "2022-01-02", "2020-01-01", + "2022-01-02", "'name' entry is missing", ), ( @@ -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 diff --git a/tests/test_units/test_rms_context.py b/tests/test_units/test_rms_context.py index 862d27692..01bfeca9b 100644 --- a/tests/test_units/test_rms_context.py +++ b/tests/test_units/test_rms_context.py @@ -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 @@ -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, @@ -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"