Jupyter notebook ATMS-305-Content/Week-8/Week 8 Handouts/Mar 8 in-class exercise.ipynb
ATMS 305 : March 8, 2017
Data visualization using xarray and matplotlib
In the last exercise, you learned how to use matplotlib
to visualize data. In this exercise, we will put it all together:
read files
subset files using fancy indexing
plot the results
We're going to use a new python package called xarray
. It is a high level interface for netCDF file reading and writing, that will make your life easier. Let's go!
Did you get this this error?
Phooey! What do we do now?
Well, we can install a new package using pip
, the python package manager. Many packages can be installed in this way.
Open a terminal and enter
When it is done, restart the kernel and try again.
Ahhhhhhhh....all better. Let's get to work!
Using xarray to read a netCDF file
It can't be much easier than this...xarray handles a lot of the dirty work for you. We can load both local files, as well as files on the internet. Either give the local file path, or the web site!
This data is a gridded time series at 2 degree resolution of monthly surface temperature anomalies starting in 1880. Let's average over all space dimensions (lat - axis 1, and lon - axis 2). We can use np.mean
and its axis
keyword (very handy) for this purpose.
This will yield a time series of globally averaged temperature!
We can use the select tool to get a subset in a box (find closest index values of lon and lat) so that we can subset the data and grab the closest point to Champaign-Urbana. We can give it a list of points. Here we will give it one.
Saving to file - easy as np.pi()
Want to save the file as a netCDF file? No problem!
Calculating time averages
Let's say we want to average the monthly time series data into annually averaged data.
There are a number of ways to do this. xarray
offers time sampling capabilities, similar to pandas
. First though, let's do it the hard way.
It looks like we don't have an evenly divisible number of months, so we don't have complete years. Let's start at the beginning and loop by 12.
Save time series to a new file, just Champaign.
Read it back in to check!
Now the easy way. We xarray
and pandas
share the same interface to resample and group time series conveniently. The documentation is available at: http://xarray.pydata.org/en/stable/time-series.html#resampling-and-grouped-operations. The codes for resampling are the same as pandas
. See http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases
How easy is that? np.pi()? You can also resample with other time frequencies, or in space, or change how you do the calculation (i.e., calculate the median instead of the mean).
Now save to a file:
Make a (nice) plot!
Introduction to mapping with basemap
.
Python's basemap
is a package that is designed to enable matplotlib
handle geographic calculations and visualize maps with geographic boundaries. All we need is an array with a 2-D variable, and associated latitudes and longitudes, and off we go.
Zoom in, use a different map projection.