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

Transfer selected blocks from global_variables to case metadata #339

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
18 changes: 18 additions & 0 deletions schema/definitions/0.8.0/examples/case.yml
Copy link
Contributor

Choose a reason for hiding this comment

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

I see that the definition of blocks to be in the case metadata seems logical, but isn't it a bit challenging with the contacts and other relevant things, could be volume factors etc.

Copy link
Member Author

Choose a reason for hiding this comment

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

In this proposal, only one new block in the (case) metadata, which is fmu.case.config. And then "whatever" into that. I.e. we don't validate in detail, so fmu-dataio can technically include anything into that block.

But given the ongoing discussion around parameters.txt where we are leaning towards having that as a separate data object, we might discuss this PR in that perspective as well. Should case config be a separate data object? 🤷‍♂️

Copy link
Member Author

Choose a reason for hiding this comment

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

And, another point - which is possibly what you meant - is access control and information classification. We cannot have a situation where someone is to be granted access to only parts of the case metadata, that would not work.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ fmu: # the fmu-block contains information directly related to the FMU context

context:
stage: case

config:
globals: # transferred from global_variables.yml
REGIONS:
WestLowland:
NUM: 1
OWC: 1660.0
FWL: 1660.0
GOC: 1000.0
COL: (238, 221, 130)
EQT: 0
CentralSouth:
NUM: 2
OWC: 1677.0
FWL: 1677.0
GOC: 1000.0
EQT: 0
COL: (105, 89, 205)

access:
asset:
Expand Down
7 changes: 7 additions & 0 deletions schema/definitions/0.8.0/schema/fmu_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,10 @@
}
}
},
"config": {
"type": "object",
"description": "Transferred elements from global config"
},
"iteration": {
"type": "object",
"required": [
Expand Down Expand Up @@ -1226,6 +1230,9 @@
},
"case": {
"$ref": "#/definitions/fmu/case"
},
"config": {
"$ref": "#/definitions/fmu/config"
}
}
},
Expand Down
9 changes: 9 additions & 0 deletions src/fmu/dataio/_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,12 @@ def __post_init__(self):
"case_symlink_realization": "To case/share, with symlinks on realizations level",
"preprocessed": "To share/preprocessed; from interactive runs but re-used later",
}

CONFIG_FIELDS_2_CASE_METADATA = {
"REGIONS": {
"type": dict,
},
"SEISMIC_DATES": {
"type": list,
},
}
25 changes: 25 additions & 0 deletions src/fmu/dataio/dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
ALLOWED_FMU_CONTEXTS,
CONTENTS_REQUIRED,
DEPRECATED_CONTENTS,
CONFIG_FIELDS_2_CASE_METADATA,
)
from ._utils import (
create_symlink,
Expand Down Expand Up @@ -996,6 +997,28 @@ def _check_already_metadata_or_create_folder(self, force=False) -> bool:

return False

def _get_globals_from_globalconfig(self):
cfg_global = dict()

for key in CONFIG_FIELDS_2_CASE_METADATA.keys():
expect_type = CONFIG_FIELDS_2_CASE_METADATA[key]["type"]
block = self.config["global"].get(key)

if block is None:
warn(f"{key} was not found in the global_config.")
continue

if isinstance(block, expect_type):
logger.info("Adding %s to case metadata", key)
cfg_global[key] = self.config["global"][key]
else:
warn(
f"{key} was found in global_config but is not of type "
f"{expect_type} and will be ignored."
)

return cfg_global

# ==================================================================================
# Public methods:
# ==================================================================================
Expand Down Expand Up @@ -1043,6 +1066,8 @@ def generate_metadata(

meta["fmu"] = dict()
meta["fmu"]["model"] = self.config["model"]
meta["fmu"]["config"] = dict()
meta["fmu"]["config"]["global"] = self._get_globals_from_globalconfig()

mcase = meta["fmu"]["case"] = dict()
mcase["name"] = self.casename
Expand Down
60 changes: 57 additions & 3 deletions tests/test_units/test_initialize_case.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
"""Test the dataio running from within RMS interactive as context.
"""Test the InitializeCase class.

In this case a user sits in RMS, which is in folder rms/model and runs
interactive. Hence the basepath will be ../../
This class is used for creating the case metadata.
"""

import logging
import os
from copy import deepcopy

import pytest
import yaml

from fmu.dataio import InitializeCase
from fmu.dataio._utils import prettyprint_dict
from fmu.dataio._definitions import CONFIG_FIELDS_2_CASE_METADATA

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -196,3 +198,55 @@ def test_inicase_deprecated_restart_from(fmurun_w_casemetadata, globalconfig2):
rootfolder=fmurun_w_casemetadata.parent.parent,
restart_from="Jurassic era",
)


def test_inicase_get_globals(globalconfig2):
"""Test the _get_globals_from_globalconfig private method."""

# Verify that fmu-dataio is _told_ to get REGIONS (test assumption)
assert "REGIONS" in CONFIG_FIELDS_2_CASE_METADATA

# verify that fmu-dataio _gets_ REGIONS
icase = InitializeCase(globalconfig2)
global_ = icase._get_globals_from_globalconfig()
assert "REGIONS" in global_
assert isinstance(global_, dict)


def test_inicase_get_globals_with_metadata_generation(globalconfig2, fmurun):
icase = InitializeCase(globalconfig2)
caseroot = fmurun.parent.parent
metadata = icase.generate_case_metadata(rootfolder=caseroot, force=True)

assert "fmu" in metadata
assert "config" in metadata["fmu"]
assert "global" in metadata["fmu"]["config"]

cfg_gl = metadata["fmu"]["config"]["global"] # short form
assert "REGIONS" in cfg_gl
expected_type = CONFIG_FIELDS_2_CASE_METADATA["REGIONS"]["type"]
assert isinstance(cfg_gl["REGIONS"], expected_type)
assert "WestLowland" in cfg_gl["REGIONS"]


def test_inicase_get_globals_missing_block(globalconfig2, fmurun):
"""Test behavior when a wanted block is missing from global_variables."""

globalconfig2_copy = deepcopy(globalconfig2)
del globalconfig2_copy["global"]["REGIONS"]

icase = InitializeCase(globalconfig2_copy)
caseroot = fmurun.parent.parent
with pytest.warns(match="REGIONS was not found"):
metadata = icase.generate_case_metadata(rootfolder=caseroot, force=True)

assert "fmu" in metadata
assert "config" in metadata["fmu"]
assert "global" in metadata["fmu"]["config"]

cfg_gl = metadata["fmu"]["config"]["global"] # short form
assert "REGIONS" not in cfg_gl
assert "SEISMIC_DATES" in cfg_gl

expected_type = CONFIG_FIELDS_2_CASE_METADATA["SEISMIC_DATES"]["type"]
assert isinstance(cfg_gl["SEISMIC_DATES"], expected_type)