Using Python with Climate Data
At the Melbourne Uni follow-up meeting for the 2013 Software Carpentry workshop Damien Irving showed off several Python libraries for working with climate data. Damien's notes are given below, he also has material available on | his blog.
Hi all,
Thanks for coming to the Software Carpentry boot camp follow up session this afternoon. Below is a summary of the text that I showed at the end, including the various links that I promised. If you're struggling with Python at all, feel free to look through my bitbucket repository for useful examples (https://bitbucket.org/DBrent/phd/src). At the 'cdat_tutorial' directory in that repository, you'll see the example code that I used today.
Contents
netCDF
- be CF compliant
- use attributes: data should be self-describing
- more details: http://drclimate.wordpress.com/2013/02/25/are-you-cf-compliant/
ipython
- excellent interactive development environment (IDE)
- use dir() and ? to explore objects
- use pdb for debugging
- use ! to run any command at the system shell
- on abyss: /usr/local/uvcdat/1.2.0rc1/bin/ipython
uvcdat
- for quickly viewing your data
- overview: http://drclimate.wordpress.com/2012/12/19/getting-started-with-python/
- youtube: http://www.youtube.com/watch?v=Nuer6B21NA0
- general cdat documentation: http://www2-pcmdi.llnl.gov/cdat
- on abyss: /usr/local/uvcdat/1.2.0rc1/bin/uvcdat
import cdms2
- for netCDF input and output
- documentation: http://esg.llnl.gov/cdat/cdms_html/cdms.htm
import cdutil
- climate relevant utilities (climatologies, regions, calendars etc)
- documentation: http://esg.llnl.gov/cdat/cdat_utilities/cdat_utilities.htm
import genutil
- climate relevant statistics (correlation, percentiles, etc)
- documentation: http://esg.llnl.gov/cdat/cdat_utilities/cdat_utilities.htm
import regrid2
- for regirdding
import MV2
- for masked arrays
- it's a copy of numpy.ma, but the data keep their attributes
import matplotlib
- for all plotting (timeseries, spatial maps, etc)
- from mpl_toolkits.basemap import Basemap, for spatial maps
- from mpl_toolkits.basemap import shiftgrid, for longitude axis fiddling
- documentation: http://matplotlib.org/
programming tips
- use the argparse module for parsing the command line
- create your own modules to avoid duplication of code
- use docstrings as your primary documentation method
- use pylint to test the quality of your code