-
-
Save ccarouge/88406146883fab7bb9569acb3d7e3ce6 to your computer and use it in GitHub Desktop.
Added sample = sample.where(np.isfinite(sample),drop=True)
. But it's not doing the trick as the NaN's do not occur at the same time steps in each file.
For example:
x1 = np.asarray([ 5., np.nan, 5., 5., 4., np.nan, 6., 8., 1., 7.,np.nan])
x2 = np.asarray([ 5., 3., 5., np.nan, 5., 4., 6., 8., 1., np.nan, 7.])
y1 = xr.DataArray(x1,dims='c',coords={'c':np.arange(0,11)})
y2 = xr.DataArray(x2,dims='c',coords={'c':np.arange(0,11)})
yy = xr.concat([y1,y2],pd.Index(['a','b'],name='n'))
yy.where(np.isfinite(yy),drop=True)
As a result, NaNs are still contained:
<xarray.DataArray (n: 2, c: 11)>
array([[ 5., nan, 5., 5., 4., nan, 6., 8., 1., 7., nan],
[ 5., 3., 5., nan, 5., 4., 6., 8., 1., nan, 7.]])
Coordinates:
* c (c) int64 0 1 2 3 4 5 6 7 8 9 10
* n (n) object 'a' 'b'
I have used your code with my data:
idir = "/short/w35/dh4185/RA_Ailie/data/REGEN_LongTermStns_v1/"
spifile = idir+"SPI"+w+"_prcptot_MON_REGENv1.1LongTerm_historical_NA_1950-2013.NaNmod.nc"
file1 = xr.open_dataset(spifile).sel(time=slice('1951-01','2013-12'))
file2 = xr.open_dataset("{}sdii_12MON_REGENv1.1LongTerm_historical_NA_1950-2013.nc".format(idir)).sel(time=slice('1951-01','2013-12'))
after that I followed exactly your steps:
sample1=file1.SPI12.stack(z=('lat','lon'))
sample1= sample1.expand_dims(dim='c',axis=0)
sample2=file2.SDII.stack(z=('lat','lon'))
sample2= sample2.expand_dims(dim='c',axis=0)
sample = xr.concat((sample1, sample2),dim='c')
%time stats = sample.groupby('z').reduce(corr2,dim='time')
stats=stats.unstack()
however, plotting both arrays gives me this: