Skip to content

Commit

Permalink
CLN: Add types grid-import
Browse files Browse the repository at this point in the history
  • Loading branch information
JB Lovland authored and janbjorge committed Dec 4, 2023
1 parent acd4a2a commit 3030fb9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ ignore_errors = True
[mypy-xtgeo.grid3d._grid_export]
ignore_errors = True

[mypy-xtgeo.grid3d._grid_import]
ignore_errors = True

[mypy-xtgeo.grid3d._grid_import_ecl]
ignore_errors = True

Expand Down
55 changes: 39 additions & 16 deletions src/xtgeo/grid3d/_grid_import.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,77 @@
"""Grid import functions for various formats."""
from __future__ import annotations

from pathlib import Path
from typing import Any, Literal

import xtgeo
from xtgeo.common import null_logger
from xtgeo.common.sys import _XTGeoFile
from xtgeo.grid3d import _grid_import_ecl, _grid_import_roff

from . import _grid_import_xtgcpgeom

logger = null_logger(__name__)


def from_file(gfile, fformat=None, **kwargs):
def from_file(
gfile: _XTGeoFile,
fformat: Literal[
"bgrdecl",
"egrid",
"fegrid",
"grdecl",
"guess",
"hdf",
"roff_ascii",
"roff_binary",
"xtg",
]
| None = None,
**kwargs: Any,
) -> dict[str, Any]:
"""Import grid geometry from file, and makes an instance of this class.
Returns:
dictionary of keyword arguments to be used in Grid constructor.
"""
if not isinstance(gfile, xtgeo._XTGeoFile):
if not isinstance(gfile, _XTGeoFile):
raise RuntimeError("Error gfile must be a _XTGeoFile instance")

result = {}
result: dict[str, Any] = dict()

result["filesrc"] = gfile.name

if fformat is None or fformat == "guess":
fformat = gfile.detect_fformat()
else:
fformat = gfile.generic_format_by_proposal(fformat) # default
_fformat = (
gfile.detect_fformat()
if fformat is None or fformat == "guess"
else gfile.generic_format_by_proposal(fformat) # default
)

gfile.check_file(raiseerror=IOError, raisetext=f"Cannot access file {gfile.name}")

if fformat in ["roff_binary", "roff_ascii"]:
if _fformat in ["roff_binary", "roff_ascii"]:
result.update(_grid_import_roff.import_roff(gfile, **kwargs))
elif fformat in ["egrid", "fegrid"]:
elif _fformat in ["egrid", "fegrid"]:
result.update(
_grid_import_ecl.import_ecl_egrid(gfile, fileformat=fformat, **kwargs)
_grid_import_ecl.import_ecl_egrid(gfile, fileformat=_fformat, **kwargs)
)
elif fformat == "grdecl":
elif _fformat == "grdecl":
result.update(_grid_import_ecl.import_ecl_grdecl(gfile, **kwargs))
elif fformat == "bgrdecl":
elif _fformat == "bgrdecl":
result.update(_grid_import_ecl.import_ecl_bgrdecl(gfile, **kwargs))
elif fformat == "xtg":
elif _fformat == "xtg":
result.update(_grid_import_xtgcpgeom.import_xtgcpgeom(gfile, **kwargs))
elif fformat == "hdf":
elif _fformat == "hdf":
result.update(_grid_import_xtgcpgeom.import_hdf5_cpgeom(gfile, **kwargs))
else:
raise ValueError(f"Invalid file format: {fformat}")
raise ValueError(f"Invalid file format: {_fformat}")

if gfile.memstream:
result["name"] = "unknown"
else:
# Mypy does not know that if gfile.memstream -> False
# then .file must be Path.
assert isinstance(gfile.file, Path)
result["name"] = gfile.file.stem

return result

0 comments on commit 3030fb9

Please sign in to comment.