Skip to content

Commit

Permalink
Ensure correct data.name when preprocessed/merged
Browse files Browse the repository at this point in the history
Now the true stratigraphic name is propagated correctly from pre-
processed to FMU merged result. Some refaktor/simplification; needed
variables such as input name, tagname and subfolder from preprocessed
are tmp stored under a _preprocessed field in the metadata.
  • Loading branch information
jcrivenaes committed Jan 3, 2023
1 parent 5f76f6e commit 4bd1e0a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 23 deletions.
13 changes: 13 additions & 0 deletions src/fmu/dataio/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ class _MetaData:
meta_file: dict = field(default_factory=dict, init=False)
meta_tracklog: list = field(default_factory=list, init=False)
meta_fmu: dict = field(default_factory=dict, init=False)
# temporary storage for preprocessed data:
meta_xpreprocessed: dict = field(default_factory=dict, init=False)

# relevant when ERT* fmu_context; same as rootpath in the ExportData class!:
rootpath: str = field(default="", init=False)
Expand Down Expand Up @@ -282,6 +284,13 @@ def _populate_meta_access(self):
if self.dataio:
self.meta_access = generate_meta_access(self.dataio.config)

def _populate_meta_xpreprocessed(self):
"""Populate a few necessary 'tmp' metadata needed for preprocessed data."""
if self.dataio.fmu_context == "preprocessed":
self.meta_xpreprocessed["name"] = self.dataio.name
self.meta_xpreprocessed["tagname"] = self.dataio.tagname
self.meta_xpreprocessed["subfolder"] = self.dataio.subfolder

def _reuse_existing_metadata(self, meta):
"""Perform a merge procedure if the key `reuse_metadata_rule` is active."""
if self.dataio and self.dataio.reuse_metadata_rule:
Expand All @@ -307,6 +316,7 @@ def generate_export_metadata(self, skip_null=True) -> dict: # TODO! -> skip_nul
self._populate_meta_class()
self._populate_meta_fmu()
self._populate_meta_file()
self._populate_meta_xpreprocessed()

# glue together metadata, order is as legacy code (but will be screwed if reuse
# of existing metadata...)
Expand All @@ -323,6 +333,9 @@ def generate_export_metadata(self, skip_null=True) -> dict: # TODO! -> skip_nul
meta["access"] = self.meta_access
meta["masterdata"] = self.meta_masterdata

if self.dataio.fmu_context == "preprocessed":
meta["_preprocessed"] = self.meta_xpreprocessed

if skip_null:
meta = drop_nones(meta)

Expand Down
7 changes: 4 additions & 3 deletions src/fmu/dataio/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,16 +450,17 @@ def glue_metadata_preprocessed(oldmeta, newmeta):
"""Glue (combine) to metadata dicts according to rule 'preprocessed'."""

meta = oldmeta.copy()

if "_preprocessed" in meta:
del meta["_preprocessed"]

meta["fmu"] = newmeta["fmu"]
meta["file"] = newmeta["file"]
meta["access"] = newmeta["access"]

newmeta["tracklog"][-1]["event"] = "merged"
meta["tracklog"].extend(newmeta["tracklog"])

# the only field in 'data' that are allowed to update is name:
meta["data"]["name"] = newmeta["data"]["name"]

return meta


Expand Down
32 changes: 16 additions & 16 deletions src/fmu/dataio/dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,22 +733,22 @@ def _check_obj_if_file(self, obj: Any) -> Any:
self.reuse_metadata_rule = "preprocessed"

currentmeta = read_metadata(obj)
if not self.name and currentmeta["data"].get("name", ""):
self.name = currentmeta["data"]["name"]

if not self.tagname and currentmeta["data"].get("tagname", ""):
self.tagname = currentmeta["data"]["tagname"]

# detect if object is on a subfolder relative to /preprocessed/xxxx
for ipar in range(3):
foldername = obj.parents[ipar].stem
if foldername == "preprocessed" and ipar == 2:
if not self.subfolder:
self.subfolder = obj.parents[0].stem
logger.info(
"Subfolder is auto-derived from preprocessed file path: %s",
self.subfolder,
)
if "_preprocessed" not in currentmeta:
raise ValidationError(
"The special entry for preprocessed data <_preprocessed> is"
"missing in the metadata. A possible solution is to rerun the"
"preprocessed export."
)

if not self.name and currentmeta["_preprocessed"].get("name", ""):
self.name = currentmeta["_preprocessed"]["name"]

if not self.tagname and currentmeta["_preprocessed"].get("tagname", ""):
self.tagname = currentmeta["_preprocessed"]["tagname"]

if not self.subfolder and currentmeta["_preprocessed"].get("subfolder", ""):
self.subfolder = currentmeta["_preprocessed"]["subfolder"]

return obj

# ==================================================================================
Expand Down
12 changes: 8 additions & 4 deletions tests/test_units/test_prerealization_surfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf):
edata = dataio.ExportData(
config=rmsglobalconfig, # read from global config
fmu_context="preprocessed",
name="preprocessedmap",
name="TopVolantis",
is_observation=True,
timedata=[[20240802, "moni"], [20200909, "base"]],
)
Expand All @@ -101,8 +101,10 @@ def _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf):

assert (
metadata["file"]["relative_path"]
== "share/preprocessed/maps/preprocessedmap--20240802_20200909.gri"
== "share/preprocessed/maps/topvolantis--20240802_20200909.gri"
)
assert metadata["data"]["name"] == "VOLANTIS GP. Top"
assert "_preprocessed" in metadata

return edata.export(regsurf)

Expand All @@ -123,7 +125,6 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath):
edata = dataio.ExportData(
config=rmsglobalconfig, # read from global config
fmu_context="case",
name="preprocessed_v2",
is_observation=True,
)
metadata = edata.generate_metadata(
Expand All @@ -132,9 +133,12 @@ def _run_case_fmu(fmurun_w_casemetadata, rmsglobalconfig, surfacepath):
logger.debug("\n%s", utils.prettyprint_dict(metadata))
assert (
metadata["file"]["relative_path"]
== "share/observations/maps/preprocessed_v2--20240802_20200909.gri"
== "share/observations/maps/topvolantis--20240802_20200909.gri"
)
assert "merged" in metadata["tracklog"][-1]["event"]
assert metadata["data"]["name"] == "VOLANTIS GP. Top"
assert "TopVolantis" in metadata["data"]["alias"]
assert "_preprocessed" not in metadata

# run two stage process
mysurf = _export_data_from_rms(rmssetup, rmsglobalconfig, regsurf)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_units/test_rms_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ def test_regsurf_export_file_set_name(rmssetup, rmsglobalconfig, regsurf):
(edata._rootpath / "share/results/maps/topvolantis.gri").resolve()
)

meta = edata.generate_metadata(
regsurf,
name="TopVolantis",
)
assert meta["data"]["name"] == "VOLANTIS GP. Top" # strat name from SMDA
assert "TopVolantis" in meta["data"]["alias"]


@inside_rms
def test_regsurf_metadata_with_timedata(rmssetup, rmsglobalconfig, regsurf):
Expand Down

0 comments on commit 4bd1e0a

Please sign in to comment.