Introduction to geophysical programming using Python

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

Objective
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

IPython
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

PyLab
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

Available variables
List available variables.

Remove user defined variables,

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

Directory Navigation in IPython
To change a directory (note the Unix style forward slashes)

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

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

Function information

 * help using ?
 * show source code using ??

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

Input history
List string from prompt[2]

Output history
Grab previous result

Grab result from prompt[2]

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

Language introduction
A Python file can be used as a script, or as a module, or both
 * 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
 * import Variations
 * basic imports
 * aliasing a name
 * importing specific symbols
 * importing everything
 * packages
 * file structure
 * Setting up PYTHONPATH
 * Reading files
 * Writing files
 * Sorting

Basics

 * 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

Statistics

 * Normal distribution
 * Cumulative distribution
 * Clinoform impedance model

Petrophysics

 * 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

 * Learning Python Learning Python by Mark Lutz
 * Python Books
 * The Code Academy
 * LearnPython.org
 * Object oriented plotting with Matplotlib
 * Nicolas P. Rougier Matplotlib tutorial