Skip to content

Commit

Permalink
BUG: Resolve restart_path correctly (#811)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnatt authored and ErichSuter committed Sep 26, 2024
1 parent 2ba7699 commit 1114ef3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
40 changes: 26 additions & 14 deletions src/fmu/dataio/providers/_fmu.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
from typing import TYPE_CHECKING, Final, Optional, Union
from warnings import warn

import pydantic

from fmu.config import utilities as ut
from fmu.dataio import _utils
from fmu.dataio._logging import null_logger
Expand Down Expand Up @@ -248,26 +250,36 @@ def _get_restart_data_uuid(self) -> UUID | None:
"""Load restart_from information"""
assert self._runpath is not None
logger.debug("Detected a restart run from environment variable")
restart_path = self._runpath / os.environ[RESTART_PATH_ENVNAME]
restart_case_metafile = (
restart_path.parent.parent / ERT_RELATIVE_CASE_METADATA_FILE
).resolve()
restart_path = (self._runpath / os.environ[RESTART_PATH_ENVNAME]).resolve()

if not restart_case_metafile.exists():
if _casepath_has_metadata(restart_path.parent.parent):
restart_case_metafile = (
restart_path.parent.parent / ERT_RELATIVE_CASE_METADATA_FILE
)
else:
warn(
f"{RESTART_PATH_ENVNAME} environment variable is set to "
f"{os.environ[RESTART_PATH_ENVNAME]} which is invalid. Metadata "
"restart_from will remain empty.",
f"Environment variable {RESTART_PATH_ENVNAME} resolves to the path "
f"{restart_path} which is non existing or points to an ERT run "
"without case metdata. Metadata 'restart_from' will remain empty.",
UserWarning,
)
return None

restart_metadata = schema.InternalCaseMetadata.model_validate(
ut.yaml_load(restart_case_metafile, loader="standard")
)
return _utils.uuid_from_string(
f"{restart_metadata.fmu.case.uuid}{restart_path.name}"
)
try:
restart_metadata = schema.InternalCaseMetadata.model_validate(
ut.yaml_load(restart_case_metafile)
)
return _utils.uuid_from_string(
f"{restart_metadata.fmu.case.uuid}{restart_path.name}"
)
except pydantic.ValidationError as err:
warn(
"The case metadata for the restart ensemble is invalid "
f"{restart_case_metafile}. Metadata 'restart_from' will remain empty. "
f"Detailed information: \n {str(err)}",
UserWarning,
)
return None

def _get_ert_parameters(self) -> fields.Parameters | None:
logger.debug("Read ERT parameters")
Expand Down
26 changes: 25 additions & 1 deletion tests/test_units/test_fmuprovider_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,30 @@ def test_fmuprovider_valid_restart_env(monkeypatch, fmurun_w_casemetadata, fmuru
assert meta_restart.iteration.restart_from == meta_restart_from.iteration.uuid


def test_fmuprovider_valid_relative_restart_env(
monkeypatch, fmurun_w_casemetadata, fmurun_pred
):
"""
Test giving a valid RESTART_FROM_PATH environment variable that contains
a relative path from the existing runpath, which is a common use case.
"""
monkeypatch.chdir(fmurun_w_casemetadata)
meta_restart_from = FmuProvider(
model=GLOBAL_CONFIG_MODEL, fmu_context=FMUContext.realization
).get_metadata()

# using a relative path as input
monkeypatch.setenv(RESTART_PATH_ENVNAME, "../iter-0")

monkeypatch.chdir(fmurun_pred)
meta_restart = FmuProvider(
model=GLOBAL_CONFIG_MODEL, fmu_context=FMUContext.realization
).get_metadata()

assert meta_restart.iteration.restart_from is not None
assert meta_restart.iteration.restart_from == meta_restart_from.iteration.uuid


def test_fmuprovider_invalid_restart_env(
monkeypatch, fmurun_w_casemetadata, fmurun_pred
):
Expand All @@ -283,7 +307,7 @@ def test_fmuprovider_invalid_restart_env(
monkeypatch.setenv(RESTART_PATH_ENVNAME, "/path/to/somewhere/invalid")

os.chdir(fmurun_pred)
with pytest.warns(UserWarning, match="RESTART_FROM_PATH environment variable"):
with pytest.warns(UserWarning, match="non existing"):
meta = FmuProvider(
model=GLOBAL_CONFIG_MODEL, fmu_context=FMUContext.realization
).get_metadata()
Expand Down

0 comments on commit 1114ef3

Please sign in to comment.