From 055cb732db4e065071a02870ed2755f2c147428b Mon Sep 17 00:00:00 2001 From: Gaute Lindkvist Date: Fri, 14 Jun 2019 16:07:09 +0200 Subject: [PATCH] Python: fix up some examples --- .../GrpcInterface/CMakeLists.cmake | 2 + .../GrpcInterface/Python/rips/Case.py | 21 ++++-- .../GrpcInterface/Python/rips/Commands.py | 69 +++++++++++++++++-- .../GrpcInterface/Python/rips/Instance.py | 12 ++-- .../GrpcInterface/Python/rips/Project.py | 6 +- .../GrpcInterface/Python/rips/Properties.py | 56 +++++++-------- .../GrpcInterface/Python/rips/__init__.py | 3 + .../Python/rips/examples/AppInfo.py | 10 +++ .../rips/examples/InputPropTestAsync.py | 5 +- .../Python/rips/examples/InputPropTestSync.py | 1 + .../Python/rips/examples/InstanceExample.py | 10 +++ .../GrpcInterface/Python/setup.py.cmake | 2 +- 12 files changed, 148 insertions(+), 49 deletions(-) create mode 100644 ApplicationCode/GrpcInterface/Python/rips/examples/AppInfo.py create mode 100644 ApplicationCode/GrpcInterface/Python/rips/examples/InstanceExample.py diff --git a/ApplicationCode/GrpcInterface/CMakeLists.cmake b/ApplicationCode/GrpcInterface/CMakeLists.cmake index 79d2117361..6096786078 100644 --- a/ApplicationCode/GrpcInterface/CMakeLists.cmake +++ b/ApplicationCode/GrpcInterface/CMakeLists.cmake @@ -156,6 +156,8 @@ if (PYTHON_EXECUTABLE AND EXISTS ${PYTHON_EXECUTABLE}) "rips/Project.py" "rips/Properties.py" "rips/Instance.py" + "rips/examples/AppInfo.py" + "rips/examples/InstanceExample.py" "rips/examples/CommandExample.py" "rips/examples/CaseInfoStreamingExample.py" "rips/examples/SoilPorvAsync.py" diff --git a/ApplicationCode/GrpcInterface/Python/rips/Case.py b/ApplicationCode/GrpcInterface/Python/rips/Case.py index 000843a563..202fb3fd24 100644 --- a/ApplicationCode/GrpcInterface/Python/rips/Case.py +++ b/ApplicationCode/GrpcInterface/Python/rips/Case.py @@ -17,9 +17,9 @@ class Case: methods in Project: loadCase, case, allCases, selectedCases Attributes: - id(int): Case Id corresponding to case Id in ResInsight project. - name(string): Case name - groupId(int): Case Group id + id (int): Case Id corresponding to case Id in ResInsight project. + name (str): Case name + groupId (int): Case Group id """ def __init__(self, channel, id): self.channel = channel @@ -43,7 +43,13 @@ class Case: return 0 def grid(self, index): - """Get Grid of a given index. Returns a rips Grid object""" + """Get Grid of a given index. Returns a rips Grid object + + Arguments: + index (int): The grid index + + Returns: Grid object + """ return Grid(index, self) def grids(self): @@ -58,7 +64,7 @@ class Case: total number of cells Arguments: - porosityModel(string): String representing an enum. + porosityModel (str): String representing an enum. must be 'MATRIX_MODEL' or 'FRACTURE_MODEL'. Returns: Cell Count object with the following integer attributes: @@ -72,9 +78,11 @@ class Case: def cellInfoForActiveCells(self, porosityModel='MATRIX_MODEL'): """Get Stream of cell info objects for current case + Arguments: - porosityModel(string): String representing an enum. + porosityModel(str): String representing an enum. must be 'MATRIX_MODEL' or 'FRACTURE_MODEL'. + Returns: Stream of cell info objects with the following attributes: grid_index(int): grid the cell belongs to @@ -82,6 +90,7 @@ class Case: coarsening_box_index(int): the coarsening box index local_ijk(Vec3i: i(int), j(int), k(int)): local cell index in i, j, k directions. parent_ijk(Vec3i: i(int), j(int), k(int)): cell index in parent grid in i, j, k. + """ porosityModelEnum = Case_pb2.PorosityModelType.Value(porosityModel) request = Case_pb2.CellInfoRequest(case_request=self.request, diff --git a/ApplicationCode/GrpcInterface/Python/rips/Commands.py b/ApplicationCode/GrpcInterface/Python/rips/Commands.py index 687e3b5c58..87517aa3a5 100644 --- a/ApplicationCode/GrpcInterface/Python/rips/Commands.py +++ b/ApplicationCode/GrpcInterface/Python/rips/Commands.py @@ -16,8 +16,9 @@ class Commands: https://resinsight.org/docs/commandfile/ The differences are: - 1. Enum values have to be provided as strings. I.e. "ALL" instead of ALL. - 2. Booleans have to be specified as correct Python. True instead of true. + * Enum values have to be provided as strings. I.e. "ALL" instead of ALL. + * Booleans have to be specified as correct Python. True instead of true. + """ def __init__(self, channel): self.channel = channel @@ -37,24 +38,61 @@ class Commands: ######################## def openProject(self, path): + """Open a project + + Arguments: + path (str): path to project file + + + """ return self.__execute(openProject=Cmd.FilePathRequest(path=path)) def closeProject(self): + """Close the current project (and reopen empty one)""" return self.__execute(closeProject=Empty()) def setStartDir(self, path): + """Set current start directory + + Arguments: + path (str): path to directory + + """ return self.__execute(setStartDir=Cmd.FilePathRequest(path=path)) def loadCase(self, path): + """Load a case + + Arguments: + path (str): path to EGRID file + + Returns: + A Case object + + """ commandReply = self.__execute(loadCase=Cmd.FilePathRequest(path=path)) assert commandReply.HasField("loadCaseResult") return Case(self.channel, commandReply.loadCaseResult.id) - def replaceCase(self, path, caseId=0): - return self.__execute(replaceCase=Cmd.ReplaceCaseRequest(newGridFile=path, + def replaceCase(self, newGridFile, caseId=0): + """Replace the given case with a new case loaded from file + + Arguments: + newGridFile (str): path to EGRID file + caseId (int): case Id to replace + + """ + return self.__execute(replaceCase=Cmd.ReplaceCaseRequest(newGridFile=newGridFile, caseId=caseId)) def replaceSourceCases(self, gridListFile, caseGroupId=0): + """Replace all source cases within a case group + + Arguments: + gridListFile (str): path to file containing a list of cases + caseGroupId (int): id of the case group to replace + + """ return self.__execute(replaceSourceCases=Cmd.ReplaceSourceCasesRequest(gridListFile=gridListFile, caseGroupId=caseGroupId)) ################## @@ -62,13 +100,36 @@ class Commands: ################## def exportMultiCaseSnapshots(self, gridListFile): + """Export snapshots for a set of cases + + Arguments: + gridListFile (str): Path to a file containing a list of grids to export snapshot for + + """ return self.__execute(exportMultiCaseSnapshot=Cmd.ExportMultiCaseRequest(gridListFile=gridListFile)) def exportSnapshots(self, type = 'ALL', prefix=''): + """ Export snapshots of a given type + + Arguments: + type (str): Enum string ('ALL', 'VIEWS' or 'PLOTS') + prefix (str): Exported file name prefix + + """ return self.__execute(exportSnapshots=Cmd.ExportSnapshotsRequest(type=type, prefix=prefix)) def exportProperty(self, caseId, timeStep, property, eclipseKeyword=property, undefinedValue=0.0, exportFile=property): + """ Export an Eclipse property + + Arguments: + caseId (int): case id + timeStep (int): time step index + property (str): property to export + eclipseKeyword (str): Eclipse keyword used as text in export header. Defaults to the value of property parameter. + undefinedValue (double): Value to use for undefined values. Defaults to 0.0 + exportFile (str): Filename for export. Defaults to the value of property parameter + """ return self.__execute(exportProperty=Cmd.ExportPropertyRequest(caseId=caseId, timeStep=timeStep, property=property, diff --git a/ApplicationCode/GrpcInterface/Python/rips/Instance.py b/ApplicationCode/GrpcInterface/Python/rips/Instance.py index d8b626c5a2..33a254bc82 100644 --- a/ApplicationCode/GrpcInterface/Python/rips/Instance.py +++ b/ApplicationCode/GrpcInterface/Python/rips/Instance.py @@ -17,11 +17,11 @@ class Instance: """The ResInsight Instance class. Use to launch or find existing ResInsight instances Attributes: - launched(bool): Tells us whether the application was launched as a new process. + launched (bool): Tells us whether the application was launched as a new process. If the application was launched we may need to close it when exiting the script. - app(App): Application information object. Set when creating an instance. - commands(Commands): Command executor. Set when creating an instance. - project(Project): Current project in ResInsight. + app (App): Application information object. Set when creating an instance. + commands (Commands): Command executor. Set when creating an instance. + project (Project): Current project in ResInsight. Set when creating an instance and updated when opening/closing projects. """ @@ -83,8 +83,8 @@ class Instance: RESINSIGHT_GRPC_PORT to RESINSIGHT_GRPC_PORT+20 Args: - startPort(int): start searching from this port - endPort(int): search up to but not including this port + startPort (int): start searching from this port + endPort (int): search up to but not including this port """ portEnv = os.environ.get('RESINSIGHT_GRPC_PORT') if portEnv: diff --git a/ApplicationCode/GrpcInterface/Python/rips/Project.py b/ApplicationCode/GrpcInterface/Python/rips/Project.py index 4d3b720fe9..d97aae4504 100644 --- a/ApplicationCode/GrpcInterface/Python/rips/Project.py +++ b/ApplicationCode/GrpcInterface/Python/rips/Project.py @@ -23,8 +23,8 @@ class Project: def open(self, path): """Open a new project from the given path - Argument: - path(string): path to project file + Arguments: + path(str): path to project file """ Commands(self.channel).openProject(path) @@ -84,7 +84,7 @@ class Project: """Load a new case from the given file path Arguments: - path(string): file path to case + path(str): file path to case Returns: A rips Case object """ diff --git a/ApplicationCode/GrpcInterface/Python/rips/Properties.py b/ApplicationCode/GrpcInterface/Python/rips/Properties.py index a95b6bdd1f..9bd5a3dc7b 100644 --- a/ApplicationCode/GrpcInterface/Python/rips/Properties.py +++ b/ApplicationCode/GrpcInterface/Python/rips/Properties.py @@ -55,18 +55,20 @@ class Properties: def available(self, propertyType, porosityModel = 'MATRIX_MODEL'): """Get a list of available properties - Arguments: - propertyType(string): string corresponding to propertyType enum - Can be one of the following: - 'DYNAMIC_NATIVE' - 'STATIC_NATIVE' - 'SOURSIMRL' - 'GENERATED' - 'INPUT_PROPERTY' - 'FORMATION_NAMES' - 'FLOW_DIAGNOSTICS' - 'INJECTION_FLOODING' - porosityModel(string): 'MATRIX_MODEL' or 'FRACTURE_MODEL'. + Arguments: + propertyType (str): string corresponding to propertyType enum. + + Can be one of the following: + + 'DYNAMIC_NATIVE' + 'STATIC_NATIVE' + 'SOURSIMRL' + 'GENERATED' + 'INPUT_PROPERTY' + 'FORMATION_NAMES' + 'FLOW_DIAGNOSTICS' + 'INJECTION_FLOODING' + porosityModel(str): 'MATRIX_MODEL' or 'FRACTURE_MODEL'. """ propertyTypeEnum = Properties_pb2.PropertyType.Value(propertyType) @@ -80,10 +82,10 @@ class Properties: """Get a cell property for all active cells. Async, so returns an iterator Arguments: - propertyType(string): string enum. See available() - propertyName(string): name of an Eclipse property + propertyType(str): string enum. See available() + propertyName(str): name of an Eclipse property timeStep(int): the time step for which to get the property for - porosityModel(string): string enum. See available() + porosityModel(str): string enum. See available() Returns: An iterator to a chunk object containing an array of double values @@ -103,11 +105,11 @@ class Properties: """Get a cell property for all grid cells. Async, so returns an iterator Arguments: - propertyType(string): string enum. See available() - propertyName(string): name of an Eclipse property + propertyType(str): string enum. See available() + propertyName(str): name of an Eclipse property timeStep(int): the time step for which to get the property for gridIndex(int): index to the grid we're getting values for - porosityModel(string): string enum. See available() + porosityModel(str): string enum. See available() Returns: An iterator to a chunk object containing an array of double values @@ -129,10 +131,10 @@ class Properties: Arguments: values_iterator(iterator): an iterator to the properties to be set - propertyType(string): string enum. See available() - propertyName(string): name of an Eclipse property + propertyType(str): string enum. See available() + propertyName(str): name of an Eclipse property timeStep(int): the time step for which to get the property for - porosityModel(string): string enum. See available() + porosityModel(str): string enum. See available() """ propertyTypeEnum = Properties_pb2.PropertyType.Value(propertyType) porosityModelEnum = Case_pb2.PorosityModelType.Value(porosityModel) @@ -155,10 +157,10 @@ class Properties: Arguments: values(list): a list of double precision floating point numbers - propertyType(string): string enum. See available() - propertyName(string): name of an Eclipse property + propertyType(str): string enum. See available() + propertyName(str): name of an Eclipse property timeStep(int): the time step for which to get the property for - porosityModel(string): string enum. See available() + porosityModel(str): string enum. See available() """ propertyTypeEnum = Properties_pb2.PropertyType.Value(propertyType) porosityModelEnum = Case_pb2.PorosityModelType.Value(porosityModel) @@ -181,11 +183,11 @@ class Properties: Arguments: values(list): a list of double precision floating point numbers - propertyType(string): string enum. See available() - propertyName(string): name of an Eclipse property + propertyType(str): string enum. See available() + propertyName(str): name of an Eclipse property timeStep(int): the time step for which to get the property for gridIndex(int): index to the grid we're setting values for - porosityModel(string): string enum. See available() + porosityModel(str): string enum. See available() """ propertyTypeEnum = Properties_pb2.PropertyType.Value(propertyType) porosityModelEnum = Case_pb2.PorosityModelType.Value(porosityModel) diff --git a/ApplicationCode/GrpcInterface/Python/rips/__init__.py b/ApplicationCode/GrpcInterface/Python/rips/__init__.py index 6d6e54d164..1522a01bcc 100644 --- a/ApplicationCode/GrpcInterface/Python/rips/__init__.py +++ b/ApplicationCode/GrpcInterface/Python/rips/__init__.py @@ -3,3 +3,6 @@ from .Case import Case from .Grid import Grid from .Properties import Properties from .Instance import Instance +from .App import App +from .Commands import Commands +from .Project import Project \ No newline at end of file diff --git a/ApplicationCode/GrpcInterface/Python/rips/examples/AppInfo.py b/ApplicationCode/GrpcInterface/Python/rips/examples/AppInfo.py new file mode 100644 index 0000000000..d8d71d340e --- /dev/null +++ b/ApplicationCode/GrpcInterface/Python/rips/examples/AppInfo.py @@ -0,0 +1,10 @@ +import sys +import os + +sys.path.insert(1, os.path.join(sys.path[0], '../../')) +import rips + +resInsight = rips.Instance.find() +if resInsight is not None: + print(resInsight.app.versionString()) + print("Is this a console run?", resInsight.app.isConsole()) diff --git a/ApplicationCode/GrpcInterface/Python/rips/examples/InputPropTestAsync.py b/ApplicationCode/GrpcInterface/Python/rips/examples/InputPropTestAsync.py index 237164e78d..b4bfdc133c 100644 --- a/ApplicationCode/GrpcInterface/Python/rips/examples/InputPropTestAsync.py +++ b/ApplicationCode/GrpcInterface/Python/rips/examples/InputPropTestAsync.py @@ -1,6 +1,7 @@ import sys import os sys.path.insert(1, os.path.join(sys.path[0], '../../')) + import rips import time @@ -11,7 +12,6 @@ def createResult(poroChunks, permxChunks): resultChunk.append(poro * permx) yield resultChunk - resInsight = rips.Instance.find() start = time.time() case = resInsight.project.case(id=0) @@ -19,7 +19,8 @@ case = resInsight.project.case(id=0) poroChunks = case.properties.activeCellProperty('STATIC_NATIVE', 'PORO', 0) permxChunks = case.properties.activeCellProperty('STATIC_NATIVE', 'PERMX', 0) -case.properties.setActiveCellPropertyAsync(createResult(poroChunks, permxChunks), 'GENERATED', 'POROPERMXAS', 0) +case.properties.setActiveCellPropertyAsync(createResult(poroChunks, permxChunks), + 'GENERATED', 'POROPERMXAS', 0) end = time.time() print("Time elapsed: ", end - start) diff --git a/ApplicationCode/GrpcInterface/Python/rips/examples/InputPropTestSync.py b/ApplicationCode/GrpcInterface/Python/rips/examples/InputPropTestSync.py index 4618fa5556..62b09804a2 100644 --- a/ApplicationCode/GrpcInterface/Python/rips/examples/InputPropTestSync.py +++ b/ApplicationCode/GrpcInterface/Python/rips/examples/InputPropTestSync.py @@ -1,6 +1,7 @@ import sys import os sys.path.insert(1, os.path.join(sys.path[0], '../../')) + import rips import time diff --git a/ApplicationCode/GrpcInterface/Python/rips/examples/InstanceExample.py b/ApplicationCode/GrpcInterface/Python/rips/examples/InstanceExample.py new file mode 100644 index 0000000000..2b08f90d6c --- /dev/null +++ b/ApplicationCode/GrpcInterface/Python/rips/examples/InstanceExample.py @@ -0,0 +1,10 @@ +import sys +import os + +sys.path.insert(1, os.path.join(sys.path[0], '../../')) +import rips + +resInsight = rips.Instance.find() + +if resInsight is None: + print('ERROR: could not find ResInsight') \ No newline at end of file diff --git a/ApplicationCode/GrpcInterface/Python/setup.py.cmake b/ApplicationCode/GrpcInterface/Python/setup.py.cmake index 17dbffd5fc..a91a6f682c 100644 --- a/ApplicationCode/GrpcInterface/Python/setup.py.cmake +++ b/ApplicationCode/GrpcInterface/Python/setup.py.cmake @@ -6,7 +6,7 @@ with open('README.md') as f: with open('LICENSE') as f: license = f.read() -RIPS_DIST_VERSION = '8' +RIPS_DIST_VERSION = '9' setup( name='rips',