Introduction to geophysical programming using Python

From SubSurfWiki
Jump to navigation Jump to search

This page supports a course on beginning Python programming for geoscientists, taught by Evan Bianco.


Enabling geoscientists through hands-on practice and exercises to learn Python, share their algorithm, code, workflows, and ideas within and beyond their organization.

What is Python?

Python is an interpreted programming language that allows you to do almost anything possible with a compiled language (C/C++/Fortran) without requiring all the complexity.

Python Highlights

  • Automatic garbage collection
  • Dynamic typing
  • Interpreted and interactive
  • Objected-oriented
  • "Batteries included"
  • Free
  • Portable
  • Easy to learn and use
  • Truly modular

Who is using Python

  • Wall street - Banks and hedge funds rely on Python for their high speed trading systems, data analysis and visualization
  • Hollywood - Digital animation and special effects: Imageworks, Disney, Dreamworks
  • Google - One of the top three languages used at Google along with C++ and Java
  • YouTube - Highly scalable web application for delivering video content
  • Redhat -Anaconda, the Redhat Linux installer program, is written in Python
  • Petroleum industry

PYPL - PopularitY of Programming Language Index


An enhanced interactive Python shell

  • Starting IPython:
    • with the pylab icon (PC)
    • by typing ipython --pylab in any terminal/command prompt
    • in Canopy, open new editor window


An interactive Python environment, designed to feel like MATLAB. The PyLab mode in IPython handles some gory details behind the scenes. It allows both the Python command interpreter and the GUI plot window to coexist. This involves a bit of multi-threaded magic. PyLab also imports some handy functions into the command interpreter for user convenience.

IPython notebook

  • .ipnb files contain Python commands, inputs, outputs, figures, plain text (markdown), titles, and more
  • Create one in Canopy by selecting: File > New > IPython notebook


In [1]: a=1
In [2]: a
Out[2]: 1

Available variables

In [3]: b = [1,2,3]

List available variables.

In [4]: %whos
Variable Type Data/Info 
a         int     1
b         list    n=3

Remove user defined variables,

In[5]: %reset
In[6]: %whos
Interactive namespace is empty.

"%reset also remove the names imported into PyLab, such as the plot command.

In [7]: plot
NameError: name 'plot' is not defined

Reload pylab

<source lang=python>
In[8]: %pylab
Welcome to pylab,...

Directory Navigation in IPython

To change a directory (note the Unix style forward slashes)

In[9]: cd c:/python_class/Demos/Seismic 

Tab completion helps you find and type directory and file names.

List directory contents (Unix style, not "dir")

Volume in drive C has no label.
Volume Serial Number is 5417-593D
Directory of c:\python_class\Demos\speed_of_light
09/01/2008 02:53 PM <DIR> .
09/01/2008 02:53 PM <DIR> ..
09/01/2008 02:48 PM  1,188 exercise_seismic.txt
09/01/2008 02:48 PM  2,682,023 measurement_description.pdf
09/01/2008 02:48 PM  187,087 earth_experiment.pdf
09/01/2008 02:48 PM  1,312 speed_of_earth.dat
09/01/2008 02:48 PM  1,436
09/01/2008 02:48 PM  1,232
2,874,278 bytes
6 File(s)
2 Dir(s) 11,997,437,952 bytes free

Directory bookmarks

Running Scripts in IPython

Function information

  • help using ?
  • show source code using ??

IPython history

List previous commands

Use 'magic' % because 'hist' is the histogram function in pylab

In [3]: %hist
a = 1

Input history

List string from prompt[2]

In[4]: _i2
Out[4]: 'a/n'

Output history

Grab previous result

In[5]: _
Out[5]: 'a/n'

Grab result from prompt[2]

In [6]: _2
Out[6]: 1

Also, use the up and down arrows to scroll through your iPython input history.

Reading simple tracebacks

Language introduction

  • Interactive Calculator
  • Strings and string methods
  • String formatting
  • Lists and list methods
  • Indexing lists
  • Slicing lists
  • Tuples (Mutable vs Immutable)
  • Dictionaries
  • Dictionary methods
  • Set objects
  • Set operations
  • Assignment of a "simple" object
  • Assignment of a container object
  • If statements
  • While loops
  • For loops
  • List comprehension
  • Generator expressions
  • Multiple assignments
  • Looping patterns
  • Looping over a dictionary
  • Anatomy of a function
    • Our new function in action
    • Function calling conventions
    • Expanding function arguments
  • Modules

A Python file can be used as a script, or as a module, or both

  • import Variations
    • basic imports
    • aliasing a name
    • importing specific symbols
    • importing everything
    • packages
    • file structure
  • Setting up PYTHONPATH
  • Reading files
  • Writing files
  • Sorting


  • Canopy
  • iPython
  • documentation
  • GitHub
  • functions
  • modules
  • classes

NumPy and SciPy

  • arrays
  • simple plotting
  • images
  • histograms
  • simple array math
  • filters
  • slicing
  • creating arrays from ascii

Dealing with SEGY

  • dynamic range
  • clipping color values limits
  • resizing, slicing, and display
  • time-series analysis
  • Fourier transforms and information analysis

Physical modeling

  • Ricker wavelet (time-domain)
  • Ricker wavelet (frequency-domain)
  • 2D 3-layer Wedge model
  • 2D 3-layer Wedge model with zoom
  • Amplitude extraction along top of wedge
  • Amplitude extraction along event extrema

Seismic data "oscilloscope"

  • an iPython or .py UI template for create a diagnostic seismic tool
  • map of the surface coordinates
  • spectral plot based on trace header information
  • histogram of the data
  • estimate signal:noise ratio


  • Normal distribution
  • Cumulative distribution
  • Clinoform impedance model


  • Parse an ASCII file
  • Display a log with annotation and units
  • Display cross plot with annotation and units
  • Log editing and correcting
  • Compute a regression
  • Sub-region lasso selection
  • Display selection on multiple plots
  • Vp/Vs vs. acoustic impedance,
  • Lambda-Mu-Rho (LMR) method
  • Rock property templates overlain

Other topics

  • PCA
  • SVD
  • Least squares
  • AVO
  • Cluatering
  • Color and graphics
  • Cartography and GIS

Resources and references