Python interface

From GeopsyWiki
Revision as of 20:08, 11 January 2024 by Marc (talk | contribs)
Jump to navigation Jump to search

Some functionalities are accessible from a Python script. Currently there are two modules:

  • GeopsyPyCoreWave: surface wave computation
  • GeopsyPyScifigs: graphical tools to plot data

Other modules will be probably proposed in the future (e.g. Neighborhood inversion).

Installation

The modules are compiled with Geopsy package if Python include files are listed in the include paths. At configure step, add Python include path (among other options not listed here, see [[Welcome_to_GeopsyWiki#Installation|general instructions] specific to your platform]):

 ./configure -I /usr/include/python3.9

Currently, modules are tested on Linux only. Under Windows and Mac OS, it is not guaranteed that it works.

Upon completion of the build process, the modules are dynamic libraries located in the lib directory of the install path. Their names start with "lib" which prevents the recognition by Python. We suggest to create a new directory at the same level as lib and to modify the library names as follow:

 mkdir python
 cp lib/libGeopsyPyCoreWave.so python/GeopsyPyCoreWave.so
 cp lib/libGeopsyPySciFigs.so python/GeopsyPySciFigs.so

Finally, you have to add the path to Python

 export PYTHONPATH=/oath/where/geopsy/is/installed/python:$PYTHONPATH

Interface documentation

The list of functions available in each module

 import GeopsyPyCoreWave as gp
 help(gp)

Example

 import GeopsyPySciFigs as sf
 import numpy as np
 import GeopsyPyCoreWave as gp
 nmodes=7
 freq=np.logspace(np.log10(1), np.log10(50), 500)
 omega=freq.copy()
 omega*=2*np.pi

h=np.array([10,20]) vp=np.array([500,1000,3000]) vs=np.array([200,600,1500]) rho=np.array([2000,2000,2500])

slowRayleigh=gp.rayleighDispersionCurve(nmodes, 0, h, vp, vs, rho, omega) slowRayleighGroup=gp.rayleighDispersionCurve(nmodes, 1, h, vp, vs, rho, omega) ellRayleigh=gp.rayleighEllipticityCurve(nmodes, h, vp, vs, rho, omega) ellRayleigh=np.arctan(ellRayleigh)*(180/np.pi) slowLove=gp.loveDispersionCurve(nmodes, 0, h, vs, rho, omega)

def layoutAttributes(p, x, y, w, h):

 p.setAnchor("TopRight")
 p.xAxis().setSizeInfo(w)
 p.yAxis().setSizeInfo(h)
 p.setPrintX(x)
 p.setPrintY(y)

def frequencyAttributes(p):

 p.xAxis().setMin(1)
 p.xAxis().setMax(50)
 p.xAxis().setScaleType("Log")
 p.xAxis().setTitle("Frequency (Hz)")

def dispersionAttributes(p):

 frequencyAttributes(p)
 p.yAxis().setScaleType("InverseLog")
 p.yAxis().setMin(1/1500)
 p.yAxis().setMax(1/150)

s=sf.newSheet()

prp=sf.newPlot(s) sf.addCurves(prp, freq, slowRayleigh) layoutAttributes(prp, 12, 1, 11 ,6) dispersionAttributes(prp) prp.yAxis().setTitle("Rayleigh phase velocity (m/s)")

prg=sf.newPlot(s) sf.addCurves(prg, freq, slowRayleighGroup) layoutAttributes(prg, 23, 1, 11 ,6) dispersionAttributes(prg) prg.yAxis().setTitle("Rayleigh group velocity (m/s)")

per=sf.newPlot(s) sf.addCurves(per, freq, ellRayleigh) layoutAttributes(per, 12, 7, 11 ,6) frequencyAttributes(per) per.yAxis().setTitle("Angular ellipticity (deg)") per.yAxis().setMin(-90) per.yAxis().setMax(90) per.graphContents().layer(0).setSignThreshold(45)

pl=sf.newPlot(s) sf.addCurves(pl, freq, slowLove) layoutAttributes(pl, 23, 7, 11 ,6) dispersionAttributes(pl) pl.yAxis().setTitle("Love phase velocity (m/s)")

s.setPaperOrientation("Landscape") s.fileSave_2("/tmp/dc.page") s.exportImage_2("/tmp/dc.pdf")