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()
11if resinsight is not None:
12    # Get a list of all cases
13    cases = resinsight.project.cases()
14
15    print("Got " + str(len(cases)) + " cases: ")
16    for 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("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(results, "GENERATED", "POROAPPENDED", 0)
49        print("Everything went well as expected")
50    except Exception as e:  # Match any exception, but it should not happen
51        print("Ooops!", e)
52
53    # Add another value, so this is outside the bounds of the active cell result storage
54    results.append(1.0)
55
56    # This time we should get a rips.RipsError exception.
57    try:
58        case.set_active_cell_property(results, "GENERATED", "POROAPPENDED", 0)
59        print("Everything went well??")
60    except rips.RipsError as e:
61        print("Server Exception Received: ", e)
62    except IndexError:
63        print("Got index out of bounds error. This shouldn't happen here")
64
65    # With a chunk size exactly matching the active cell count the server will not
66    # be able to see any error as it will successfully close the stream after receiving
67    # the correct number of values, even if the python client has more chunks to send
68    case.chunk_size = active_cell_count
69
70    try:
71        case.set_active_cell_property(results, "GENERATED", "POROAPPENDED", 0)
72        print("Everything went well??")
73    except rips.RipsError as e:
74        print("Got unexpected server exception", e, "This should not happen now")
75    except IndexError:
76        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
 6resinsight = rips.Instance.find()
 7
 8if resinsight is None:
 9    print("ERROR: could not find ResInsight")
10else:
11    print("Successfully connected to ResInsight")

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()
11if resinsight is not None:
12    cases = resinsight.project.selected_cases()
13
14    print("Got " + str(len(cases)) + " cases: ")
15    for case in cases:
16        print(case.name)
17        for property in case.available_properties("DYNAMIC_NATIVE"):
18            print(property)