Created
June 1, 2016 12:00
-
-
Save rabernat/bc4c6990eb20942246ce967e6c9c3dbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xarray as xr | |
import numpy as np | |
# create an example dataset | |
da = xr.DataArray(np.random.rand(10,30,40), dims=['time', 'lat', 'lon']) | |
# define a function to compute a linear trend of a timeseries | |
def linear_trend(x): | |
pf = np.polyfit(x.time, x, 1) | |
# we need to return a dataarray or else xarray's groupby won't be happy | |
return xr.DataArray(pf[0]) | |
# stack lat and lon into a single dimension called allpoints | |
stacked = da.stack(allpoints=['lat','lon']) | |
# apply the function over allpoints to calculate the trend at each point | |
trend = stacked.groupby('allpoints').apply(linear_trend) | |
# unstack back to lat lon coordinates | |
trend_unstacked = trend.unstack('allpoints') | |
#Just a little add for netcdf climate dataset
def linear_trend(x):
date = x.time
ndate = np.arange(len(date))
pf = np.polyfit(ndate, x, 1)
# we need to return a dataarray or else xarray's groupby won't be happy
return xr.DataArray(pf[0])
How the xarray.polyfit different from this ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am calculating r_value(correlation coefficient) through the time axis between a 3D array vs a 1D array.
my 3d array in the shape (240 time, 180longitude, 37 latitude) and my 1 d array in the shape (240 time)
i want to iterate and calculate the r_value for a range of time series,
say calculate r_value for first 60 in the time series, followed by first 61 in the time series,first 62 in the time series...........up to first 240 in the time series
ie t1-t60[3D array vs 1D array], t1-t61[3D array vs 1D array], t1-t62[3D array vs 1D array].................. t1-t240[3D array vs 1D array]
save all these values in one array which also contain latitude and longitude
i want to keep my longitude and latitude unchanged(means i want to calculate this for every single grid points)
finally i want to get a array which have a shape like (all series rvalue, all longitude, all latitude)
Is there any way to do this?