Skip to content

Commit

Permalink
CLN: Add some typing to GridProperty
Browse files Browse the repository at this point in the history
Touched on the `scan_keywords` method while working looking through
`_grid3d_utils.scan_keywords` invocations.
  • Loading branch information
mferrera committed Nov 1, 2023
1 parent 734b173 commit 01ce380
Showing 1 changed file with 46 additions and 25 deletions.
71 changes: 46 additions & 25 deletions src/xtgeo/grid3d/grid_properties.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# -*- coding: utf-8 -*-

"""Module for Grid Properties."""
from __future__ import annotations

import hashlib
import io
import warnings
from typing import List, Optional
from typing import TYPE_CHECKING, List, Literal, Optional, Tuple, Union

import deprecation
import numpy as np
Expand All @@ -18,9 +19,18 @@
from ._grid3d import _Grid3D
from .grid_property import GridProperty

if TYPE_CHECKING:
import pathlib

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

KeywordTuple = Tuple[str, str, int, int]
KeywordDateTuple = Tuple[str, str, int, int, Union[str, int]]
GridPropertiesKeywords = Union[
List[Union[KeywordTuple, KeywordDateTuple]], pd.DataFrame
]


def gridproperties_from_file(
pfile,
Expand Down Expand Up @@ -291,7 +301,6 @@ def __init__(
# compatability until the names setter has been
# deprecated
self._names = []

self.props = props or []

def __repr__(self): # noqa: D105
Expand Down Expand Up @@ -724,44 +733,56 @@ def _consistency_check(self):

@staticmethod
def scan_keywords(
pfile, fformat="xecl", maxkeys=MAXKEYWORDS, dataframe=False, dates=False
):
pfile: Union[str, pathlib.Path, io.BytesIO, io.StringIO],
fformat: Literal["roff", "xecl"] = "xecl",
maxkeys: int = MAXKEYWORDS,
dataframe: bool = False,
dates: bool = False,
) -> GridPropertiesKeywords:
"""Quick scan of keywords in Eclipse binary files, or ROFF binary files.
For Eclipse files:
Returns a list of tuples (or dataframe), e.g. ('PRESSURE',
'REAL', 355299, 3582700), where (keyword, type, no_of_values,
byteposition_in_file)
Returns a list of tuples (or dataframe), e.g.
``('PRESSURE', 'REAL', 355299, 3582700)``
where
``(keyword, type, no_of_values, byteposition_in_file)``
For ROFF files
Returns a list of tuples (or dataframe), e.g.
('translate!xoffset', 'float', 1, 3582700),
where (keyword, type, no_of_values, byteposition_in_file).
For ROFF files:
Returns a list of tuples (or dataframe), e.g.
``('translate!xoffset', 'float', 1, 3582700)``
where
``(keyword, type, no_of_values, byteposition_in_file)``
For Eclipse, the byteposition is to the KEYWORD, while for ROFF
the byte position is to the beginning of the actual data.
Args:
pfile (str): Name or a filehandle to file with properties
fformat (str): xecl (Eclipse INIT, RESTART, ...) or roff for
ROFF binary,
maxkeys (int): Maximum number of keys
dataframe (bool): If True, return a Pandas dataframe instead
dates (bool): if True, the date is the last column (only
menaingful for restart files). Default is False.
Parameters:
pfile:
Name or a filehandle to file with properties.
fformat:
xecl (Eclipse INIT, RESTART, ...) or roff for ROFF binary.
Default is "xecl".
maxkeys:
Maximum number of keys. Default is
``xtgeo.commom.constants.MAXKEYWORDS``.
dataframe:
If True, return a Pandas dataframe instead. Default is False.
dates:
If True, the date is the last column (only
meaningful for restart files). Default is False.
Return:
Returns:
A list of tuples or dataframe with keyword info
Example::
>>> dlist = GridProperties.scan_keywords(reek_dir + "/REEK.UNRST")
"""
pfile = xtgeo._XTGeoFile(pfile)
pfile.check_file(raiseerror=ValueError)
xtg_file = xtgeo._XTGeoFile(pfile)
xtg_file.check_file(raiseerror=ValueError)

return utils.scan_keywords(
pfile,
xtg_file,
fformat=fformat,
maxkeys=maxkeys,
dataframe=dataframe,
Expand Down

0 comments on commit 01ce380

Please sign in to comment.