TypeError:无效类型促销Xarray空间平均值

发布于 2025-02-08 04:07:08 字数 1015 浏览 2 评论 0原文

我正在尝试计算子午线 cesine加权均值 - NetCDF数据集的区域和时间层,这是我的代码,

from netCDF4 import Dataset
import xarray as xr
import numpy as np

min_lat=2
max_lat=9
datestr="2017-03-10"

olrfile="olr-daily_v01r02_20170101_20171231.nc"
ds=xr.open_dataset(olrfile)
olr=ds.sel(lat=slice(min_lat,max_lat),time=datestr)
weights=np.cos(np.deg2rad(ds.lat))
olrw=olr.weighted(weights)
olrm=olrw.mean(dim=('lat'))

最终的均值语句陷入了错误

File "/afs/ictp/home/t/tompkins/.local/lib/python3.6/site-packages/numpy/core/einsumfunc.py", line 1350, in einsum
    return c_einsum(*operands, **kwargs)
TypeError: invalid type promotion

,我不知道有什么问题...我知道我可以使用cdo,但是我想我会尝试以xarray的速度进行内联。

netcdf文件dir的链接在这里。

I'm trying to calculate a meridional cosine weighted mean of a sub-region and timeslice of a netcdf dataset, here is my code,

from netCDF4 import Dataset
import xarray as xr
import numpy as np

min_lat=2
max_lat=9
datestr="2017-03-10"

olrfile="olr-daily_v01r02_20170101_20171231.nc"
ds=xr.open_dataset(olrfile)
olr=ds.sel(lat=slice(min_lat,max_lat),time=datestr)
weights=np.cos(np.deg2rad(ds.lat))
olrw=olr.weighted(weights)
olrm=olrw.mean(dim=('lat'))

The final mean statement falls over with the error

File "/afs/ictp/home/t/tompkins/.local/lib/python3.6/site-packages/numpy/core/einsumfunc.py", line 1350, in einsum
    return c_einsum(*operands, **kwargs)
TypeError: invalid type promotion

and I have no idea what is wrong... I know I can do this with cdo, but I thought I would try to do it inline in xarray for speed.

The link to the netcdf file dir is here.

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

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

发布评论

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

评论(1

眼趣 2025-02-15 04:07:08

问题似乎是您当前的代码正在计算所有Xarray变量的平均值。作为CDO用户,我会因为Xarray设置BND与变量而感到困惑。在这种情况下,它具有time_bnds作为变量,在您的代码中,它试图计算该变量的平均值,但它不起作用,因为(我认为)没有纬度尺寸。

在计算加权之前,您只需要选择OLR变量即可。

from netCDF4 import Dataset
import xarray as xr
import numpy as np

min_lat=2
max_lat=9
datestr="2017-03-10"

olrfile="olr-daily_v01r02_20170101_20171231.nc"
ds=xr.open_dataset(olrfile)
olr=ds["olr"].sel(lat=slice(min_lat,max_lat),time=datestr)
weights=np.cos(np.deg2rad(ds.lat))
olrw=olr.weighted(weights)
olrm=olrw.mean(dim=('lat'))

The problem appears to be that your current code is calculating the mean for all xarray variables. As more of a CDO user personally, I get confused by xarray setting bnds to variables. In this case it has time_bnds as a variable, and in your code it is trying to calculate the mean for that variable, but it's not working because (I think) there is no lat dimension for it.

You would just need to select the olr variable before calculating the weighting.

from netCDF4 import Dataset
import xarray as xr
import numpy as np

min_lat=2
max_lat=9
datestr="2017-03-10"

olrfile="olr-daily_v01r02_20170101_20171231.nc"
ds=xr.open_dataset(olrfile)
olr=ds["olr"].sel(lat=slice(min_lat,max_lat),time=datestr)
weights=np.cos(np.deg2rad(ds.lat))
olrw=olr.weighted(weights)
olrm=olrw.mean(dim=('lat'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文