Interface between Python and SGeMS

aboucher - Posted on 05 May 2009

SGeMS provide three commands to have Python interact with SGeMS data and algorithms. One can import or export data from-to SGeMS or execute the SGeMS commands.

get_property(gridName, propertyName)
Retrieve the property propertyName from the grid gridName and return the value in a python list
set_property(gridName, propertyName,data)
Save the python list data to the grid gridName with the name propertyName. Note that it will overwrite an existing property with the name propertyName
Execute any of the SGeMS command described here

These commands are in the library sgems. One must first include these functions in Python with the command import sgems. For example, the following code creates a 50x50x50 cartesian grid called grid using the SGeMS command NewCartesianGrid:

import sgems
sgems.execute('NewCartesianGrid  grid::50::50::50')

Next create a vertical trend that has values 0 at the bottom and 1 at the top. We first get the Z coordinates by retrieving the property _Z_ and then storing the maximum value z[-1]. The Z coordinates are then normalized and written back to the grid.

import sgems
z = sgems.get_property('grid','_Z_')
nz = z[-1]
for k in range(len(z)) :
  z[k] = z[k]/nz
sgems.set_property('grid','vertical trend',z)

The same operation can be rewritten in a few lines

import sgems
z = sgems.get_property('grid','_Z_')
sgems.set_property('grid','vertical trend', [k/z[-1]  for k in z])

The coordinates of all grids can be obtained by getting the property _X_, _Y_ or _Z_.

Finally add some dampened cyclicity to the trend by multiplying a sine and exponential function. The math library which contains many standard mathematical functions including the exp and sin function.

import sgems
import math
z = sgems.get_property('grid','_Z_')
t = [ math.exp(k/z[-1])*math.sin(k/z[-1]*5*math.pi)  for k in z]
tmin = min(t)
tmax = max(t)
t_norm = [(k-tmin)/(tmax-tmin) for k in t]
sgems.set_property('grid','vertical cycle trend', t_norm)