Working with Multidimensional Coordinates

Author: Ryan Abernathey

Many datasets have physical coordinates which differ from their logical coordinates. Xarray provides several ways to plot and analyze such datasets.

In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: import xarray as xr

In [4]: import netCDF4

In [5]: import cartopy.crs as ccrs

In [6]: import matplotlib.pyplot as plt

As an example, consider this dataset from the xarray-data repository.

In [7]: ds = xr.tutorial.load_dataset('rasm')
---------------------------------------------------------------------------
ConnectionRefusedError                    Traceback (most recent call last)
/usr/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1317                 h.request(req.get_method(), req.selector, req.data, headers,
-> 1318                           encode_chunked=req.has_header('Transfer-encoding'))
   1319             except OSError as err: # timeout error

/usr/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
   1238         """Send a complete request to the server."""
-> 1239         self._send_request(method, url, body, headers, encode_chunked)
   1240 

/usr/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1284             body = _encode(body, 'body')
-> 1285         self.endheaders(body, encode_chunked=encode_chunked)
   1286 

/usr/lib/python3.6/http/client.py in endheaders(self, message_body, encode_chunked)
   1233             raise CannotSendHeader()
-> 1234         self._send_output(message_body, encode_chunked=encode_chunked)
   1235 

/usr/lib/python3.6/http/client.py in _send_output(self, message_body, encode_chunked)
   1025         del self._buffer[:]
-> 1026         self.send(msg)
   1027 

/usr/lib/python3.6/http/client.py in send(self, data)
    963             if self.auto_open:
--> 964                 self.connect()
    965             else:

/usr/lib/python3.6/http/client.py in connect(self)
   1391 
-> 1392             super().connect()
   1393 

/usr/lib/python3.6/http/client.py in connect(self)
    935         self.sock = self._create_connection(
--> 936             (self.host,self.port), self.timeout, self.source_address)
    937         self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

/usr/lib/python3.6/socket.py in create_connection(address, timeout, source_address)
    721     if err is not None:
--> 722         raise err
    723     else:

/usr/lib/python3.6/socket.py in create_connection(address, timeout, source_address)
    712                 sock.bind(source_address)
--> 713             sock.connect(sa)
    714             return sock

ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

URLError                                  Traceback (most recent call last)
<ipython-input-7-45e669c5b239> in <module>()
----> 1 ds = xr.tutorial.load_dataset('rasm')

/build/python-xarray-vHjScK/python-xarray-0.9.2/xarray/tutorial.py in load_dataset(name, cache, cache_dir, github_url, **kws)
     54 
     55         url = '/'.join((github_url, 'raw', 'master', fullname))
---> 56         _urlretrieve(url, localfile)
     57 
     58     ds = _open_dataset(localfile, **kws).load()

/usr/lib/python3.6/urllib/request.py in urlretrieve(url, filename, reporthook, data)
    246     url_type, path = splittype(url)
    247 
--> 248     with contextlib.closing(urlopen(url, data)) as fp:
    249         headers = fp.info()
    250 

/usr/lib/python3.6/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    221     else:
    222         opener = _opener
--> 223     return opener.open(url, data, timeout)
    224 
    225 def install_opener(opener):

/usr/lib/python3.6/urllib/request.py in open(self, fullurl, data, timeout)
    524             req = meth(req)
    525 
--> 526         response = self._open(req, data)
    527 
    528         # post-process response

/usr/lib/python3.6/urllib/request.py in _open(self, req, data)
    542         protocol = req.type
    543         result = self._call_chain(self.handle_open, protocol, protocol +
--> 544                                   '_open', req)
    545         if result:
    546             return result

/usr/lib/python3.6/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    502         for handler in handlers:
    503             func = getattr(handler, meth_name)
--> 504             result = func(*args)
    505             if result is not None:
    506                 return result

/usr/lib/python3.6/urllib/request.py in https_open(self, req)
   1359         def https_open(self, req):
   1360             return self.do_open(http.client.HTTPSConnection, req,
-> 1361                 context=self._context, check_hostname=self._check_hostname)
   1362 
   1363         https_request = AbstractHTTPHandler.do_request_

/usr/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1318                           encode_chunked=req.has_header('Transfer-encoding'))
   1319             except OSError as err: # timeout error
-> 1320                 raise URLError(err)
   1321             r = h.getresponse()
   1322         except:

URLError: <urlopen error [Errno 111] Connection refused>

