TypeError:无效类型促销Xarray空间平均值
我正在尝试计算子午线 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
的速度进行内联。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题似乎是您当前的代码正在计算所有Xarray变量的平均值。作为CDO用户,我会因为Xarray设置BND与变量而感到困惑。在这种情况下,它具有time_bnds作为变量,在您的代码中,它试图计算该变量的平均值,但它不起作用,因为(我认为)没有纬度尺寸。
在计算加权之前,您只需要选择OLR变量即可。
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.