diff --git a/src/fmu/dataio/_fmu_provider.py b/src/fmu/dataio/_fmu_provider.py index 94826f0ce..73dd93621 100644 --- a/src/fmu/dataio/_fmu_provider.py +++ b/src/fmu/dataio/_fmu_provider.py @@ -114,6 +114,9 @@ def __post_init__(self) -> None: logger.debug("FMU context is <%s>...", self.fmu_context) if not FmuEnv.ENSEMBLE_ID.value: + logger.debug( + "No ERT environment variables detected, provider will be empty" + ) return # not an FMU run self._provider = "ERT" diff --git a/src/fmu/dataio/_metadata.py b/src/fmu/dataio/_metadata.py index d3d0ee7c7..403ac60e4 100644 --- a/src/fmu/dataio/_metadata.py +++ b/src/fmu/dataio/_metadata.py @@ -292,26 +292,10 @@ def _populate_meta_fmu(self) -> None: ) logger.info("FMU provider is %s", fmudata.get_provider()) - if not fmudata.get_provider(): # e.g. run from RMS not in a FMU run - actual_context = ( - FmuContext.PREPROCESSED - if self.dataio.fmu_context == FmuContext.PREPROCESSED - else FmuContext.get("non_fmu") - ) - if self.dataio.fmu_context != actual_context: - logger.warning( - "Requested fmu_context is <%s> but since this is detected as a non " - "FMU run, the actual context is force set to <%s>", - self.dataio.fmu_context, - actual_context, - ) - self.dataio.fmu_context = actual_context - - else: - self.meta_fmu = fmudata.get_metadata() - self.rootpath = fmudata.get_casepath() - self.iter_name = fmudata.get_iter_name() - self.real_name = fmudata.get_real_name() + self.meta_fmu = fmudata.get_metadata() + self.rootpath = fmudata.get_casepath() + self.iter_name = fmudata.get_iter_name() + self.real_name = fmudata.get_real_name() logger.debug("Rootpath is now %s", self.rootpath) @@ -449,10 +433,12 @@ def generate_export_metadata( self._populate_meta_masterdata() self._populate_meta_access() + if self.dataio._fmurun: + self._populate_meta_fmu() + self._populate_meta_tracklog() self._populate_meta_objectdata() self._populate_meta_class() - self._populate_meta_fmu() self._populate_meta_file() self._populate_meta_display() self._populate_meta_xpreprocessed() diff --git a/src/fmu/dataio/dataio.py b/src/fmu/dataio/dataio.py index 3cab2fa6e..4f8c0b747 100644 --- a/src/fmu/dataio/dataio.py +++ b/src/fmu/dataio/dataio.py @@ -21,6 +21,7 @@ FmuContext, ValidationError, ) +from ._fmu_provider import FmuEnv from ._logging import null_logger from ._utils import ( create_symlink, @@ -445,6 +446,7 @@ class ExportData: _metadata: dict = field(default_factory=dict, init=False) _pwd: Path = field(default_factory=Path, init=False) _config_is_valid: bool = field(default=True, init=False) + _fmurun: bool = field(default=False, init=False) # << NB! storing ACTUAL casepath: _rootpath: Path = field(default_factory=Path, init=False) @@ -461,6 +463,7 @@ def __post_init__(self) -> None: logger.info("Running __post_init__ ExportData") logger.debug("Global config is %s", prettyprint_dict(self.config)) + self._fmurun = FmuEnv.ENSEMBLE_ID.value is not None self.fmu_context = FmuContext.get(self.fmu_context) # set defaults for mutable keys @@ -479,6 +482,7 @@ def __post_init__(self) -> None: self.config = some_config_from_env(GLOBAL_ENVNAME) or {} self._validate_content_key() + self._validate_fmucontext_key() self._update_globalconfig_from_settings() # check state of global config @@ -518,6 +522,16 @@ def _validate_fmucontext_key(self) -> None: if isinstance(self.fmu_context, str): self.fmu_context = FmuContext.get(self.fmu_context) + # fmu_context is only allowed to be preprocessed if not in a fmu run + if not self._fmurun and self.fmu_context != FmuContext.PREPROCESSED: + logger.warning( + "Requested fmu_context is <%s> but since this is detected as a non " + "FMU run, the actual context is force set to <%s>", + self.fmu_context, + FmuContext.get("non_fmu"), + ) + self.fmu_context = FmuContext.get("non_fmu") + def _update_fmt_flag(self) -> None: # treat special handling of "xtgeo" in format name: if self.points_fformat == "csv|xtgeo" or self.polygons_fformat == "csv|xtgeo": diff --git a/tests/test_units/test_dataio.py b/tests/test_units/test_dataio.py index 570e38f46..b181c593a 100644 --- a/tests/test_units/test_dataio.py +++ b/tests/test_units/test_dataio.py @@ -8,6 +8,8 @@ import pytest import yaml +from fmu.dataio._definitions import FmuContext +from fmu.dataio._fmu_provider import FmuEnv from fmu.dataio._utils import prettyprint_dict from fmu.dataio.dataio import ExportData, ValidationError, read_metadata @@ -332,6 +334,50 @@ def test_global_config_from_env(monkeypatch, global_config2_path): assert "smda" in edata.config["masterdata"] +def test_fmurun_attribute_outside_fmu(rmsglobalconfig): + """Test that _fmurun attribute is True when in fmu""" + + # check that ERT environment variable is not set + assert FmuEnv.ENSEMBLE_ID.value is None + + edata = ExportData(config=rmsglobalconfig, content="depth") + assert edata._fmurun is False + + +def test_fmurun_attribute_inside_fmu(fmurun_w_casemetadata, rmsglobalconfig): + """Test that _fmurun attribute is True when in fmu""" + + # check that ERT environment variable is not set + assert FmuEnv.ENSEMBLE_ID.value is not None + + edata = ExportData(config=rmsglobalconfig, content="depth") + assert edata._fmurun is True + + +def test_fmu_context_outside_fmu_input_overwrite(rmsglobalconfig): + """ + For non-fmu run fmu_context should be overwritten when input + is not "preprocessed" + """ + edata = ExportData( + config=rmsglobalconfig, content="depth", fmu_context="realization" + ) + assert edata._fmurun is False + assert edata.fmu_context == FmuContext.NON_FMU + + +def test_fmu_context_outside_fmu_no_input_overwrite(rmsglobalconfig): + """ + For non-fmu run fmu_context should not be overwritten when input + is "preprocessed" + """ + edata = ExportData( + config=rmsglobalconfig, content="depth", fmu_context="preprocessed" + ) + assert edata._fmurun is False + assert edata.fmu_context == FmuContext.PREPROCESSED + + def test_norwegian_letters_globalconfig( globalvars_norwegian_letters, regsurf,