Utf-8'en'waite Cider 2019 Tutorial

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Preparing for volcano geophysics tutorial

1. Install either anaconda or miniconda


2. Install obspy via conda
3. Install additional packages
4. Try the jupyter notebooks:
1. 01_python_basics.ipynb
2. 02_python_basic_numpy.ipynb
3. 03_python_basic_plotting.ipynb
4. 04_programming_control.ipynb
5. 05_intro_2_obspy.ipynb
Intro to python – Key Advantages:
Open and Free
• Python is interpretive
• No compiling of programs (unlike C, C++, or Fortran)
• Means the python interpreter executes one line at a time (like Matlab)
• Python has many available open-source packages/modules
• Packages include scripts/functions that perform operations
• For example, obspy (http://obspy.org) has extensive functions written for
seismology
• Python works on any platform (Windows, Linux, Mac)
• Can be used on command line, in Matlab-like Spyder application, or
Jupyter Notebooks, which are great for teaching
Python disadvantages?
• Somewhat steep learning curve
• Not as easy to use initially as some other interpretive languages (Matlab)
• Must get used to command-line functions
• Not as fast for complex processing as well-written compiled codes in
C++ or Fortran
• External codes can be linked from within python to take advantage of speed
• Somewhat complicated to install, keep track of all the packages
• Package managers and environments make this easier
Python packages and environments
• Packages often depend on other packages
• E.g., function dothis, which is in package A1 is referenced from another
function, dothat, which is in the package B2.
• Getting all the right packages installed can be a real hassle, but is made easier
using package managers, like conda: (https://www.anaconda.com/)
• Environments allow for groups of packages to be installed where they
won’t interfere with system python setup
• Can create environments with different python versions for certain
applications, or different versions of packages
Anaconda (3Gb) or miniconda (1Gb)
• Installation with Anaconda takes a lot of space, but includes lots of
useful features (along with some you may never use)
• It’s a little simpler to get set up with Anaconda, especially on
Windows
• Miniconda is bare bones; anything you do with anaconda can be
done, but may just take a little more set up
• If you are using Windows and have the disk space, use anaconda
• Although you probably already have python installed on your system,
this conda installation will be independent and won’t corrupt
anything that already exists
Windows Installation with conda and Anaconda
• Install Anaconda: https://www.anaconda.com/download/
• You want 3.7 and 64 bit
• Install to your C: drive (should be the default)
• Then open the Anaconda Prompt from the start menu
• This will open a black window with command line.
Mac or Linux Installation of Anaconda
• Install Anaconda: https://www.anaconda.com/download/
• You want 3.7 and 64 bit
• Follow the instructions to make sure it installs properly so the
path to the commands is set
• Or install miniconda from
https://docs.conda.io/en/latest/miniconda.html
• You can watch my little screen capture of the installation on a
mac: https://youtu.be/W4UOHzTno1g
• With either Anaconda or Miniconda installed…
• Then open a terminal window
• If the prompt begins with (base), type conda deactivate
• The next sequence of commands will
1. create a new environment for obspy and
2. install obspy and all the dependencies
Installation of obspy with conda
On the command line (anaconda prompt in windows, or terminal window in linux or macos), type the following
conda config --add channels conda-forge
• The above command sets conda to install packages from a certain place (conda-forge.org). The next command creates an
environment called obspy (but you can call it just about anything) with python version 3.7.
conda create -n obspy python=3.7
• Next you will activate that newly created environment
conda activate obspy
• You will notice that the command line prompt now has the name of the environment in parentheses (obspy). Now install obspy, and
all the packages it depends on:
conda install obspy
• conda will look for the obspy package at conda-forge, download it, and install it. You will be prompted a couple times for confirmation
of download/install.
• You have installed the obspy package, but there are a few more things that are useful to add. This one adds functionality to jupyter
notebook to allow for environment switching
conda install nb_conda
• This should already be installed, but check.
conda install basemap
• This adds more plotting functions.
conda install ipympl
• And, if you installed with miniconda instead of anaconda (probably unnecessary if you installed nb_conda first).
conda install jupyter
Using jupyter notebook
• Jupyter notebook is a web-browser tool that allows you to create
python scripts and run then in an interactive mode. It a bit easier to
use than the command line in python so lets move to that
• To start this up
1. open Anacoda Prompt (windows) or terminal/xterm (osx or linux)
2. activate the obspy environment on the command line
3. then start jupyter notebook from the command line

4. This will open a new browser window with the jupyter notebook
5. Now you can open a new notebook or an existing one (they have file
extensions (.ipynb for ipython notebook)
6. From the jupyter notebook browser, navigate to, and open the file:
01_python_basics.ipynb
Important things to remember
• You can update packages with conda, e.g.:
conda update obspy
• You should use a new environment for different software packages.
• If you use python for something else, you should create a new environment and
install what you need for that purpose there.
• Some packages require different versions of dependencies so things can get
corrupted if you install too many things in one place
• You can have different environments with different versions of python for
different uses
• For example, if a package is not supported in python 3.7, you can create an
environment that uses python 3.6 (or 3.5 or whatever) and run the package in that
environment
• Always start your obspy sessions the same way:
1. open Anacoda Prompt if on windows
2. activate the obspy environment
3. then start jupyter notebook or ipython or…(we will get into this)
more useful things to know
• In python scripts (or ipython or jupyter notebooks), line indents have
meaning
• Some things that work in ipython will not work in python
• Some useful shortcuts in jupyter:
• shift + return executes a cell and moves to next
• CNTL + return executes a cell
• indent selected lines with CNTRL + ]
• reduce indent using CNTRL + [
• CNTRL + / toggles between commented and uncommented

You might also like