将stats.linregress应用于netcdf文件中的每个网格单元

问题描述 投票:0回答:1

我有一个包含以下内容的netcdf文件:

<xarray.Dataset> Dimensions: (latitude: 65, longitude: 49, time: 7306) Coordinates: * latitude (latitude) float32 21.0 20.75 20.5 20.25 ... 5.75 5.5 5.25 5.0 * longitude (longitude) float32 116.0 116.25 116.5 ... 127.5 127.75 128.0 * time (time) datetime64[ns] 1985-12-31T23:00:00 ... 2005-12-31T11:00:00 Data variables: pr (time, latitude, longitude) float32 0.049636062 ... 0.6215298 time_bnds (time) datetime64[ns] 1985-12-31T23:00:00 ... 2005-12-31T11:00:00

我的目标是将stats.linregress应用于此数据集的每个网格单元我采取了以下方法:

from scipy import stats
import nump as np
import xarray as xr

#load data
rain = xr.load_dataset('../precipitation.1986-2005.nc')

#group by (lon,lat) pairs by:
stacked = rain.stack(paired_points=['latitude','longitude'])
grouped = stacked.groupby('paired_points').apply(stats.linregress(stacked.time.astype(float),stacked['pr']))
unstack = grouped.unstack('paired_points')

在应用stats.linregress之后,我想创建一个图,其中每个网格单元都由根据线性回归计算的斜率值进行着色。

当我运行代码时,会出现ValueError

ValueError: all the input array dimensions for the concatenation 
axis must match exactly, but along dimension 1, the array at index 0 has size 7306 and the 
array at index 1 has size 3185

问题似乎出在访问每个网格的降水时间序列并为每个网格成功应用stats.linregress

有人可以建议前进的道路吗?

您的帮助将很有价值。

python scipy netcdf python-xarray
1个回答
0
投票

已通过以下方式解决:

rain = xr.load_dataset('../precipitation.1986-2005.nc')

def slope(x):
sl = stats.linregress(x.time.astype(float),x[dict(paired_points=0)]).slope   
return xr.DataArray(sl)

#group by (lon,lat) pairs by: stacked = rain.stack(paired_points=['latitude','longitude']) grouped=stacked.groupby('paired_points').apply(slope) unstack = grouped.unstack('paired_points')

以上解决方案遵循Ryan Abernathey博士在[]中采取的方法

https://gist.github.com/rabernat/bc4c6990eb20942246ce967e6c9c3dbe

© www.soinside.com 2019 - 2024. All rights reserved.