Skip to content

Commit

Permalink
CLN: Add types to grid3d_fence (equinor#1052)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnatt authored Nov 30, 2023
1 parent fcbff40 commit 55b799e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 51 deletions.
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ ignore_errors = True
[mypy-xtgeo.grid3d._grdecl_grid]
ignore_errors = True

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

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

Expand Down
37 changes: 22 additions & 15 deletions src/xtgeo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,29 @@ def _xprint(msg):

_xprint("Import various XTGeo modules... wells...")

from xtgeo.grid3d import GridRelative, Units, grid, grid_properties, grid_property
from xtgeo.grid3d.grid import Grid
from xtgeo.grid3d.grid_properties import (
GridProperties,
gridproperties_dataframe,
gridproperties_from_file,
)
from xtgeo.grid3d.grid_property import (
GridProperty,
gridproperty_from_file,
gridproperty_from_roxar,
)

_xprint("Import various XTGeo modules... 3D grids...")

from xtgeo.surface import regular_surface
from xtgeo.surface.regular_surface import RegularSurface
from xtgeo.surface.regular_surface import (
RegularSurface,
surface_from_cube,
surface_from_file,
surface_from_grid3d,
surface_from_roxar,
)
from xtgeo.surface.surfaces import Surfaces

_xprint("Import various XTGeo modules... surface...")
Expand All @@ -103,12 +124,6 @@ def _xprint(msg):

_xprint("Import various XTGeo modules... cube...")

from xtgeo.grid3d import GridRelative, Units, grid, grid_properties, grid_property
from xtgeo.grid3d.grid import Grid
from xtgeo.grid3d.grid_properties import GridProperties, gridproperties_dataframe
from xtgeo.grid3d.grid_property import GridProperty

_xprint("Import various XTGeo modules... 3D grids...")

from xtgeo.metadata.metadata import (
MetaDataCPGeometry,
Expand All @@ -134,14 +149,6 @@ def _xprint(msg):
grid_from_file,
grid_from_roxar,
)
from xtgeo.grid3d.grid_properties import gridproperties_from_file
from xtgeo.grid3d.grid_property import gridproperty_from_file, gridproperty_from_roxar
from xtgeo.surface.regular_surface import (
surface_from_cube,
surface_from_file,
surface_from_grid3d,
surface_from_roxar,
)
from xtgeo.well.blocked_well import blockedwell_from_file, blockedwell_from_roxar
from xtgeo.well.blocked_wells import blockedwells_from_files, blockedwells_from_roxar
from xtgeo.well.well1 import well_from_file, well_from_roxar
Expand Down
76 changes: 44 additions & 32 deletions src/xtgeo/grid3d/_grid3d_fence.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
# -*- coding: utf-8 -*-

"""Some grid utilities, file scanning etc."""
from __future__ import annotations

from typing import TYPE_CHECKING

import numpy as np

import xtgeo
from xtgeo import _cxtgeo
from xtgeo import _cxtgeo # type: ignore
from xtgeo.common import null_logger
from xtgeo.common.constants import UNDEF_LIMIT
from xtgeo.grid3d import _gridprop_lowlevel as gl
from xtgeo.surface import _regsurf_lowlevel as rl
from xtgeo.surface.regular_surface import surface_from_grid3d
from xtgeo.xyz import Polygons

if TYPE_CHECKING:
from xtgeo.grid3d import Grid, GridProperty


logger = null_logger(__name__)


def get_randomline(
self,
fencespec,
prop,
zmin=None,
zmax=None,
zincrement=1.0,
hincrement=None,
atleast=5,
nextend=2,
):
self: Grid,
fencespec: np.ndarray | Polygons,
prop: str | GridProperty,
zmin: float | int | None = None,
zmax: float | int | None = None,
zincrement: float | int = 1.0,
hincrement: float | int | None = None,
atleast: int = 5,
nextend: int = 2,
) -> tuple[float, float, float, float, np.ndarray]:
"""Extract a randomline from a 3D grid.
This is a difficult task, in particular in terms of acceptable speed.
Expand All @@ -32,15 +42,19 @@ def get_randomline(

_update_tmpvars(self)

if hincrement is None and isinstance(fencespec, xtgeo.Polygons):
if hincrement is None and isinstance(fencespec, Polygons):
logger.info("Estimate hincrement from Polygons instance...")
fencespec = _get_randomline_fence(self, fencespec, hincrement, atleast, nextend)
logger.info("Estimate hincrement from Polygons instance... DONE")

logger.info("Get property...")
if isinstance(prop, str):
prop = self.get_prop_by_name(prop)
grid_prop = self.get_prop_by_name(prop)
if grid_prop is None:
raise ValueError(f"No property with name {prop} was found in grid")
prop = grid_prop

assert isinstance(fencespec, np.ndarray)
xcoords = fencespec[:, 0]
ycoords = fencespec[:, 1]
hcoords = fencespec[:, 3]
Expand Down Expand Up @@ -87,14 +101,14 @@ def get_randomline(

logger.info("Running C routine to get randomline... DONE")

values[values > xtgeo.UNDEF_LIMIT] = np.nan
values[values > UNDEF_LIMIT] = np.nan
arr = values.reshape((xcoords.shape[0], nzsam)).T

logger.info("Getting randomline... DONE")
return (hcoords[0], hcoords[-1], zmin, zmax, arr)


def _update_tmpvars(self, force=False):
def _update_tmpvars(self: Grid, force: bool = False) -> None:
"""The self._tmp variables are needed to speed up calculations.
If they are already created, the no need to recreate
Expand All @@ -106,24 +120,16 @@ def _update_tmpvars(self, force=False):
one = self._tmp["onegrid"]
logger.info("Make a tmp onegrid instance... DONE")
logger.info("Make a set of tmp surfaces for I J locations + depth...")
self._tmp["topd"] = xtgeo.surface_from_grid3d(
self._tmp["topd"] = surface_from_grid3d(
one, where="top", mode="depth", rfactor=4
)
self._tmp["topi"] = xtgeo.surface_from_grid3d(
one, where="top", mode="i", rfactor=4
)
self._tmp["topj"] = xtgeo.surface_from_grid3d(
one, where="top", mode="j", rfactor=4
)
self._tmp["basd"] = xtgeo.surface_from_grid3d(
self._tmp["topi"] = surface_from_grid3d(one, where="top", mode="i", rfactor=4)
self._tmp["topj"] = surface_from_grid3d(one, where="top", mode="j", rfactor=4)
self._tmp["basd"] = surface_from_grid3d(
one, where="base", mode="depth", rfactor=4
)
self._tmp["basi"] = xtgeo.surface_from_grid3d(
one, where="base", mode="i", rfactor=4
)
self._tmp["basj"] = xtgeo.surface_from_grid3d(
one, where="base", mode="j", rfactor=4
)
self._tmp["basi"] = surface_from_grid3d(one, where="base", mode="i", rfactor=4)
self._tmp["basj"] = surface_from_grid3d(one, where="base", mode="j", rfactor=4)

self._tmp["topi"].fill()
self._tmp["topj"].fill()
Expand All @@ -140,7 +146,13 @@ def _update_tmpvars(self, force=False):
logger.info("Re-use existing onegrid and tmp surfaces for I J")


def _get_randomline_fence(self, fencespec, hincrement, atleast, nextend):
def _get_randomline_fence(
self: Grid,
polygon: Polygons,
hincrement: float | int | None,
atleast: int,
nextend: int,
) -> np.ndarray:
"""Compute a resampled fence from a Polygons instance."""
if hincrement is None:
geom = self.get_geometrics()
Expand All @@ -151,7 +163,7 @@ def _get_randomline_fence(self, fencespec, hincrement, atleast, nextend):
distance = hincrement

logger.info("Getting fence from a Polygons instance...")
fspec = fencespec.get_fence(
fspec = polygon.get_fence(
distance=distance, atleast=atleast, nextend=nextend, asnumpy=True
)
logger.info("Getting fence from a Polygons instance... DONE")
Expand Down
2 changes: 1 addition & 1 deletion src/xtgeo/grid3d/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2654,7 +2654,7 @@ def get_randomline(
hincrement: float | bool | None = None,
atleast: int = 5,
nextend: int = 2,
) -> tuple[int, int, int, int, np.ndarray]:
) -> tuple[float, float, float, float, np.ndarray]:
"""Get a sampled randomline from a fence spesification.
This randomline will be a 2D numpy with depth on the vertical
Expand Down

0 comments on commit 55b799e

Please sign in to comment.