In [8]: ds
Out[8]: 
<xarray.Dataset>
Dimensions:         (time: 3, x: 2, y: 2)
Coordinates:
    lat             (x, y) float64 42.25 42.21 42.63 42.59
    lon             (x, y) float64 -99.83 -99.32 -99.79 -99.23
  * time            (time) datetime64[ns] 2014-09-06 2014-09-07 2014-09-08
    reference_time  datetime64[ns] 2014-09-05
    day             (time) int64 6 7 8
Dimensions without coordinates: x, y
Data variables:
    temperature     (x, y, time) float64 11.04 23.57 20.77 9.346 6.683 17.17 ...
    precipitation   (x, y, time) float64 5.904 2.453 3.404 9.847 9.195 ...

In this example, the logical coordinates are x and y, while the physical coordinates are xc and yc, which represent the latitudes and longitude of the data.

In [9]: ds.xc.attrs
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-824248f2223e> in <module>()
----> 1 ds.xc.attrs

/build/python-xarray-vHjScK/python-xarray-0.9.2/xarray/core/common.py in __getattr__(self, name)
    166                     return source[name]
    167         raise AttributeError("%r object has no attribute %r" %
--> 168                              (type(self).__name__, name))
    169 
    170     def __setattr__(self, name, value):

AttributeError: 'Dataset' object has no attribute 'xc'

In [10]: ds.yc.attrs
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-faf38965074d> in <module>()
----> 1 ds.yc.attrs

/build/python-xarray-vHjScK/python-xarray-0.9.2/xarray/core/common.py in __getattr__(self, name)
    166                     return source[name]
    167         raise AttributeError("%r object has no attribute %r" %
--> 168                              (type(self).__name__, name))
    169 
    170     def __setattr__(self, name, value):

AttributeError: 'Dataset' object has no attribute 'yc'

Plotting

Let’s examine these coordinate variables by plotting them.

In [11]: fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(9,3))

In [12]: ds.xc.plot(ax=ax1);

In [13]: ds.yc.plot(ax=ax2);
../_images/xarray_multidimensional_coords_8_2.png

Note that the variables xc (longitude) and yc (latitude) are two-dimensional scalar fields.

If we try to plot the data variable Tair, by default we get the logical coordinates.

In [14]: ds.Tair[0].plot();
../_images/xarray_multidimensional_coords_10_1.png

In order to visualize the data on a conventional latitude-longitude grid, we can take advantage of xarray’s ability to apply cartopy map projections.

In [15]: plt.figure(figsize=(7,2));

In [16]: ax = plt.axes(projection=ccrs.PlateCarree());

In [17]: ds.Tair[0].plot.pcolormesh(ax=ax, transform=ccrs.PlateCarree(),
   ....:                            x='xc', y='yc', add_colorbar=False);
   ....: 

In [18]: ax.coastlines();

In [19]: plt.tight_layout();
examples/../_build/html/_static/xarray_multidimensional_coords_12_0.png

Multidimensional Groupby

The above example allowed us to visualize the data on a regular latitude-longitude grid. But what if we want to do a calculation that involves grouping over one of these physical coordinates (rather than the logical coordinates), for example, calculating the mean temperature at each latitude. This can be achieved using xarray’s groupby function, which accepts multidimensional variables. By default, groupby will use every unique value in the variable, which is probably not what we want. Instead, we can use the groupby_bins function to specify the output coordinates of the group.

# define two-degree wide latitude bins
In [20]: lat_bins = np.arange(0, 91, 2)

# define a label for each bin corresponding to the central latitude
In [21]: lat_center = np.arange(1, 90, 2)

# group according to those bins and take the mean
In [22]: Tair_lat_mean = ds.Tair.groupby_bins('xc', lat_bins, labels=lat_center).mean()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-22-83b74688111e> in <module>()
----> 1 Tair_lat_mean = ds.Tair.groupby_bins('xc', lat_bins, labels=lat_center).mean()

/build/python-xarray-vHjScK/python-xarray-0.9.2/xarray/core/common.py in __getattr__(self, name)
    166                     return source[name]
    167         raise AttributeError("%r object has no attribute %r" %
--> 168                              (type(self).__name__, name))
    169 
    170     def __setattr__(self, name, value):

AttributeError: 'Dataset' object has no attribute 'Tair'

# plot the result
In [23]: Tair_lat_mean.plot();
../_images/xarray_multidimensional_coords_14_1.png

Note that the resulting coordinate for the groupby_bins operation got the _bins suffix appended: xc_bins. This help us distinguish it from the original multidimensional variable xc.