In [2]:
# Pay no attention to this cell
# All will be revealed in due time.
import pandas as pd
from IPython.display import Image
syllabus.index = range(1,len(syllabus)+1)

Python for Earth Science Students

Authors: Lisa Tauxe, [email protected] & Hanna Asefaw, [email protected]

How to take the class

  • Read each lecture in sequence. They build up progressively
  • Work the Practice Problems in the Practice_Problems directory
  • Do the assignements. If you want solutions, contact [email protected]

Installing Python

Downloading lectures.

  • If you are viewing this lecture in nbviewer on github, you can download this or any notebook by clicking on the "inbox" tray to the upper right of your browser window
  • To install the whole package, go to this link: and "clone the repository.
  • Click on the “Clone or download” green button and choose the “Download ZIP” option. Double click on the file “” to unpack the folder.
  • Open a terminal window:
    • If you are using the MacOS operating system, look for the Terminal application in the Utilities folder within the Applications folder.
    • On a PC look for 'cmd'
    • When the application is launched, you will get a "terminal window". The Unix (and MacOS) Bash shell has a $ sign as a prompt. Other shells have other command line prompts, such as the antiquated ‘C-shell’ '\%'. On PCs you'll have some carets.
  • In the terminal window, download a few non-default Python packages needed for this class (in later lectures). Type the following after the prompt (followed by a return or Enter key):

    conda install future
    conda install basemap --channel conda-forge

  • Then type:

    jupyter notebook

This will open a page with a directory listing of your home directory. Navigate to where you put this class (Python-for-Earth-Science-Students-master).

  • Click on Lecture_01_syllabus.ipynb

Then you will be here! only for real! Now read this lecture to find out how to use notebooks.

Computers in Earth Science

Computers are essential to all modern Earth Science research. We use them for compiling and analyzing data, preparing illustrations like maps or data plots, writing manuscripts, and so on. In this class, you will learn to write computer programs with special applications useful to Earth Scientists. We will learn Python, an object-oriented programming language, and use jupyter notebooks to write our Python programs.


So, why learn Python?

  • Flexible, freely available, cross platform
  • Easier to learn than many other languages
  • It has many numerical, statistical and visualization packages
  • It is well supported and has lots of online documentation
  • The name 'Python' refers to 'Monty Python' - not the snake - and many examples in the Python documentation use jokes from the old Monty Python skits. If you have never heard of Monty Python, look it up on youtube; you are in for a treat.

  • Python is undergoing a transition from 2.7 to 3. The notebooks in this series, apart from a few exceptions, are compatible with both.

In [3]:
Topic Application
1 Intro to the class
2 Variables and Operations
3 Data structures
4 Dictionaries, program loops (if, while and for)
5 functions and modules
6 NumPy and matplotlib seismic record
7 NumPy arrays
8 file systems and paths
9 Pandas, file I/O P-S wave arrival times
10 object oriented programming objects and classes
11 recursions and exceptions fibonacci spiral
12 lambda, map, filter reduce, list comprehension
13 data wrangling with Pandas seismic travel time plots
14 subplots, bar charts pie charts elemental abundances
15 histograms and cumulative distribution functions hypsometric curve
16 statistics 101 Univariate data
17 hypothesis testing t, F
18 application to grain sizes grain sizes
19 line and curve fitting Bivariate data & Hubble plot
20 maps spatial data; earthquake locations/ depths
21 gridding and contouring IGRF
22 rose diagrams and equal area projections glacial striations
23 matrix math - dot and cross products poles to planes and more
24 plotting great and small circles
25 3D plots of points and surfaces benioff zone
26 Time series - periodograms temporal data
27 Animations Indian plate motion

Jupyter notebooks

This class is entirely structured around a special programming environment called jupyter notebooks. A Jupyter notebook is a development environment where you can write, debug, and execute your programs.

Jupyter notebook anatomy

Jupyter notebooks have two basic cells:

  • Markdown: for typesetting notes. This cell is an example of a markdown cell.

  • Code: for writing python code

You can insert a new cell by clicking on the Insert dropdown menu and selectin "Insert Cell Below":

In [4]:

You change the cell "flavor" with the menu that defaults to 'Code' and can be changed to "Markdown".

And you "execute" a cell (either typeset or run the code) by clicking on the run key (sideways triangle with vertical line) or select Run Cells under the Cell drop-down menu.

In [4]:

Practice with inserting cells

Insert a cell below this one. Change it to ‘Markdown’ and type some notes. You can just type most things, but special features like Section headers, bullets, numbered lists and other fun things require special formatting. For a pretty good explanation see this link:

Run the cell to typeset your notes. If you want to change them, just double click on the cell, type away, then 'run' it again.

Practice with markdown cells

If you see plain text in one of the cells in the lecture notebooks, just double click on it and all will be revealed. Try double clicking on THIS cell to see how to make section headers - then run it.

Practice with code cells

Insert another cell, keeping it the default of Code.

type: print("Hello World") in the cell and then run that cell.

It should look like this:

In [5]:
print ("Hello World")
Hello World

Congratulations! That was your first Python program.

In a code block, you can only type valid python statements EXCEPT after a pound sign - everything after that will be ignored.
That is how you write "comments" in your code to remind yourself or tell others what you were thinking:

In [6]:
# I can type anything here
but not here
  File "<ipython-input-6-ae00b29ef7b9>", line 2
    but not here
SyntaxError: invalid syntax

That was an example of a bug which oculd be fixed by commenting out the second line, or making it a valid statement:

In [7]:
# I can type anything here
# but not here
print ("but not here")
but not here

Creating new notebooks, naming and saving.

To open a new notebook, pull down the 'File' menu and select the 'New Notebook => Python 3' option. You can 'tear' the notebook off so it is in a separate window if you like seeing both together.

To rename the notebook, choose 'Rename' under the File menu.

To save it, click on the disk icon on the menu bar. Wait until the 'checkpoint created...' message disappears before you try to exit the notebook.

Splitting your screen

Click on the green button in the top left corner of your browser and drag it to the side you want your lecture in. Click your "notes" notebook and it will be placed on the other side.

We will learn much more about markdown and code blocks in the coming weeks as you begin to understand how to write programs.

Practice with notebooks

Open a new notebook, rename it to, for example, 'Lecture_01_notes', split your screen if you like, make a Markdown cell, write some notes, and save the file. Now look in your folder and see your brand new notebook!