Introduction to Python: Difference between revisions

From CSDMS
(→‎Introduction: open-source)
(→‎Downloading and Installing: Basic Linux, plus more stuff to install)
Line 14: Line 14:
=== Downloading and Installing ===
=== Downloading and Installing ===


We will be using Python 2.7, the Scipy/Numpy package that includes tools for many common scientific and numerical applications and n-dimensional arrays, and the Matplotlib package that comprises a range of plotting tools.
We will be using Python 2.7, the Scipy and Numpy packages that include tools for many common scientific and numerical applications and ''n''-dimensional arrays, and the Matplotlib package that comprises a range of plotting tools.
 
I also highly recommend:
* ''iPython'', a high-quality python shell (terminal window with syntax highlighting, tab-completion, etc.)
* A nice text editor that does syntax highlighting
 
In addition, some colleagues of mine really like Spyder, which is a all-inclusive interface to edit and execute Python code, and is reminiscent of Matlab.
 
Here I will provide instructions to install these on the different major operating systems:


==== Windows ====
==== Windows ====
Line 22: Line 30:
==== Linux ====
==== Linux ====


package manager
In Linux, the easiest way to install Python and its packages is with the package manager. This, as you probably already know, installs the required tools and all of their dependencies (programs that must be installed in order that the tool that you want to use works properly).
 
<syntaxhighlight lang=bash>
sudo apt-get install python2.7 python-numpy python-scipy python-matplotlib
</syntaxhighlight>


=== Helpful tools and information ===
=== Helpful tools and information ===

Revision as of 23:16, 22 August 2012

Introduction

At the 2012 NCED Summer Institute in Earth Surface Dynamics (SIESD), we realized that there is a large community desire to transition to open source programming languages and tools, but that a roadblock is the lack of a coherent set of information to allow individual scientists to act on these desires without needing to invest a large amount of time. Here I will introduce programming in Python, a fairly intuitive open-source interpreted programming language, and some basic finite difference numerical methods through a simple example of a linear-diffusive hillslope.

Python

What is it?

Python is a general-purpose, high-level, interpreted programming language. This means that:

  • General-purpose: You can use Python for numerical analysis, mapping and GIS, web design, interfacing with computer hardware, plotting and graphics, database management, image processing, and certainly lots of additional applications that scientists do not often have need to use. These applications are embodied in specific modules that can be imported into your Python script.
  • High-level: Python uses array operations and pre-constructed functions to allow you to write your code in many fewer lines than would be required in a low-level language like C.
  • Interpreted: Some languages use a compiler to convert what you write into bytecode that can be interpreted by the computer. Python is "interpreted", meaning that it calls pre-compiled chunks of code that are combined to run the program. It is easier and faster, but can be slower for computationally-intensive applications. However, your own compiled C code can be included in Python scripts for these applications, and the ease of use is a huge advantage!

Downloading and Installing

We will be using Python 2.7, the Scipy and Numpy packages that include tools for many common scientific and numerical applications and n-dimensional arrays, and the Matplotlib package that comprises a range of plotting tools.

I also highly recommend:

  • iPython, a high-quality python shell (terminal window with syntax highlighting, tab-completion, etc.)
  • A nice text editor that does syntax highlighting

In addition, some colleagues of mine really like Spyder, which is a all-inclusive interface to edit and execute Python code, and is reminiscent of Matlab.

Here I will provide instructions to install these on the different major operating systems:

Windows

Mac

Linux

In Linux, the easiest way to install Python and its packages is with the package manager. This, as you probably already know, installs the required tools and all of their dependencies (programs that must be installed in order that the tool that you want to use works properly).

sudo apt-get install python2.7 python-numpy python-scipy python-matplotlib

Helpful tools and information

  • iPython
  • Spyder
  • Mathesaurus Matlab-Numpy

How to Write a Model in Python

The source code

First, I will write the whole source code. Then I will break it down into sections where I explain each component. Then I will present a new model that uses an implicit matrix-style solution instead of a foward-time-stepping solution, and write about numerical (finite difference) methods.

#! /usr/bin/python

# Import modules
import numpy as np
from matplotlib import pyplot as plt

# Center of hill is at 0, two incising channels at steady rate at edges
# no channel-hillslope feedback and assuming entire hill is made of mobile 
# regolith

# Set up domain
dx = 10 # [m]
xmax = 500 # [m]
x = np.arange(-xmax, xmax + dx/10., dx) # [m], +dx/10. to make sure that edges are included

# Set up river channel boundary condition
zdot_channel = 2E-4 # [m], 0.2 mm/yr boundary incision

# Rate of soil motion is proportional to slope of hill
k = 1E-5 # Slope -- soil flux scaling
# Something with S here

# Put into continuity equation to get linear diffusion equation - assuming
# k constant across hill
# 
# 

# Run


# Plot

Components

The "Shebang"

This script starts with:

#! /usr/bin/python

This is a "shebang" that tells the program where to look for the Python interpreter on Unix-like systems (the most popular being Mac and Linux, the former more popular for desktop applications and the latter more popular for supercomputers).