Info |
---|
The examples and code provided for creating stand-alone programs that use the GX Developer are provided as a guide so you know what is possible. Note that the examples provided are not tested in the Current API. As a consequence, there may be minor discrepancies between the documentation and the working examples, and the working examples may require minor changes to be functional in the current release. |
Stand-Alone Python Script
A stand-alone python program (script) can be run from directly from Python without Oasis montaj, but and you can still access GXAPI and the GXPY modules for any functions that do not depend on Oasis montaj.
- Use the "
if __name__ == "__main__":
" to isolate the stand-alone code execution path. - Methods in the gxpy.om module are not available.
- Methods in the gxapi module that require Oasis montaj interface (such as GXEDB, GXEMAP, etc.) are not available.
You must create a GXContext before calling any Geosoft modules:
Code Block # example using geosoft.gxpy include geosoft.gxpy as gxpy if __name__ == "__main__": gxc = gxpy.gx.GXpy() # ... your code follows ...
Code Block # example using geosoft.gxapi include geosoft.gxpy as gxapi if __name__ == "__main__": gxc = gxapy.GXContext.create("my program","1.0") # ... your code follows ...
Python GX
There are cases where you may want to organize your workflow to use a stand-alone Python script. For example, a python GUI application like PyQt may require a stand-alone instance.
- Maki Rider (Unlicensed) add Python.gx to the GX folder and complete this documentation
Info | ||
---|---|---|
| ||
Python.gx is a GX that will run a Python script directly from an open Oasis montaj project. The GX saves all current state information to a YAML file, which the Python script can open to obtain state information, such as the current database, displayed channels, current map, etc. You will find the Python GX and source code, together with script examples here: python.zip v1.1 |
You will find a number of Python examples in:
C:\Program Files (x86)\Geosoft\GX Developer\apps\examples\python
ChanAdd.py - This is a simple example of reading and writing from a Geosoft database. It also demonstrates working with command line arguments, writing to a file and how to get lists of channels and lines from a database.
GridCopy.py - A simple example showing how to copy a grid. It demonstrates the use of the GXIMG interface
GridFilter.py - This is a more sophisticated grid handling routine. The GXIMU.grid_filt function upon which it depends is complicated, because it is very flexible. It requires 10 parameters, several of which are inter-dependent. The user can specify a predefined filter or a filename containing the filter elements. In the GX version of this utility the user can also input a filter string directly, though this has not been implemented here.
You should copy these samples to a working folder on your system, and then review how they work and experiment with your own modifications. A number of Python concepts are demonstrated, including a filtered list comprehension, slicing, several styles of iteration and print statements, and simple text file processing.
.
In all cases we use the Python argparse class to provide consistent use of the command line. for example, running:
python ChanAdd.py -h
will display the command line help for the ChanAdd.py script.
You will see that all scripts will import gx.geosoft as gx
, and a GX context is obtained by calling gx.GXContext.create()
. Most calls into the GX API will require the GX copntext to be provided as the first argument.
The GX API also includes many constants, and you will see examples of obtaining and using these constants in these sample files.
Note that some GX methods need to pass back a simple immutable data value (string, boolean, integer or float) by reference, but passing by reference is not possible in Python. For these situations, the GX API provides a set of simple classes which can be used to pass a mutable value that can be shared between the caller and the called function. Search for "immutable values by reference" in the on-line documentation for more information.
And finally, we include source code for all Geosoft GX tools as part of the GX Developer package. You can use these for reference to see how more complicated things are achieved in a GX, and the Python code will be very similar.