Skip to content

Commit

Permalink
Added convert_grid_format script, with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrivenaes authored and berland committed Feb 25, 2020
1 parent 33d5691 commit 03c2847
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 0 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

SSCRIPTS = [
"bjobsusers = subscript.bjobsusers.bjobsusers:main",
"convert_grid_format = subscript.convert_grid_format.convert_grid_format:main",
"csvMergeEnsembles = subscript.csv_merge.csv_merge:main_deprecated",
"csv_merge = subscript.csv_merge.csv_merge:main",
"csvStack = subscript.csv_stack.csv_stack:main",
Expand Down
Empty file.
195 changes: 195 additions & 0 deletions src/subscript/convert_grid_format/convert_grid_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# -*- coding: utf-8 -*-
"""Conversion between grid corner point formats"""

from __future__ import division, print_function, absolute_import

import argparse
import os
import sys

import xtgeo
from xtgeo.common import XTGeoDialog

APPNAME = "convert_grid_format (subscript)"

# allowed CONVERSIONS and MODES:
CONVERSIONS = ["ecl2roff"]
MODES = ["grid", "init", "restart"]

xtg = XTGeoDialog()
logger = xtg.functionlogger(__name__)

try:
from ..version import version
__version__ = version
except ImportError:
__version__ = "0.0.0"


def _do_parse_args(args):

if args is None:
args = sys.argv[1:]
else:
args = args

usetxt = "convert_grid_format ... "

parser = argparse.ArgumentParser(
description="Convert between various 3D grid cornerpoint formats", usage=usetxt
)

parser.add_argument(
"--conversion",
dest="conversion",
type=str,
default="ecl2roff",
help="Conversion method, select from {} "
"(default ecl2roff)".format(CONVERSIONS),
)

parser.add_argument(
"--mode",
dest="mode",
type=str,
default="grid",
help="Mode: {} (default grid)".format(MODES),
)

parser.add_argument(
"--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(
"--propnames",
dest="propnames",
type=str,
help="List of propnames, separate either with \
spaces or colon",
)
parser.add_argument(
"--dates",
dest="dates",
type=str,
help="List of dates, separate either with \
spaces or colon",
)

parser.add_argument(
"--standardfmu",
dest="stdfmu",
action="store_true",
default=False,
help="Use standard fmu name setting of file (no args)",
)

if len(args) < 2:
parser.print_help()
print("QUIT")
sys.exit(0)

args = parser.parse_args(args)
return args


def _convert_ecl2roff(filename, mode, outfile, option, props, dates):
"""Conversion..."""

# pylint: disable=too-many-branches

xtg.say("ecl2roff...")

fname, fext = os.path.splitext(filename)
oname, _oext = os.path.splitext(outfile)

filesep = "_"
if option: # standardfmu separator
filesep = "--"

if mode in ["grid", "init", "restart"]:
logger.info("Running GRID conversion...")

logger.info("Convert from EGRID to ROFF...")
mygrid = xtgeo.grid3d.Grid(fname + ".EGRID", fformat="egrid")

if mode == "grid":
xtg.say("Mode is grid")
outputfile = oname + ".roff"
xtg.say("Output grid to <{}>".format(oname + ".roff"))
mygrid.to_file(outputfile, fformat="roff")
else:
raise SystemExit("STOP! Invalid mode: <{}>".format(mode))

if mode in ("restart", "init"):
xtg.say("Mode is {}".format(mode))
logger.info("Running %s conversion...", mode.upper())
fname, fext = os.path.splitext(filename)

if not props:
raise SystemExit("STOP. No properties given")

if ":" in props:
props = props.split(":")
else:
props = props.split()

fformat = mode
fformat = fformat.replace("restart", "unrst")
if mode == "restart":
if dates is None:
raise SystemExit("STOP. No dates given")

if ":" in dates:
dates = dates.split(":")
else:
dates = dates.split()
else:
dates = None

if fext in (".UNRST", ".INIT", ""):

myprops = xtgeo.grid3d.GridProperties()

usext = ".{}".format(fformat).upper()

myprops.from_file(
fname + usext, names=props, dates=dates, fformat=fformat, grid=mygrid
)

for prop in myprops.props:
pname = prop.name.lower().replace("_", filesep)
outputfile = oname + filesep + pname + ".roff"
xtg.say("Output grid property to <{}>".format(outputfile))
prop.to_file(outputfile, fformat="roff")
else:
raise SystemExit("Invalid grid extions")


def main(args=None):

XTGeoDialog.print_xtgeo_header(APPNAME, __version__)

args = _do_parse_args(args)

logger.info(args)

if args.conversion not in set(CONVERSIONS):
logger.critical("ERROR")
SystemExit(
"Illegal conversion <{}>. Allowed are: {}".format(
args.conversion, CONVERSIONS
)
)

xtg.say("Running conversion...")
_convert_ecl2roff(
args.infile, args.mode, args.outfile, args.stdfmu, args.propnames, args.dates
)


if __name__ == "__main__":
main()
61 changes: 61 additions & 0 deletions tests/test_convert_grid_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""Test the convert_grid_format script"""

import os
import shutil
import pytest

import xtgeo
from xtgeo.common import XTGeoDialog

import subscript.convert_grid_format.convert_grid_format as cgf

xtg = XTGeoDialog()
logger = xtg.basiclogger(__name__)


RFILE1 = "tests/data/reek/eclipse/model/2_R001_REEK-0.EGRID"
RFILE2 = "tests/data/reek/eclipse/model/2_R001_REEK-0.UNRST"


def test_convert_grid_format_egrid(tmpdir):
"""Convert an ECLIPSE egrid to roff"""

outfile = os.path.join(tmpdir, "reek_grid.roff")

cgf.main(["--file", RFILE1, "--output", outfile, "--mode", "grid", "--standardfmu"])

# check number of active cells
gg = xtgeo.Grid(outfile)
assert gg.nactive == 35817

shutil.rmtree(tmpdir)


def test_convert_grid_format_restart(tmpdir):
"""Convert an ECLIPSE SOIL from restart to roff"""

outfile = os.path.join(tmpdir, "reek_grid.roff")

cgf.main(
[
"--file",
RFILE2,
"--output",
outfile,
"--mode",
"restart",
"--propnames",
"SOIL",
"--dates",
"20000701",
"--standardfmu",
]
)

actual_outfile = os.path.join(tmpdir, "reek_grid--soil--20000701.roff")

gprop = xtgeo.GridProperty(actual_outfile)

assert gprop.values.mean() == pytest.approx(0.0857, abs=0.001)

shutil.rmtree(tmpdir)

0 comments on commit 03c2847

Please sign in to comment.