Skip to content

Commit

Permalink
MAINT: Port forward_models to new ERT Plugin system
Browse files Browse the repository at this point in the history
  • Loading branch information
tnatt committed Sep 3, 2024
1 parent f31e9c5 commit 6e2d7c9
Show file tree
Hide file tree
Showing 15 changed files with 262 additions and 160 deletions.
6 changes: 0 additions & 6 deletions src/grid3d_maps/aggregate/grid3d_aggregate_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@
"Aggregate property maps from 3D grids. Docs:\n"
+ "https://fmu-docs.equinor.com/docs/grid3d-maps/"
)
CATEGORY = "modelling.reservoir"
EXAMPLES = """
.. code-block:: console
FORWARD_MODEL GRID3D_AGGREGATE_MAP(<CONFIG_AGGREGATE>=conf.yml, <ECLROOT>=<ECLBASE>)
"""


def write_map(x_nodes, y_nodes, map_, filename):
Expand Down
6 changes: 0 additions & 6 deletions src/grid3d_maps/aggregate/grid3d_migration_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
"Generate migration time property maps. Docs:\n"
+ "https://fmu-docs.equinor.com/docs/grid3d-maps/"
)
CATEGORY = "modelling.reservoir"
EXAMPLES = """
.. code-block:: console
FORWARD_MODEL GRID3D_MIGRATION_TIME(<CONFIG_MIGTIME>=conf.yml, <ECLROOT>=<ECLBASE>)
"""


