Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@

from fmu.dataio.dataio import ExportData

CFG = ut.yaml_load("../../fmuconfig/output/global_variables.yml")
SCRIPT_DIR = Path(__file__).resolve().parent
EXAMPLES_ROOT = SCRIPT_DIR.parents[1]

DEPTH_FILE = Path("../output/maps/structure/") / "topvolantis--ds_extract_geogrid.gri"
CFG = ut.yaml_load(EXAMPLES_ROOT / "fmuconfig/output/global_variables.yml")

DEPTH_FILE = (
SCRIPT_DIR.parent / "output/maps/structure/topvolantis--ds_extract_geogrid.gri"
)


def export_preprocessed_surface():
"""Export a preprocessed surface with metadata."""

export_data = ExportData(
config=CFG,
preprocessed=True,
name="preprocessedmap",
fmu_context="case",
content="depth",
is_observation=True,
subfolder="mysub",
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ filterwarnings = [
"ignore:The value of 'vertical_domain' is 'depth'.*vertical_domain.*will be set to 'time':UserWarning",
"ignore:Setting 'rep_include' from the config is deprecated:FutureWarning",
"ignore:Providing input settings through environment variables is deprecated:FutureWarning",
"ignore:The 'fmu_context' argument is deprecated:FutureWarning",
# Ignore mutation deprecation warnings (tested in test_dataio.py::test_mutation_*)
"ignore:Mutating ExportData\\..* after initialization is deprecated:FutureWarning",
# Ignore importlib numpy size incompatibility warning
Expand Down
19 changes: 16 additions & 3 deletions src/fmu/dataio/_export/_export_config_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,13 @@ def _resolve_fmu_context(
)

effective_context = _determine_effective_fmu_context(fmu_context_input, env_context)
if (
preprocessed
and fmu_context_input is None
and effective_context == FMUContext.realization
):
effective_context = FMUContext.case
Comment on lines +361 to +366

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not need to the fmu_context_input here, if fmu_context_input was None the effective_context was set from environment. So this should be enough

Suggested change
if (
preprocessed
and fmu_context_input is None
and effective_context == FMUContext.realization
):
effective_context = FMUContext.case
if (
preprocessed
and effective_context == FMUContext.realization
):
effective_context = FMUContext.case

Another note, the change here would essentially allow all realizations running in parallell to export to the same file, so might cause problems..

I know we have an error saying preprocessed should be run outside of ERT or with context case... but this seems wrong to me, preprocessed is only intended to be run outside of an ERT run, and then to be included into the run by the COPY_PREPROCESSED ERT workflow. Hence I struggle to see the use case for supporting case context here.

Could we make an issue to deprecate preprocessed in case context? 🙂


_validate_fmu_context_combination(effective_context, preprocessed)

return effective_context, preprocessed
Expand Down Expand Up @@ -389,6 +396,13 @@ def _handle_fmu_context_deprecations(
Returns:
Tuple of (transformed_fmu_context, transformed_preprocessed).
"""
if fmu_context_input is not None and fmu_context_input != "preprocessed":
warnings.warn(
"The 'fmu_context' argument is deprecated and will be removed in the "
"future. fmu-dataio now infers the FMU context from the environment.",
FutureWarning,
)

if fmu_context_input == "preprocessed":
warnings.warn(
"Using the 'fmu_context' argument with value 'preprocessed' is "
Expand Down Expand Up @@ -468,9 +482,8 @@ def _validate_fmu_context_combination(
if preprocessed and context == FMUContext.realization:
raise ValueError(
"Can't export preprocessed data in a fmu_context='realization'. "
"Preprocessed data should be exported with fmu_context='case' or "
"outside of FMU entirely, and then re-exported using "
"ExportPreprocessedData."
"Preprocessed data should be exported in case context or outside of "
"FMU entirely, and then re-exported using ExportPreprocessedData."
)


Expand Down
2 changes: 1 addition & 1 deletion src/fmu/dataio/dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ class ExportData:
# Does not appear to have been used for anything.

fmu_context: str | None = None
# Optional string with value ``realization`` or ``case``.
# Deprecated. fmu-dataio infers this from the environment.
#
# .. tip::
# You most likely do not need to set this. fmu-dataio infers this by itself.
Expand Down
8 changes: 8 additions & 0 deletions tests/test_units/test_dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def convert_datestr_to_isoformat(value: str, format: str = "%Y%m%d") -> str:
return datetime.strptime(value, format).isoformat()


def test_exportdata_fmu_context_argument_is_deprecated(
mock_global_config: dict[str, Any],
) -> None:
"""Setting fmu_context on ExportData emits a deprecation warning."""
with pytest.warns(FutureWarning, match="'fmu_context' argument is deprecated"):
ExportData(config=mock_global_config, content="depth", fmu_context="case")


def test_generate_metadata_simple(mock_global_config: dict[str, Any]) -> None:
"""Test generating metadata"""

Expand Down
37 changes: 33 additions & 4 deletions tests/test_units/test_fmu_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,29 @@ def test_fmu_context_preprocessed_emits_deprecation_warning() -> None:
assert fmu_context is None


@pytest.mark.parametrize("explicit_context", ["realization", "case", "ensemble"])
def test_fmu_context_argument_emits_deprecation_warning(
explicit_context: str,
) -> None:
"""Using fmu_context explicitly triggers deprecation warning."""
with pytest.warns(FutureWarning, match="'fmu_context' argument is deprecated"):
fmu_context, preprocessed = _handle_fmu_context_deprecations(
fmu_context_input=explicit_context,
preprocessed_input=False,
)

assert fmu_context == explicit_context
assert preprocessed is False


@pytest.mark.parametrize("iteration_variant", ["iteration", "ITERATION", "Iteration"])
def test_iteration_converted_to_ensemble(iteration_variant: str) -> None:
"""Using "iteration" context is converted to "ensemble"."""
fmu_context, preprocessed = _handle_fmu_context_deprecations(
fmu_context_input=iteration_variant,
preprocessed_input=False,
)
with pytest.warns(FutureWarning, match="'fmu_context' argument is deprecated"):
fmu_context, preprocessed = _handle_fmu_context_deprecations(
fmu_context_input=iteration_variant,
preprocessed_input=False,
)

assert fmu_context == "ensemble"

Expand Down Expand Up @@ -214,3 +230,16 @@ def test_resolve_fmu_context_integration_with_preprocessed(
preprocessed_input=True,
)
assert preprocessed is True


def test_resolve_preprocessed_uses_case_context_from_realization_env(
runpath_no_dotfmu: Path,
) -> None:
"""Preprocessed exports in realization env are written at case level."""
context, preprocessed = _resolve_fmu_context(
fmu_context_input=None,
preprocessed_input=True,
)

assert context == FMUContext.case
assert preprocessed is True