#13861 Python: Add surfaces() and surface_by_name() to the rips API

Project.surfaces(surface_type) returns every surface in the project,
SurfaceCollection.surfaces(surface_type, recursive) the surfaces of one
folder, and Project.surface_by_name(name, surface_type, folder) looks a
surface up by its user description. Subclasses of the requested type are
included; ensemble surfaces are skipped unless include_ensembles is set.

Project.surface_folder(name) now returns None when the folder does not
exist, as PythonExamples/surfaces_and_visualization/surface_import.py
already expects, instead of failing with a gRPC error.
This commit is contained in:
Stefan Crooijmans authored and Kristian Bendiksen committed 2026-11-04 10:45:24 +01:00
1 parent 3cc11eeae8
commit bcae2ccbcb
4 files changed
+204 -4

No files matched your search

+118 -2
View File
@@ -1,10 +1,18 @@
import inspect
import uuid
from .exception import RipsError
from .pdmobject import add_method
from .project import Project
from .resinsight_classes import RegularSurface
from typing import List
from .resinsight_classes import (
EnsembleSurface,
RegularSurface,
SurfaceCollection,
SurfaceInterface,
)
from typing import List, Optional, Type, TypeVar
SurfaceT = TypeVar("SurfaceT", bound=SurfaceInterface)
@add_method(RegularSurface)
@@ -61,3 +69,111 @@ def get_property(self: RegularSurface, name: str) -> List[float]:
project.remove_key_values(key)
except Exception:
pass
def _check_surface_type(surface_type: Type[SurfaceT]) -> None:
if not (
inspect.isclass(surface_type) and issubclass(surface_type, SurfaceInterface)
):
raise RipsError(
"surface_type must be a surface class such as rips.SurfaceInterface, "
"rips.Surface or rips.RegularSurface, got {}".format(surface_type)
)
@add_method(SurfaceCollection)
def surfaces(
self: SurfaceCollection,
surface_type: Type[SurfaceT] = SurfaceInterface,
recursive: bool = False,
include_ensembles: bool = False,
) -> List[SurfaceT]:
"""Get the surfaces in this folder
Arguments:
surface_type (class): Only return surfaces of this class, subclasses included.
rips.SurfaceInterface (the default) returns every surface, rips.Surface
returns file surfaces only, and rips.RegularSurface also returns
rips.RegularFileSurface.
recursive (bool): Also return the surfaces in sub folders.
include_ensembles (bool): When recursive, also descend into ensemble surfaces
(rips.EnsembleSurface), which hold six statistics surfaces and one surface
per realization. Off by default, since that can be a long list.
Returns:
List of surfaces of the requested class, this folder first, then its sub folders
"""
_check_surface_type(surface_type)
result: List[SurfaceT] = [
s for s in self.surfaces_field() if isinstance(s, surface_type)
]
if recursive:
for folder in self.sub_collections():
if isinstance(folder, EnsembleSurface) and not include_ensembles:
continue
result.extend(
folder.surfaces(
surface_type, recursive=True, include_ensembles=include_ensembles
)
)
return result
@add_method(Project) # type: ignore[no-redef]
def surfaces( # noqa: F811
self: Project,
surface_type: Type[SurfaceT] = SurfaceInterface,
include_ensembles: bool = False,
) -> List[SurfaceT]:
"""Get all surfaces in the project, from the top surface folder and its sub folders
Arguments:
surface_type (class): Only return surfaces of this class, subclasses included.
rips.SurfaceInterface (the default) returns every surface, rips.Surface
returns file surfaces only, and rips.RegularSurface also returns
rips.RegularFileSurface.
include_ensembles (bool): Also return the statistics and realization surfaces
of ensemble surfaces (rips.EnsembleSurface). Off by default.
Returns:
List of surfaces of the requested class
"""
folder = self.surface_folder()
if folder is None:
return []
return folder.surfaces(
surface_type, recursive=True, include_ensembles=include_ensembles
)
@add_method(Project)
def surface_by_name(
self: Project,
name: str,
surface_type: Type[SurfaceT] = SurfaceInterface, # type: ignore[assignment]
folder: Optional[SurfaceCollection] = None,
) -> Optional[SurfaceT]:
"""Get a surface by name
Names are only unique within one folder. Without a folder the first match is
returned, searching the top surface folder first and then its sub folders, ensemble
surfaces excluded. The name is matched against surface_user_description, so a depth
offset shown in the tree does not affect the match.
Arguments:
name (str): The surface name (surface_user_description)
surface_type (class): Only consider surfaces of this class, subclasses included
folder (SurfaceCollection): Only look in this folder, sub folders excluded
Returns:
The surface, or None when no surface has that name
"""
if folder is not None:
candidates = folder.surfaces(surface_type)
else:
candidates = self.surfaces(surface_type)
for surface in candidates:
if surface.surface_user_description == name:
return surface
return None
@@ -283,3 +283,87 @@ def test_import_surface_errors(rips_instance, initialize_test):
on_name_conflict=rips.NameConflictPolicy.AUTO_RENAME,
)
assert renamed.surface_user_description == "mysurface.ts_1"
def test_list_surfaces(rips_instance, initialize_test):
case_path = dataroot.PATH + "/Case_with_10_timesteps/Real0/BRUGGE_0000.EGRID"
case = rips_instance.project.load_case(path=case_path)
root = rips_instance.project.surface_folder()
assert root is not None
assert len(rips_instance.project.surfaces()) == 0
root.new_regular_surface(name="Top")
grid_surface = root.new_surface(case, 5)
folder = root.add_folder(folder_name="Folder A")
folder.new_regular_surface(name="Top")
with tempfile.TemporaryDirectory(prefix="rips") as tmpdirname:
path = Path(tmpdirname, "k_layer.ts")
grid_surface.export_to_file(path.as_posix())
file_surface = folder.import_surface(path.as_posix())
assert file_surface is not None
# Non-recursive on a folder, recursive from the project
assert len(root.surfaces()) == 2
assert len(root.surfaces(recursive=True)) == 4
assert len(folder.surfaces()) == 2
assert len(rips_instance.project.surfaces()) == 4
# Filter by class; rips.Surface is the file surface class
assert len(rips_instance.project.surfaces(rips.RegularSurface)) == 2
assert len(rips_instance.project.surfaces(rips.GridCaseSurface)) == 1
assert len(rips_instance.project.surfaces(rips.Surface)) == 1
assert len(root.surfaces(rips.Surface)) == 0
assert len(folder.surfaces(rips.Surface)) == 1
assert all(
isinstance(s, rips.RegularSurface)
for s in rips_instance.project.surfaces(rips.RegularSurface)
)
with pytest.raises(rips.RipsError, match="surface class"):
rips_instance.project.surfaces(rips.SurfaceCollection)
def test_surface_by_name(rips_instance, initialize_test):
case_path = dataroot.PATH + "/Case_with_10_timesteps/Real0/BRUGGE_0000.EGRID"
rips_instance.project.load_case(path=case_path)
root = rips_instance.project.surface_folder()
folder = root.add_folder(folder_name="Folder A")
root_top = root.new_regular_surface(name="Top")
folder_top = folder.new_regular_surface(name="Top")
folder.new_regular_surface(name="Base")
# First match is the one in the top folder; a folder argument restricts the search
assert rips_instance.project.surface_by_name("Top").address() == root_top.address()
assert (
rips_instance.project.surface_by_name("Top", folder=folder).address()
== folder_top.address()
)
assert rips_instance.project.surface_by_name("Base", folder=root) is None
assert rips_instance.project.surface_by_name("Base") is not None
assert rips_instance.project.surface_by_name("Missing") is None
assert rips_instance.project.surface_by_name("Top", rips.GridCaseSurface) is None
# The displayed name gets the depth offset appended, the match does not
root_top.depth_offset = 10.0
root_top.update()
assert rips_instance.project.surface_by_name("Top").address() == root_top.address()
def test_surface_folder_lookup(rips_instance, initialize_test):
case_path = dataroot.PATH + "/Case_with_10_timesteps/Real0/BRUGGE_0000.EGRID"
rips_instance.project.load_case(path=case_path)
root = rips_instance.project.surface_folder()
assert (
root.address()
== rips_instance.project.descendants(rips.SurfaceCollection)[0].address()
)
folder = root.add_folder(folder_name="Folder A")
assert (
rips_instance.project.surface_folder("Folder A").address() == folder.address()
)
assert rips_instance.project.surface_folder("Missing") is None