Skip to content
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
4 changes: 2 additions & 2 deletions docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ welltest_dpds ✅ ✅ ⛔️
======================== === ================= ============

.. [*] ``convert_grid_format`` is the script that contains functionality
for the ``ECLGRID2ROFF``, ``ECLINIT2ROFF``, and ``ECLRST2ROFF`` forward
models.
for the ``ECLGRID2ROFF``, ``ECLINIT2ROFF``, ``ECLRST2ROFF``,
and ``ROFF2ECLGRID`` forward models.
84 changes: 73 additions & 11 deletions src/subscript/convert_grid_format/convert_grid_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
APPNAME = "convert_grid_format (subscript)"

# allowed CONVERSIONS and MODES:
CONVERSIONS = ["ecl2roff"]
CONVERSIONS = ["ecl2roff", "roff2ecl"]
MODES = ["grid", "init", "restart"]
ECL_FORMATS = {"grdecl", "bgrdecl", "egrid"}

xtg = XTGeoDialog()

Expand Down Expand Up @@ -47,16 +48,30 @@ def get_parser() -> argparse.ArgumentParser:
dest="mode",
type=str,
default="grid",
help=f"Mode: {MODES} (default grid)",
help=f"Mode: {MODES} (default grid). roff2ecl supports grid only.",
)

parser.add_argument(
"--file", dest="infile", type=str, help="Input file name (full name or root)"
"--file",
dest="infile",
type=str,
help="Input file name (full name or root)",
)

parser.add_argument(
"--output", dest="outfile", type=str, help="Output file name (full name)"
)
parser.add_argument(
"--outformat",
dest="outformat",
type=str,
default="auto",
help=(
"Output Eclipse format for roff2ecl: "
f"{sorted(ECL_FORMATS)} or auto (infer from --output extension, "
"fallback to grdecl)"
),
)

parser.add_argument(
"--propnames",
Expand Down Expand Up @@ -181,6 +196,45 @@ def _convert_ecl2roff(
raise SystemExit("Invalid grid extention")


def _resolve_ecl_format(outfile: str, outformat: str) -> str:
format_name = outformat.lower()
if format_name != "auto":
if format_name not in ECL_FORMATS:
raise SystemExit(
f"Invalid outformat <{outformat}>. Allowed: {ECL_FORMATS} or auto"
)
return format_name

_name, ext = os.path.splitext(outfile)
ext_map = {".grdecl": "grdecl", ".bgrdecl": "bgrdecl", ".egrid": "egrid"}
inferred = ext_map.get(ext.lower())
if inferred:
return inferred

logger.info(
"outformat=auto could not infer format from extension <%s>; defaulting to "
"grdecl",
ext or "<none>",
)
return "grdecl"


def _convert_roff2ecl(
filename: str,
mode: str,
outfile: str,
outformat: str,
) -> None:
if not filename or not outfile:
raise SystemExit("STOP. Both --file and --output are required")
if mode != "grid":
raise SystemExit(f"STOP! Invalid mode for roff2ecl: <{mode}>")

ecl_format = _resolve_ecl_format(outfile, outformat)
grid = xtgeo.grid_from_file(filename, fformat="roff")
grid.to_file(outfile, fformat=ecl_format)


def main(args: Sequence[str] | None = None) -> None:
"""Entry-point"""

Expand All @@ -196,14 +250,22 @@ def main(args: Sequence[str] | None = None) -> None:
)

xtg.say("Running conversion...")
_convert_ecl2roff(
parsed_args.infile,
parsed_args.mode,
parsed_args.outfile,
parsed_args.stdfmu,
parsed_args.propnames,
parsed_args.dates,
)
if parsed_args.conversion == "ecl2roff":
_convert_ecl2roff(
parsed_args.infile,
parsed_args.mode,
parsed_args.outfile,
parsed_args.stdfmu,
parsed_args.propnames,
parsed_args.dates,
)
else:
_convert_roff2ecl(
parsed_args.infile,
parsed_args.mode,
parsed_args.outfile,
parsed_args.outformat,
)


if __name__ == "__main__":
Expand Down
45 changes: 45 additions & 0 deletions src/subscript/hook_implementations/forward_model_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,50 @@ def documentation() -> ForwardModelStepDocumentation | None:
)


class Roff2EclGrid(ForwardModelStepPlugin):
def __init__(self) -> None:
super().__init__(
name="ROFF2ECLGRID",
command=[
shutil.which("convert_grid_format"),
"--conversion",
"roff2ecl",
"--file",
"<INPUT>",
"--output",
"<OUTPUT>",
"--outformat",
"<OUTFORMAT>",
"--mode",
"grid",
],
default_mapping={"<OUTFORMAT>": "auto"},
)

@staticmethod
def documentation() -> ForwardModelStepDocumentation | None:
return ForwardModelStepDocumentation(
description="""Convert a ROFF grid geometry file to Eclipse format.

This forward model uses the script ``convert_grid_format`` from subscript.

Supported output formats are ``grdecl``, ``bgrdecl`` and ``egrid``.
Destination directory must exist.

``<OUTFORMAT>`` is optional and defaults to ``auto`` (infer from output
extension). If the extension is missing or not recognized, it defaults to
``grdecl``.
""",
category="utility.eclipse",
examples="""
.. code-block:: console

FORWARD_MODEL ROFF2ECLGRID(<INPUT>=share/results/grids/reek.roff, \
<OUTPUT>=share/results/grids/reek.EGRID, <OUTFORMAT>=egrid)
""",
)


class GravSubsMaps(ForwardModelStepPlugin):
def __init__(self) -> None:
super().__init__(
Expand Down Expand Up @@ -861,6 +905,7 @@ def installable_forward_model_steps() -> list[type[ForwardModelStepPlugin]]:
Eclgrid2Roff,
Eclinit2Roff,
Eclrst2Roff,
Roff2EclGrid,
GravSubsMaps,
GravSubsPoints,
InterpRelperm,
Expand Down
Loading