Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Support restart cases with no iteration folder #827

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/fmu/dataio/providers/_fmu.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
# case metadata relative to casepath
ERT_RELATIVE_CASE_METADATA_FILE: Final = "share/metadata/fmu_case.yml"
RESTART_PATH_ENVNAME: Final = "RESTART_FROM_PATH"
DEFAULT_ITER_NAME: Final = "iter-0"

logger: Final = null_logger(__name__)

Expand Down Expand Up @@ -145,7 +146,7 @@ def __post_init__(self) -> None:
self._real_name = self._runpath.parent.name
else:
logger.debug("No iteration folder found, using default name iter-0")
self._iter_name = "iter-0"
self._iter_name = DEFAULT_ITER_NAME
self._real_name = self._runpath.name

logger.debug("Found iter name from runpath: %s", self._iter_name)
Expand Down Expand Up @@ -256,6 +257,12 @@ def _get_restart_data_uuid(self) -> UUID | None:
restart_case_metafile = (
restart_path.parent.parent / ERT_RELATIVE_CASE_METADATA_FILE
)
restart_iter_name = restart_path.name
elif _casepath_has_metadata(restart_path.parent):
restart_case_metafile = (
restart_path.parent / ERT_RELATIVE_CASE_METADATA_FILE
)
restart_iter_name = DEFAULT_ITER_NAME
else:
warn(
f"Environment variable {RESTART_PATH_ENVNAME} resolves to the path "
Expand All @@ -270,7 +277,7 @@ def _get_restart_data_uuid(self) -> UUID | None:
ut.yaml_load(restart_case_metafile)
)
return _utils.uuid_from_string(
f"{restart_metadata.fmu.case.uuid}{restart_path.name}"
f"{restart_metadata.fmu.case.uuid}{restart_iter_name}"
)
except pydantic.ValidationError as err:
warn(
Expand Down
27 changes: 26 additions & 1 deletion tests/test_units/test_fmuprovider_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
# from conftest import pretend_ert_env_run1
from fmu.dataio._model.enums import FMUContext
from fmu.dataio.exceptions import InvalidMetadataError
from fmu.dataio.providers._fmu import RESTART_PATH_ENVNAME, FmuEnv, FmuProvider
from fmu.dataio.providers._fmu import (
DEFAULT_ITER_NAME,
RESTART_PATH_ENVNAME,
FmuEnv,
FmuProvider,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -293,6 +298,26 @@ def test_fmuprovider_valid_relative_restart_env(
assert meta_restart.iteration.restart_from == meta_restart_from.iteration.uuid


def test_fmuprovider_restart_env_no_iter_folder(
monkeypatch, fmurun_no_iter_folder, fmurun_pred
):
"""
Test giving a valid RESTART_FROM_PATH environment variable
for a fmu run without iteration folders
"""
monkeypatch.chdir(fmurun_no_iter_folder)
meta_restart_from = FmuProvider(fmu_context=FMUContext.realization).get_metadata()
assert meta_restart_from.iteration.name == DEFAULT_ITER_NAME

# using a relative path as input
monkeypatch.setenv(RESTART_PATH_ENVNAME, str(fmurun_no_iter_folder))

monkeypatch.chdir(fmurun_pred)
meta_restart = FmuProvider(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 Down