One of the operations many scientists do is calculating a linear trend along a specified dimension (e.g. time) on each grid point of a dataset. linear_trend makes this very easy. For demonstration purposes lets load some monthly gridded Argo data from APDRC
In [11]:
import xarray as xr
import numpy as np
%matplotlib inline
In [12]:
path = 'http://apdrc.soest.hawaii.edu:80/dods/public_data/Argo_Products/monthly_mean/monthly_mixed_layer'
ds = xr.open_dataset(path, use_cftime=True)
ds
Out[12]:
Lets find out how much the salinity in each grid point changed over the full period (20 years)
In [13]:
from xarrayutils.utils import linear_trend
In [14]:
# create an array
salinity_regressed = linear_trend(ds.mls, 'time')
salinity_regressed
Out[14]:
Now we can plot the slope as a map
In [15]:
salinity_regressed.slope.plot(robust=True)
Out[15]:
linear_trend converts the dimension over which to integrate into logical indicies, so the units of the plot above are (salinity/timestep of the original product), so here PSS/month.
But what about a bit more complex task? Lets find out how mixedlayer salinity and temperature correlate. For this we use xr_linregress (for which linear_trend is just a thin wrapper):
In [16]:
from xarrayutils.utils import linear_trend, xr_linregress
In [17]:
tempxsalt = xr_linregress(ds.mlt, ds.mls, dim='time')
In [18]:
tempxsalt.r_value.plot()
Out[18]:
This works in any dimension the dataset has:
In [19]:
tempxsalt = xr_linregress(ds.mlt, ds.mls, dim='lon')
In [20]:
tempxsalt.r_value.plot(x='time')
Out[20]:
This map shows that in lower latitudes spatial patterns of salinity are generally anticorrlated with temperature and vice versa in the high latitudes.
In [ ]: