猿问

使用 Python Xarray 的特定经纬度坐标的平均值

我想使用 Xarray 计算特定经纬度点的平均值:(-41.25, -3.75)、(-38.75, -6.25) 和 (-38.75, -3.75)。这是一个简单的想法,获取点的值并计算平均值。

我读到xarray.DataArray.sel_points函数已停用,我不知道如何仅使用xarray.DataArray.sel进行计算。


SMILET
浏览 590回答 1
1回答

月关宝盒

假设您有以下 DataArray:>>> da<xarray.DataArray (time: 5, lon: 4, lat: 3)>array([[[...]]])Coordinates:&nbsp; * time&nbsp; &nbsp; &nbsp;(time) int64 0 1 2 3 4&nbsp; * lon&nbsp; &nbsp; &nbsp; (lon) int64 0 1 2 3&nbsp; * lat&nbsp; &nbsp; &nbsp; (lat) int64 0 1 2并且您想选择以下 (lon, lat) 点:>>> points = [(0, 0), (0, 1), (2, 1)]&nbsp; # list(tuple(lon, lat), ...)然后,您不会坚持使用原始的 (lon, lat) 网格,因此您可以将这些维度堆叠在一起:>>> da.stack(pos=("lon", "lat"))<xarray.DataArray (time: 5, pos: 12)>array([[...]])Coordinates:&nbsp; * time&nbsp; &nbsp; &nbsp;(time) int64 0 1 2 3 4&nbsp; * pos&nbsp; &nbsp; &nbsp; (pos) MultiIndex&nbsp; - lon&nbsp; &nbsp; &nbsp; (pos) int64 0 0 0 1 1 1 2 2 2 3 3 3&nbsp; - lat&nbsp; &nbsp; &nbsp; (pos) int64 0 1 2 0 1 2 0 1 2 0 1 2从那里,您可以选择点:>>> da.stack(pos=("lon", "lat")).sel(pos=points)<xarray.DataArray (time: 5, pos: 3)>array([[...]])Coordinates:&nbsp; * time&nbsp; &nbsp; &nbsp;(time) int64 0 1 2 3 4&nbsp; * pos&nbsp; &nbsp; &nbsp; (pos) MultiIndex&nbsp; - lon&nbsp; &nbsp; &nbsp; (pos) int64 0 0 2&nbsp; - lat&nbsp; &nbsp; &nbsp; (pos) int64 0 1 1计算它们的平均时间:>>> da.stack(pos=("lon", "lat")).sel(pos=points).mean("time")<xarray.DataArray (pos: 3)>array([24., 25., 31.])Coordinates:&nbsp; * pos&nbsp; &nbsp; &nbsp; (pos) MultiIndex&nbsp; - lon&nbsp; &nbsp; &nbsp; (pos) int64 0 0 2&nbsp; - lat&nbsp; &nbsp; &nbsp; (pos) int64 0 1 1并且,如果需要,通过取消堆叠返回到原始网格:>>> da.stack(pos=("lon", "lat")).sel(pos=points).mean("time").unstack("pos")<xarray.DataArray (lon: 2, lat: 2)>array([[24., 25.],&nbsp; &nbsp; &nbsp; &nbsp;[nan, 31.]])Coordinates:&nbsp; * lon&nbsp; &nbsp; &nbsp; (lon) int64 0 2&nbsp; * lat&nbsp; &nbsp; &nbsp; (lat) int64 0 1请注意,会有一些nan值未选择新网格的点(这就是我们首先堆叠的原因)。
随时随地看视频慕课网APP

相关分类

Python
我要回答