Currently the posts are filtered by: gildas
Reset this filter to see all posts.


Using Gildas/Class from Python

As many people have been asking me on how to use Gildas/Class from within Python, I'll try to give a quick tour here. You will need to have Class installed including the Pyclass extension.

Before you start the Python script you need to initialize Gildas in the shell. Make also sure it is in your $PYTHONPATH. On my local PC using a bash shell where Gildas is installed in $GILDAS_DIR that is

source $GILDAS_DIR/
export PYTHONPATH=$PYTHONPATH:$GILAS_DIR/gildas-exe-mar18b/x86_64-ubuntu14.04-gfortran/python/

The example above is for the version of Class tested in March 2018 - you might need to change the path for your installed version accordingly. Check if Class is running as usual and that indeed you find the Gildas-path in your $PYTHONPATH.

Then you can execute the Python script or go into iPython. Here is a minimal example:

import pygildas, pyclass
# if you want your program to catch PygildasErrors:
from pgutils import PygildasError
# you are good to go from here, but to access stuff easier, I prefer
# to assign the two most commonly used objects to individual 
# variables for easy access
# get the gildas command line interpreter to issue commands quickly
sic = pyclass.comm
# get the pyclass.gdict object for easy Gildas variable access
g = pyclass.gdict
# just execute commands as you would within Class
# you just do not have the display - use matplotlib ;)
sic('SIC MESSAGE GLOBAL ON') # or OFF for less noise
sic('file in "myData.apex"')
# ...

To access Gildas variables we use the pyclass.gdict (the object we assigned to g). Unfortunately