Basic Operations

This page shows Python examples from the basic_operations folder.

All Cases

all_cases.py
 1###################################################################################
 2# This example will connect to ResInsight, retrieve a list of cases and print info
 3#
 4###################################################################################
 5
 6# Import the ResInsight Processing Server Module
 7import rips
 8
 9# Connect to ResInsight
10resinsight = rips.Instance.find()
11
12# Get a list of all cases
13cases = resinsight.project.cases()
14
15print("Got " + str(len(cases)) + " cases: ")
16for case in cases:
17    print("Case id: " + str(case.id))
18    print("Case name: " + case.name)
19    print("Case type: " + case.__class__.__name__)
20    print("Case file name: " + case.file_path)
21    print("Case reservoir bounding box:", case.reservoir_boundingbox())
22
23    timesteps = case.time_steps()
24    for t in timesteps:
25        print("Year: " + str(t.year))
26        print("Month: " + str(t.month))
27
28    if isinstance(case, rips.EclipseCase):
29        print("Getting coarsening info for case: ", case.name, case.id)
30        coarsening_info = case.coarsening_info()
31        if coarsening_info:
32            print("Coarsening information:")
33
34        for c in coarsening_info:
35            print(
36                "[{}, {}, {}] - [{}, {}, {}]".format(
37                    c.min.x, c.min.y, c.min.z, c.max.x, c.max.y, c.max.z
38                )
39            )

Case Info Streaming Example

case_info_streaming_example.py
 1###############################################################################
 2# This example will get the cell info for the active cells for the first case
 3###############################################################################
 4
 5# Import the ResInsight Processing Server Module
 6import rips
 7
 8# Connect to ResInsight
 9resinsight = rips.Instance.find()
10
11# Get the first case. This will fail if you haven't loaded any cases
12case = resinsight.project.cases()[0]
13
14# Get the cell count object
15cell_counts = case.cell_count()
16print("Number of active cells: " + str(cell_counts.active_cell_count))
17print("Total number of reservoir cells: " + str(cell_counts.reservoir_cell_count))
18
19# Get information for all active cells
20active_cell_infos = case.cell_info_for_active_cells()
21
22# A simple check on the size of the cell info
23assert cell_counts.active_cell_count == len(active_cell_infos)
24
25# Print information for the first active cell
26print("First active cell: ")
27print(active_cell_infos[0])

Error Handling

error_handling.py
 1###################################################################
 2# This example demonstrates the use of ResInsight exceptions
 3# for proper error handling
 4###################################################################
 5
 6import rips
 7import tempfile
 8
 9resinsight = rips.Instance.find()
10
11case = None
12
13# Try loading a non-existing case. We should get a grpc.RpcError exception from the server
14try:
15    case = resinsight.project.load_case("Nonsense")
16except rips.RipsError as e:
17    print("Expected Server Exception Received while loading case: ", e)
18
19# Try loading well paths from a non-existing folder.  We should get a rips.RipsError exception from the server
20try:
21    well_path_files = resinsight.project.import_well_paths(
22        well_path_folder="NONSENSE/NONSENSE"
23    )
24except rips.RipsError as e:
25    print("Server Exception Received while loading wellpaths: ", e)
26
27# Try loading well paths from an existing but empty folder. We should get a warning.
28try:
29    with tempfile.TemporaryDirectory() as tmpdirname:
30        well_path_files = resinsight.project.import_well_paths(
31            well_path_folder=tmpdirname
32        )
33        assert len(well_path_files) == 0
34        assert resinsight.project.has_warnings()
35        print("Should get warnings below")
36        for warning in resinsight.project.warnings():
37            print(warning)
38except rips.RipsError as e:
39    print("Unexpected Server Exception caught!!!", e)
40
41case = resinsight.project.case(case_id=0)
42if case is not None:
43    results = case.active_cell_property(rips.PropertyType.STATIC_NATIVE, "PORO", 0)
44    active_cell_count = len(results)
45
46    # Send the results back to ResInsight inside try / except construct
47    try:
48        case.set_active_cell_property(
49            results, rips.PropertyType.GENERATED, "POROAPPENDED", 0
50        )
51        print("Everything went well as expected")
52    except Exception as e:  # Match any exception, but it should not happen
53        print("Ooops!", e)
54
55    # Add another value, so this is outside the bounds of the active cell result storage
56    results.append(1.0)
57
58    # This time we should get a rips.RipsError exception.
59    try:
60        case.set_active_cell_property(
61            results, rips.PropertyType.GENERATED, "POROAPPENDED", 0
62        )
63        print("Everything went well??")
64    except rips.RipsError as e:
65        print("Server Exception Received: ", e)
66    except IndexError:
67        print("Got index out of bounds error. This shouldn't happen here")
68
69    # With a chunk size exactly matching the active cell count the server will not
70    # be able to see any error as it will successfully close the stream after receiving
71    # the correct number of values, even if the python client has more chunks to send
72    case.chunk_size = active_cell_count
73
74    try:
75        case.set_active_cell_property(
76            results, rips.PropertyType.GENERATED, "POROAPPENDED", 0
77        )
78        print("Everything went well??")
79    except rips.RipsError as e:
80        print("Got unexpected server exception", e, "This should not happen now")
81    except IndexError:
82        print("Got expected index out of bounds error on client side")

