In [1]:
name = '2015-11-13-meeting-summary'
title = '13 November Meeting Summary'
tags = 'anaconda, hpc, netcdf, xray'
author = 'Denis Sergeev'

In [3]:
from nb_tools import connect_notebook_to_post
from IPython.core.display import HTML

html = connect_notebook_to_post(name, title, tags, author)

Today was quite an informal meeting and since all the attendants were beginners to Python, we talked about basics.

Launching Python on Grace using Anaconda

About a half of people that completed the survey use Python on a remote server and today there were questions about it too. Below is the essential set of commands to create your own Python ecosystem on Grace using Anaconda software.

<div class="alert alert-warning", style="font-size: 120%"> The instructions below have been summarised in a script which is available here: grace-python. </div>

Login to Grace and load Anaconda module

After you log in to Grace, you should load the Anaconda module by typing the following command:

module load python/anaconda/2.3.0

Note that I'm using Python 2 here, but Anaconda with Python 3 is also available on Grace (you can find that module using the command module avail)

Create your environment

To be able to manage your Python distribution without calling IT, it's better to create an Anaconda environment in your home directory:

conda create -n myenv python anaconda

where myenv is just a name of the environment (could be anything, such as my_awesome_environment). Now, typing

conda list

will print out the environments available to you. In my case the result was

myenv /gpfs/home/abc12xyz/.conda/envs/myenv

root * /gpfs/grace/anaconda/2.3.0

Switch to your newly created environment

source activate myenv

This command appended the correct path to the system path.

(!) One thing that you should do is to clear standard environment variables:

unset PYTHONHOME PYTHONPATH

If you decided to stick with Anaconda, put the previous line in your .bashrc file.

Your Anaconda ecosystem is ready to use. Among many cool commands you can use conda install package_name to install a package.

Example

To install xray: conda install xray

To install iris from SciTools channel (because iris is not in the default conda channel): conda install -c scitools iris

To test it, launch python (or better, ipython) and type import package_name.

Python basics: getting familiar with numpy, scipy

  • Scipy Lectures - a short and very good tutorial with many useful examples
  • Matlab users can find a lot of useful tips here: Numpy for Matlab users
  • Stick to standard aliases for imported modules and sub-modules, such as:

In [3]:
import matplotlib.pyplot as plt
import numpy as np

Opening a NetCDF file

We had a brief look at two packages that are widely used to work with data in NetCDF format: netcdf4 and xray.

netcdf4

Import the module (note the upper case)


In [4]:
import netCDF4 as nc

Open the file as a Dataset:


In [5]:
ds = nc.Dataset('../data/data.nc')

Print all variables:


In [6]:
ds.variables.keys()


Out[6]:
[u'longitude', u'latitude', u'level', u'time', u'w', u'vo', u'd', u'u', u'v']

Interrogate the dimensions of the dataset:


In [7]:
ds.dimensions.keys()


Out[7]:
[u'longitude', u'latitude', u'level', u'time']

Get values of u variable:


In [8]:
u = ds.variables['u']
print('Name of u: {}'.format(u.long_name))
print('Units of u: {}'.format(u.units))
print('Shape of u: {}'.format(u.shape))
print('First element of u array: {}'.format(u[0,0,0,0]))


Name of u: U component of wind
Units of u: m s**-1
Shape of u: (2, 4, 241, 480)
First element of u array: 12.1834030015

xray


In [9]:
import xray

Open the same file as a xray Dataset:


In [10]:
xds = xray.open_dataset('../data/data.nc')

Neatly print out all the dataset info:


In [11]:
print(xds)


<xray.Dataset>
Dimensions:    (latitude: 241, level: 4, longitude: 480, time: 2)
Coordinates:
  * longitude  (longitude) float32 0.0 0.75 1.5 2.25 3.0 3.75 4.5 5.25 6.0 ...
  * latitude   (latitude) float32 90.0 89.25 88.5 87.75 87.0 86.25 85.5 ...
  * level      (level) int32 700 800 900 1000
  * time       (time) datetime64[ns] 2015-01-31 2015-01-31T12:00:00
Data variables:
    w          (time, level, latitude, longitude) float64 -0.0713 -0.0713 ...
    vo         (time, level, latitude, longitude) float64 -6.332e-06 ...
    d          (time, level, latitude, longitude) float64 -4.835e-06 ...
    u          (time, level, latitude, longitude) float64 12.18 12.23 12.27 ...
    v          (time, level, latitude, longitude) float64 3.379 3.219 3.059 ...
Attributes:
    Conventions: CF-1.0
    history: 2015-04-26 19:20:07 GMT by grib_to_netcdf-1.13.1: grib_to_netcdf /data/data04/scratch/netcdf-atls03-a562cefde8a29a7288fa0b8b7f9413f7-Msj0Ul.target -o /data/data04/scratch/netcdf-atls03-a562cefde8a29a7288fa0b8b7f9413f7-V6ScBM.nc -utime

Select a variable and get its metadata:


In [12]:
u = xds.u
print(u)


<xray.DataArray 'u' (time: 2, level: 4, latitude: 241, longitude: 480)>
[925440 values with dtype=float64]
Coordinates:
  * latitude   (latitude) float32 90.0 89.25 88.5 87.75 87.0 86.25 85.5 ...
  * time       (time) datetime64[ns] 2015-01-31 2015-01-31T12:00:00
  * longitude  (longitude) float32 0.0 0.75 1.5 2.25 3.0 3.75 4.5 5.25 6.0 ...
  * level      (level) int32 700 800 900 1000
Attributes:
    units: m s**-1
    long_name: U component of wind
    standard_name: eastward_wind

Question about interpolation

During today's meeting a question was asked about interpolating gridded data (2D) to a finer grid. Luckily, there is a scipy function with a simple example:

scipy.interpolate.interp2d


In [13]:
HTML(html)


Out[13]:

This post was written as an IPython (Jupyter) notebook. You can view or download it using nbviewer.