Xarray切片函数替代沿维度计算平均值的替代方案

发布于 2025-01-25 12:56:55 字数 1261 浏览 2 评论 0 原文

我正在使用Xarray和NetCDF气象数据。我有通常的尺寸时间,纬度和经度以及两个主要变量:风速(时间,lat,lon)和纬度位置(时间,lon)。

 <xarray.Dataset>
Dimensions:             (lon: 53, time: 25873, lat: 20)
Coordinates:
  * lon                 (lon) float64 -80.0 -77.5 -75.0 -72.5 ... 45.0 47.5 50.0
  * time                (time) datetime64[ns] 1950-01-31 ... 2020-12-01
  * lat                 (lat) float32 70.0 67.5 65.0 62.5 ... 27.5 25.0 22.5
Data variables:
    uwnd                (time, lat, lon) float32 -0.0625 0.375 ... -1.812 -2.75
    positions           (time, lon) float64 40.0 40.0 45.0 ... 70.0 70.0 70.0

每次,LON,我都想计算位置周围的纬度平均值。

如果我做一个循环,我会这样做(对于 +-2.5°平均平均水平):

for i in ds.lon.values:
        for t in ds.time.values:
              wind_averaged.loc[t,i]=ds.uwnd.sel(lon=i,time=t).sel(lat=slice(2.5+ds.positions.sel(lon=i,time=t).values,ds.positions.sel(lon=i,time=t).values-2.5)).mean('lat')

这显然很糟糕,我想使用 slice()这样:

wind_averaged=ds.uwnd.sel(lat=slice(2.5+ds.jet_positions.values,ds.jet_positions.values-2.5)).mean('lat')

但是它给出了错误,因为我

cannot use non-scalar arrays in a slice for xarray indexing

有其他选择可以通过使用Xarray Power做我想做的事情吗?

谢谢

I'm using Xarray and netCDF meteorological data. I have the usual dimensions time, latitude and longitude and two main variables: the wind speed (time, lat, lon) and a latitudinal position (time, lon).

 <xarray.Dataset>
Dimensions:             (lon: 53, time: 25873, lat: 20)
Coordinates:
  * lon                 (lon) float64 -80.0 -77.5 -75.0 -72.5 ... 45.0 47.5 50.0
  * time                (time) datetime64[ns] 1950-01-31 ... 2020-12-01
  * lat                 (lat) float32 70.0 67.5 65.0 62.5 ... 27.5 25.0 22.5
Data variables:
    uwnd                (time, lat, lon) float32 -0.0625 0.375 ... -1.812 -2.75
    positions           (time, lon) float64 40.0 40.0 45.0 ... 70.0 70.0 70.0

For each time, lon, I'd like to calculate a latitudinal average around the positions.

If I do a loop, I would do this (for a +-2.5° latitude average):

for i in ds.lon.values:
        for t in ds.time.values:
              wind_averaged.loc[t,i]=ds.uwnd.sel(lon=i,time=t).sel(lat=slice(2.5+ds.positions.sel(lon=i,time=t).values,ds.positions.sel(lon=i,time=t).values-2.5)).mean('lat')

This is obviously very bad and I wanted to use slice() like this:

wind_averaged=ds.uwnd.sel(lat=slice(2.5+ds.jet_positions.values,ds.jet_positions.values-2.5)).mean('lat')

but it gives an error because I

cannot use non-scalar arrays in a slice for xarray indexing

Is there any alternative to do what I want without doing two for loops by using Xarray power?

Thanks

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

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

发布评论

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

评论(1

眼眸印温柔 2025-02-01 12:56:55

我相信您正在寻找多维集团。如果我正确理解的话,这里有一个有关此问题的教程:

I believe you are looking for the multidimensional groupby. If I understand correctly, there is a tutorial for this problem here: https://xarray.pydata.org/en/stable/examples/multidimensional-coords.html

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