Instance Example

instance_example.py
 1#######################################
 2# This example connects to ResInsight
 3#######################################
 4import rips
 5
 6try:
 7    resinsight = rips.Instance.find()
 8    print("Successfully connected to ResInsight")
 9except rips.RipsError as e:
10    print(f"ERROR: could not find ResInsight: {e}")

Load Case

load_case.py
 1# Access to environment variables and path tools
 2import os
 3
 4# Load ResInsight Processing Server Client Library
 5import rips
 6
 7# Connect to ResInsight instance
 8resinsight = rips.Instance.find()
 9
10# This requires the TestModels to be installed with ResInsight (RESINSIGHT_BUNDLE_TESTMODELS):
11resinsight_exe_path = os.environ.get("RESINSIGHT_EXECUTABLE")
12
13# Get the TestModels path from the executable path
14resinsight_install_path = os.path.dirname(resinsight_exe_path)
15test_models_path = os.path.join(resinsight_install_path, "TestModels")
16path_name = os.path.join(
17    test_models_path, "TEST10K_FLT_LGR_NNC/TEST10K_FLT_LGR_NNC.EGRID"
18)
19case = resinsight.project.load_case(path_name)
20case.create_view()
21
22# Print out lots of information from the case object
23print("Case id: " + str(case.id))
24print("Case name: " + case.name)
25print("Case type: " + case.__class__.__name__)
26print("Case file name: " + case.file_path)
27print("Case reservoir bounding box:", case.reservoir_boundingbox())
28
29timesteps = case.time_steps()
30for t in timesteps:
31    print("Year: " + str(t.year))
32    print("Month: " + str(t.month))

Replace Case

replace_case.py
 1# Load ResInsight Processing Server Client Library
 2import rips
 3
 4# Connect to ResInsight instance
 5resinsight = rips.Instance.find()
 6# Example code
 7print("ResInsight version: " + resinsight.version_string())
 8
 9case = resinsight.project.case(case_id=0)
10case.replace(
11    new_grid_file="C:/Users/lindkvis/Projects/ResInsight/TestModels/Case_with_10_timesteps/Real0/BRUGGE_0000.EGRID"
12)

Selected Cases

selected_cases.py
 1############################################################################
 2# This example returns the currently selected cases in ResInsight
 3# Because running this script in the GUI takes away the selection
 4# This script does not run successfully from within the ResInsight GUI
 5# And will need to be run from the command line separately from ResInsight
 6############################################################################
 7
 8import rips
 9
10resinsight = rips.Instance.find()
11cases = resinsight.project.selected_cases()
12
13print("Got " + str(len(cases)) + " cases: ")
14for case in cases:
15    print(case.name)
16    for property in case.available_properties(rips.PropertyType.DYNAMIC_NATIVE):
17        print(property)