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')
In [8]: ds
Out[8]:
<xarray.Dataset>
Dimensions: (time: 36, x: 275, y: 205)
Coordinates:
* time (time) datetime64[ns] 1980-09-16T12:00:00 1980-10-17 ...
xc (y, x) float64 189.2 189.4 189.6 189.7 189.9 190.1 190.2 190.4 ...
yc (y, x) float64 16.53 16.78 17.02 17.27 17.51 17.76 18.0 18.25 ...
Unindexed dimensions:
x, y
Data variables:
Tair (time, y, x) float64 nan nan nan nan nan nan nan nan nan nan ...
Attributes:
title: /workspace/jhamman/processed/R1002RBRxaaa01a/lnd/temp/R1002RBRxaaa01a.vic.ha.1979-09-01.nc
institution: U.W.
source: RACM R1002RBRxaaa01a
output_frequency: daily
output_mode: averaged
convention: CF-1.4
references: Based on the initial model of Liang et al., 1994, JGR, 99, 14,415- 14,429.
comment: Output from the Variable Infiltration Capacity (VIC) model.
nco_openmp_thread_number: 1
NCO: "4.6.0"
history: Tue Dec 27 14:15:22 2016: ncatted -a dimensions,,d,, rasm.nc rasm.nc
Tue Dec 27 13:38:40 2016: ncks -3 rasm.nc rasm.nc
history deleted for brevity
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
Out[9]:
OrderedDict([('long_name', 'longitude of grid cell center'),
('units', 'degrees_east'),
('bounds', 'xv')])
In [10]: ds.yc.attrs