Python for Climate Science
1. Introduction
- Python tutorials on the web
- General and more advanced Python references
- python.org., providing link to many Python resources
- Paul Dubois' indispensible documentation of the Numeric and Masked Array modules
- Books on Python
2. Use of the basic constructs:Programming Python with Style and Flair
- Modules and the Import statement
- Fundamental role of modules
- Every Python script is a module
- Store your functions in a module! It's not like Matlab.
- from ... import *. import ... as ...
- Name-spaces. Nested modules
- Example: A table of physical constants
- Getting information about modules and objects: dir( ) and help( )
- Lists,oh, the things you can do with lists!
- Building lists using append()
- Building lists using functions
- Useful list operations (methods)
- Accessing an element or range from a list
- Loops: for,while,continue,break
- Looping over lists
- The range( ) and xrange( ) statements
- Define lots of functions
- Function definition and the argument list
- A warning on side-affecting arguments
- Program Development Techniques
- Use modules to organize objects and functions
- Use Idle, with a scratchpad for new functions/classes
- Use the interpreter to test functions
- Ways to run Python scripts
- From inside interpreter
- Import a module (not recommended)
- Import a module then invoke functions
- Using the execute statement
- From the command line
- Use the Python statement
- The pound-bang hack
- A warning on reference vs. identity
- Strings, oh the things you can do with strings!
- String operations and methods
- Search,replace,split, etc.
- Using format strings
- Getting a fixed length field ('%0<n>i'%N)
- The string module. string.atoi( ), etc.
- Newline, <cr> and tab codes
- Objects and their uses
- Fundamental importance of the index methods __getitem( )__ and __setitem( )__
- Input and output
- Input from the terminal: raw_input( )
- Files
- read( ),readline( ),readlines( )
- write( )
- seek( )
- Example: Processing a space-delimited ascii data file
- Example: Converting a Unix text file to Mac format crfix1.py
- Interaction with c files: the struct module
- A note on Fortran files
- Dictionaries and their uses
- Tip: Dictionaries are nice to use with __getitem( )__ and __setitem( )
- Exception handling
- try/except blocks
- raise (Also use raise to bail out of a loop or script)
- use of assert statement in debugging
3. Python as a scripting language
- Your friend, the os module
- Generic directory and path commands
- glob.glob( )
- Executing arbitrary system commands os.system( )
- Example: Pruning restart files.
- Example: Converting a directory of history files to netCDF
4. Python for simulation
- A few examples in unadorned Python
- Chaos in the Logistic map: The Logistic Map Explorer class
- A 1-dimensional Runge-Kutta solver, with applications
- Orbits of the planets
- Introduction to Numeric arrays
- Example: The 1D diffusion equation
- The model--Explicit stepping version
- The model--Implicit version
- A more interesting boundary condition: Radiative cooling from the top
- A simple sea-ice model
- Hey there Snoopy, don't be loopy! A short polemic on program design. Reasons for writing code in a "non-loopy" way. (Efficiency, but it also makes it easier to do automatic parallelization, since high-level arithmetic on objects is a signal of parallelism. Also encourages dealing with higher-levels of abstractions, making code more readable and maintainable.)
- Use of Masked arrays
- Extending Python with c and SWIG
- Introduction to SWIG
- A note on building Swigged modules under Mac OSX (Darwin)
- Finding the right level for building objects
- Global variables
- Interacting with c arrays
- Interacting with c structures
- Example 1:
- Example 2: A simple float array extension
- Example 3: The diffusion equation
- Working with Fortran
- General remarks on c and Fortran interaction
- Example: Writing a c stub for a Fortran routine
- Dealing with Fortran arrays
- Dealing with Fortran common blocks
- Manipulating Numeric arrays with Fortran or c
5. Python for Data Analysis
- The cdms module
- Overview
- Variables,file variables, and Masked Variables
- Reading and writing netCDF files
- About axes
- Getting information about axes
- Specifying a variable's axes
- Creating a new axis
- Averaging over an axis
- Plotting data using vcs (needs Python 2.2)
- Making a basic contour plot.
- Customization: Continents, contour values,etc
- Choosing a plot method (isofill, etc)
- Map projections
- Line plots
- Saving plots to a graphics file
- Plotting data using ncl scripts