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=pd.read_csv('syllabus.csv',header=0)
syllabus=syllabus.fillna("")
syllabus.index = range(1,len(syllabus)+1)

Python for Earth Science Students

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

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, click on the little cat (github logo) and clone the repository.
  • Read the rest of this notebook to learn how to use them.
In [3]:
Image('Figures/nbviewer.png')
Out[3]:

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.

Python

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.

Which Python?

  • Python is undergoing a transition from 2.7 to 3. The notebooks in this series, apart from a few exceptions, are compatible with both.
  • Install the most recent version of Anaconda python for your operating system : https://www.anaconda.com/download/
In [3]:
syllabus[['Topic','Application']]
Out[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. Put all the lectures and other directories associated with this class into a folder on your computer. To launch a notebook, follow the instructions on this website:

jupyter-notebook-beginner-guide.readthedocs.io/en/latest/execute.html

Launch Lecture_01_syllabus.ipynb

You should now be looking at this notebook.

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 selecting Insert Cell Below in the drop-down menu:

In [3]:
Image(filename='Figures/insertCell.png')
Out[3]:

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]:
Image(filename='Figures/menuBar.png')
Out[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: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet

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!