Note

This tutorial was generated from an IPython notebook that can be downloaded here.

Create xarray region mask

In this tutorial we will show how to create a mask for arbitrary latitude and longitude grids using xarray. It is very similar to the tutorial Create Mask (numpy).

Import regionmask and check the version:

'0.2.0'

Load xarray and the tutorial data:

The example data is a temperature field over North America. Let’s plot the first time step:

../../_images/mask_xarray_8_0.png

Conviniently we can directly pass an xarray object to the mask function. It gets the longitude and latitude from the DataArray/ Dataset and creates the mask. If the longituda and latitude in the xarray object are not called lon and lat, respectively; their name can be given via the lon_name and lat_name keyword. Here we use the Giorgi regions.

All elements of mask are NaN. Try to set 'wrap_lon=True'.
All NaN?  True

This didn’t work - all elements are NaNs! The reason is that airtemps has its longitude from 0 to 360 while the Giorgi regions are defined as -180 to 180. Thus we can provide the wrap_lon keyword:

All NaN?  False

This is better. Let’s plot the regions:

../../_images/mask_xarray_14_0.png

We want to select the region ‘Central North America’. Thus we first need to find out which number this is:

6

Select using where

xarray provides the handy where function:

Check everything went well by repeating the first plot with the selected region:

../../_images/mask_xarray_21_0.png

Looks good - let’s take the area average and plot the time series. (Note: you should use cos(lat) weights to correctly calculate an area average. Unfortunately this is not yet (as of version 0.7) implemented in xarray.)

../../_images/mask_xarray_23_0.png

Select using groupby

'0.8.2'
<xarray.Dataset>
Dimensions:  (region: 6, time: 2920)
Coordinates:
  * time     (time) datetime64[ns] 2013-01-01 2013-01-01T06:00:00 ...
  * region   (region) float64 4.0 5.0 6.0 7.0 8.0 9.0
Data variables:
    air      (region, time) float64 293.6 292.2 291.4 293.6 293.2 291.1 ...

we can add the abbreviations and names of the regions to the DataArray

<xarray.Dataset>
Dimensions:  (region: 6, time: 2920)
Coordinates:
  * time     (time) datetime64[ns] 2013-01-01 2013-01-01T06:00:00 ...
  * region   (region) float64 4.0 5.0 6.0 7.0 8.0 9.0
    abbrevs  (region) |S3 'CAM' 'WNA' 'CNA' 'ENA' 'ALA' 'GRL'
    names    (region) |S21 'Central America' 'Western North America' ...
Data variables:
    air      (region, time) float64 293.6 292.2 291.4 293.6 293.2 291.1 ...

now we can select the regions in many ways

../../_images/mask_xarray_31_0.png