Skip to content

Commit

Permalink
CLN: Move fmu_context validation for non-fmu runs to ExportData
Browse files Browse the repository at this point in the history
  • Loading branch information
tnatt committed Mar 18, 2024
1 parent 481714e commit c08e594
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 21 deletions.
3 changes: 3 additions & 0 deletions src/fmu/dataio/_fmu_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
28 changes: 7 additions & 21 deletions src/fmu/dataio/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions src/fmu/dataio/dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
FmuContext,
ValidationError,
)
from ._fmu_provider import FmuEnv
from ._logging import null_logger
from ._utils import (
create_symlink,
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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":
Expand Down
46 changes: 46 additions & 0 deletions tests/test_units/test_dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c08e594

Please sign in to comment.