def calculate_migration_time_property(
Expand Down
14 changes: 3 additions & 11 deletions src/grid3d_maps/avghc/grid3d_average_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,20 @@

APPNAME = "grid3d_average_map"

APPDESCR = (
# Module variables for ERT hook implementation:
DESCRIPTION = (
"Make average property maps directly from 3D grids. Docs:\n"
+ "https://fmu-docs.equinor.com/docs/grid3d-maps/"
)

# Module variables for ERT hook implementation:
DESCRIPTION = APPDESCR
CATEGORY = "modelling.reservoir"
EXAMPLES = """
.. code-block:: console
FORWARD_MODEL GRID3D_AVERAGE_MAP(<CONFIG_AVGMAP>=conf.yml, <ECLROOT>=<ECLBASE>)
"""

xtg = XTGeoDialog()

logger = xtg.basiclogger(__name__)


def do_parse_args(args):
"""Parse command line arguments that will override config."""
return _configparser.parse_args(args, APPNAME, APPDESCR)
return _configparser.parse_args(args, APPNAME, DESCRIPTION)


def yamlconfig(inputfile, args):
Expand Down
14 changes: 3 additions & 11 deletions src/grid3d_maps/avghc/grid3d_hc_thickness.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,19 @@

APPNAME = "grid3d_hc_thickness"

APPDESCR = (
# Module variables for ERT hook implementation:
DESCRIPTION = (
"Make HC thickness maps directly from 3D grids. Docs:\n"
+ "https://fmu-docs.equinor.com/docs/grid3d-maps/"
)

# Module variables for ERT hook implementation:
DESCRIPTION = APPDESCR
CATEGORY = "modelling.reservoir"
EXAMPLES = """
.. code-block:: console
FORWARD_MODEL GRID3D_HC_THICKNESS(<CONFIG_HCMAP>=conf.yml, <ECLROOT>=<ECLBASE>)
"""

xtg = XTGeoDialog()

logger = xtg.basiclogger(__name__)


def do_parse_args(args):
return _configparser.parse_args(args, APPNAME, APPDESCR)
return _configparser.parse_args(args, APPNAME, DESCRIPTION)


def yamlconfig(inputfile, args):
Expand Down
5 changes: 0 additions & 5 deletions src/grid3d_maps/config_jobs/GRID3D_AGGREGATE_MAP

This file was deleted.

5 changes: 0 additions & 5 deletions src/grid3d_maps/config_jobs/GRID3D_AVERAGE_MAP

This file was deleted.

5 changes: 0 additions & 5 deletions src/grid3d_maps/config_jobs/GRID3D_HC_THICKNESS

This file was deleted.

5 changes: 0 additions & 5 deletions src/grid3d_maps/config_jobs/GRID3D_MIGRATION_TIME

This file was deleted.

13 changes: 13 additions & 0 deletions src/grid3d_maps/forward_models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from __future__ import annotations

from .grid3d_aggregate_map import Grid3dAggregateMap
from .grid3d_average_map import Grid3dAverageMap
from .grid3d_hc_thickness import Grid3dHcThickness
from .grid3d_migration_time import Grid3dMigrationTime

__all__ = [
"Grid3dAggregateMap",
"Grid3dAverageMap",
"Grid3dHcThickness",
"Grid3dMigrationTime",
]
45 changes: 45 additions & 0 deletions src/grid3d_maps/forward_models/grid3d_aggregate_map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from __future__ import annotations

from ert import (
ForwardModelStepDocumentation,
ForwardModelStepJSON,
ForwardModelStepPlugin,
)

from grid3d_maps.aggregate.grid3d_aggregate_map import DESCRIPTION


class Grid3dAggregateMap(ForwardModelStepPlugin):
def __init__(self) -> None:
super().__init__(
name="GRID3D_AGGREGATE_MAP",
command=[
"grid3d_aggregate_map",
"--config",
"<CONFIG_AGGREGATE>",
"--eclroot",
"<ECLROOT>",
],
)

def validate_pre_realization_run(
self, fm_step_json: ForwardModelStepJSON
) -> ForwardModelStepJSON:
return fm_step_json

def validate_pre_experiment(self, fm_step_json: ForwardModelStepJSON) -> None:
return fm_step_json

@staticmethod
def documentation() -> ForwardModelStepDocumentation | None:
return ForwardModelStepDocumentation(
category="modelling.reservoir",
source_package="grid3d_maps",
source_function_name="Grid3dAggregateMap",
description=DESCRIPTION,
examples="""
.. code-block:: console
FORWARD_MODEL GRID3D_AGGREGATE_MAP(<CONFIG_AGGREGATE>=conf.yml, <ECLROOT>=<ECLBASE>)
""",
)
45 changes: 45 additions & 0 deletions src/grid3d_maps/forward_models/grid3d_average_map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from __future__ import annotations

from ert import (
ForwardModelStepDocumentation,
ForwardModelStepJSON,
ForwardModelStepPlugin,
)

from grid3d_maps.avghc.grid3d_average_map import DESCRIPTION


class Grid3dAverageMap(ForwardModelStepPlugin):
def __init__(self) -> None:
super().__init__(
name="GRID3D_AVERAGE_MAP",
command=[
"grid3d_average_map",
"--config",
"<CONFIG_AVGMAP>",
"--eclroot",
"<ECLROOT>",
],
)

def validate_pre_realization_run(
self, fm_step_json: ForwardModelStepJSON
) -> ForwardModelStepJSON:
return fm_step_json

def validate_pre_experiment(self, fm_step_json: ForwardModelStepJSON) -> None:
return fm_step_json

@staticmethod
def documentation() -> ForwardModelStepDocumentation | None:
return ForwardModelStepDocumentation(
category="modelling.reservoir",
source_package="grid3d_maps",
source_function_name="Grid3dAverageMap",
description=DESCRIPTION,
examples="""
.. code-block:: console
FORWARD_MODEL GRID3D_AVERAGE_MAP(<CONFIG_AVGMAP>=conf.yml, <ECLROOT>=<ECLBASE>)
""",
)
45 changes: 45 additions & 0 deletions src/grid3d_maps/forward_models/grid3d_hc_thickness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from __future__ import annotations

from ert import (
ForwardModelStepDocumentation,
ForwardModelStepJSON,
ForwardModelStepPlugin,
)

from grid3d_maps.avghc.grid3d_hc_thickness import DESCRIPTION


class Grid3dHcThickness(ForwardModelStepPlugin):
def __init__(self) -> None:
super().__init__(
name="GRID3D_HC_THICKNESS",
command=[
"grid3d_hc_thickness",
"--config",
"<CONFIG_HCMAP>",
"--eclroot",
"<ECLROOT>",
],
)

def validate_pre_realization_run(
self, fm_step_json: ForwardModelStepJSON
) -> ForwardModelStepJSON:
return fm_step_json

def validate_pre_experiment(self, fm_step_json: ForwardModelStepJSON) -> None:
return fm_step_json

@staticmethod
def documentation() -> ForwardModelStepDocumentation | None:
return ForwardModelStepDocumentation(
category="modelling.reservoir",
source_package="grid3d_maps",
source_function_name="Grid3dHcThickness",
description=DESCRIPTION,
examples="""
.. code-block:: console
FORWARD_MODEL GRID3D_HC_THICKNESS(<CONFIG_HCMAP>=conf.yml, <ECLROOT>=<ECLBASE>)
""",
)
45 changes: 45 additions & 0 deletions src/grid3d_maps/forward_models/grid3d_migration_time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from __future__ import annotations

from ert import (
ForwardModelStepDocumentation,
ForwardModelStepJSON,
ForwardModelStepPlugin,
)

from grid3d_maps.aggregate.grid3d_migration_time import DESCRIPTION


class Grid3dMigrationTime(ForwardModelStepPlugin):
def __init__(self) -> None:
super().__init__(
name="GRID3D_MIGRATION_TIME",
command=[
"grid3d_migration_time",
"--config",
"<CONFIG_MIGTIME>",
"--eclroot",
"<ECLROOT>",
],
)

def validate_pre_realization_run(
self, fm_step_json: ForwardModelStepJSON
) -> ForwardModelStepJSON:
return fm_step_json

def validate_pre_experiment(self, fm_step_json: ForwardModelStepJSON) -> None:
return fm_step_json

@staticmethod
def documentation() -> ForwardModelStepDocumentation | None:
return ForwardModelStepDocumentation(
category="modelling.reservoir",
source_package="grid3d_maps",
source_function_name="Grid3dMigrationTime",
description=DESCRIPTION,
examples="""
.. code-block:: console
FORWARD_MODEL GRID3D_MIGRATION_TIME(<CONFIG_MIGTIME>=conf.yml, <ECLROOT>=<ECLBASE>)
""",
)
75 changes: 19 additions & 56 deletions src/grid3d_maps/hook_implementations/jobs.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,27 @@
import importlib
import os
import sys
from pathlib import Path
from __future__ import annotations

try:
from ert.shared.plugins.plugin_manager import hook_implementation
from ert.shared.plugins.plugin_response import plugin_response
except ModuleNotFoundError:
from ert_shared.plugins.plugin_manager import hook_implementation
from ert_shared.plugins.plugin_response import plugin_response

PLUGIN_NAME = "grid3d_maps"


def _get_jobs_from_directory(directory):
resource_directory = Path(sys.modules[PLUGIN_NAME].__file__).parent / directory

all_files = [
os.path.join(resource_directory, f)
for f in os.listdir(resource_directory)
if os.path.isfile(os.path.join(resource_directory, f))
]
return {os.path.basename(path): path for path in all_files}
import ert

from grid3d_maps.forward_models import (
Grid3dAggregateMap,
Grid3dAverageMap,
Grid3dHcThickness,
Grid3dMigrationTime,
)

@hook_implementation
@plugin_response(plugin_name=PLUGIN_NAME)
def installable_jobs():
return _get_jobs_from_directory("config_jobs")
PLUGIN_NAME = "grid3d_maps"


@hook_implementation
@plugin_response(plugin_name=PLUGIN_NAME)
def installable_workflow_jobs():
@ert.plugin(name=PLUGIN_NAME)
def installable_workflow_jobs() -> dict[str, str]:
return {}


def _get_module_if_exists(module_name):
try:
script_module = importlib.import_module(module_name)
except ImportError:
return None
return script_module


@hook_implementation
@plugin_response(plugin_name=PLUGIN_NAME)
def job_documentation(job_name):
subscript_jobs = set(installable_jobs().data.keys())
if job_name not in subscript_jobs:
return None

for sm in ["avghc", "aggregate"]:
module_name = f"{PLUGIN_NAME}.{sm}.{job_name.lower()}"
module = _get_module_if_exists(module_name)
if module is not None:
print(module_name)
return {
"description": getattr(module, "DESCRIPTION", ""),
"examples": getattr(module, "EXAMPLES", ""),
"category": getattr(module, "CATEGORY", "other"),
}
return None
@ert.plugin(name=PLUGIN_NAME)
def installable_forward_model_steps() -> list[ert.ForwardModelStepPlugin]:
return [
Grid3dHcThickness,
Grid3dAggregateMap,
Grid3dAverageMap,
Grid3dMigrationTime,
]
Loading

0 comments on commit 6e2d7c9

Please sign in to comment.