Xarray-在特定X和Y坐标处选择数据

发布于 2025-01-27 08:47:25 字数 307 浏览 4 评论 0原文

在x,y位置选择数据时,我会获取任何一对x,y的数据。我想拥有一个1D阵列,而不是选择中的2D数组。有没有有效的方法? (目前我正在使用循环...)

x = [x1,x2,x3,x4] y = [y1,y2,y3,y4]

ds = 2-d阵列

subset = Dataset.sel(longitude=x, latitude=y, method='nearest')

到rephrase,我想在[x1,y1],[x2,y2],[x3,y3],[x4,y4上都有数据集]不在其他位置,即[x1,y2]。

When selecting data with xarray at x,y locations, I get data for any pair of x,y. I would like to have a 1-D array not a 2-D array from the selection. Is there an efficient way to do this? (For now I am doing it with a for-loop...)

x = [x1,x2,x3,x4]
y = [y1,y2,y3,y4]

DS = 2-D array

subset = Dataset.sel(longitude=x, latitude=y, method='nearest')

To rephrase, I would like to have the dataset at [x1,y1],[x2,y2],[x3,y3],[x4,y4] not at other location i.e. [x1,y2].

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江挽川 2025-02-03 08:47:25

如果索引器为dataArray s,则可以沿多个索引选择点列表。这将导致数组沿索引器的共同维度重新索引。

直接从更高级的索引

In [78]: da = xr.DataArray(np.arange(56).reshape((7, 8)), dims=['x', 'y'])

In [79]: da
Out[79]: 
<xarray.DataArray (x: 7, y: 8)>
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47],
       [48, 49, 50, 51, 52, 53, 54, 55]])
Dimensions without coordinates: x, y

In [80]: da.isel(x=xr.DataArray([0, 1, 6], dims='z'),
   ....:         y=xr.DataArray([0, 1, 0], dims='z'))
   ....: 
Out[80]: 
<xarray.DataArray (z: 3)>
array([ 0,  9, 48])
Dimensions without coordinates: z

indexing the Indexing)阵列也可以轻松地从pandas dataframe中拔出,并带有da.sel(pertitude = df.df.longendity.to_xarray(),latitude = df.latitude.to_xarray())< /code>,这将导致dataarray dataframe的索引重新索引。

因此,就您而言,而不是选择列表或数组x,y,将它们变成带有常见dim的数据阵列 - 让我们称其为location> location

x = xr.DataArray([x1,x2,x3,x4], dims=['location'])
y = xr.DataArray([y1,y2,y3,y4], dims=['location'])

现在您的选择将工作如您所希望的:

ds.sel(longitude=x, latitude=y, method='nearest')

A list of points can be selected along multiple indices if the indexers are DataArrays with a common dimension. This will result in the array being reindexed along the indexers' common dimension.

Straight from the docs on More Advanced Indexing:

In [78]: da = xr.DataArray(np.arange(56).reshape((7, 8)), dims=['x', 'y'])

In [79]: da
Out[79]: 
<xarray.DataArray (x: 7, y: 8)>
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47],
       [48, 49, 50, 51, 52, 53, 54, 55]])
Dimensions without coordinates: x, y

In [80]: da.isel(x=xr.DataArray([0, 1, 6], dims='z'),
   ....:         y=xr.DataArray([0, 1, 0], dims='z'))
   ....: 
Out[80]: 
<xarray.DataArray (z: 3)>
array([ 0,  9, 48])
Dimensions without coordinates: z

The indexing array can also be easily pulled out of a pandas DataFrame, with something like da.sel(longitude=df.longitude.to_xarray(), latitude=df.latitude.to_xarray()), which will result in the DataArray being reindexed by the DataFrame's index.

So in your case, rather than selecting with the lists or arrays x, y, turn them into DataArrays with a common dim - let's call it location:

x = xr.DataArray([x1,x2,x3,x4], dims=['location'])
y = xr.DataArray([y1,y2,y3,y4], dims=['location'])

Now your selection will work as you hope:

ds.sel(longitude=x, latitude=y, method='nearest')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文