Introduction to geophysical programming using Python
This page supports a course on beginning Python programming for geoscientists, taught by Evan Bianco.
- 1 Objective
- 2 What is Python?
- 3 IPython
- 4 PyLab
- 5 IPython notebook
- 6 IPython
- 7 Basics
- 8 NumPy and SciPy
- 9 Dealing with SEGY
- 10 Physical modeling
- 11 Seismic data "oscilloscope"
- 12 Statistics
- 13 Petrophysics
- 14 Other topics
- 15 Resources and references
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.
- Automatic garbage collection
- Dynamic typing
- Interpreted and interactive
- "Batteries included"
- 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.
- .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 : a=1 In : a Out: 1
In : b = [1,2,3]
List available variables.
In : %whos Variable Type Data/Info a int 1 b list n=3
Remove user defined variables,
In: %reset In: %whos Interactive namespace is empty.
"%reset also remove the names imported into PyLab, such as the plot command.
In : plot NameError: name 'plot' is not defined <source> Reload pylab <source lang=python> In: %pylab Welcome to pylab,...
To change a directory (note the Unix style forward slashes)
In: cd c:/python_class/Demos/Seismic c:/python_class/Demos/Seismic
Tab completion helps you find and type directory and file names.
List directory contents (Unix style, not "dir")
In:ls 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 speed_of_sound.py 09/01/2008 02:48 PM 1,232 speed_of_ground2.py 2,874,278 bytes 6 File(s) 2 Dir(s) 11,997,437,952 bytes free
Running Scripts in IPython
- help using ?
- show source code using ??
List previous commands
Use 'magic' % because 'hist' is the histogram function in pylab
In : %hist a = 1 a
List string from prompt
In: _i2 Out: 'a/n'
Grab previous result
In: _ Out: 'a/n'
Grab result from prompt
In : _2 Out: 1
Also, use the up and down arrows to scroll through your iPython input history.
Reading simple tracebacks
- Interactive Calculator
- Strings and string methods
- String formatting
- Lists and list methods
- Indexing lists
- Slicing lists
- Tuples (Mutable vs Immutable)
- 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
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
- file structure
- Setting up PYTHONPATH
- Reading files
- Writing files
NumPy and SciPy
- simple plotting
- simple array math
- 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
- 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
- Least squares
- Color and graphics
- Cartography and GIS
Resources and references
- Learning Python Learning Python by Mark Lutz
- Python Books
- The Code Academy
- Object oriented plotting with Matplotlib
- Nicolas P. Rougier Matplotlib tutorial