问题描述
我不知道如何解决这个错误,当我尝试打开这个看起来像这样的 .nc 文件时:
remote_var
Out[99]:
<xarray.Dataset>
Dimensions: (lat: 721,lon: 1440,time: 1)
Coordinates:
* lat (lat) float64 90.0 89.75 89.5 89.25 ... -89.25 -89.5 -89.75 -90.0
* lon (lon) float64 0.0 0.25 0.5 0.75 1.0 ... 359.0 359.2 359.5 359.8
Dimensions without coordinates: time
Data variables:
M (time,lat,lon) float32 dask.array<chunksize=(1,721,1440),Meta=np.ndarray>
我的错误如下:
Traceback (most recent call last):
File "C:\Users\U321103\.spyder-py3\ERA5_MAPPING\ERA5_MAP_100M_WSPD.py",line 35,in <module>
selectvar.M.plot()
IndexError: index -1 is out of bounds for axis 0 with size 0
<figure size 432x288 with 0 Axes>
我的代码如下:
#The easiest way to read the data is:
path = "//porfiler03/gtdshare/VORTEX/ANOMALY_FILES/anomaly.M.2021.05.vs30y/era5.M.2021.05.nc"
remote_var = xr.open_mfdataset(path,combine='nested',concat_dim='time')
selectvar = remote_var.where((-135 < remote_var.lon) & (remote_var.lon < -60)
& (25 < remote_var.lat) & (remote_var.lat < 50),drop=True)
#plot M
plt.figure()
selectvar.M.plot()
exit()
感谢您的帮助!
解决方法
您已正确打开 Dataset
中的 remote_var
。如果您想要 remote_var
的子集,您可以尝试 .sel()
为:
selectvar = remote_var.sel({'lon':slice(-135,-60),'lat':slice(25,50)})
如果不同变量有多个网格,可以使用:
selectM = remote_var.M.sel({'lon':slice(-135,50)})
selectM.plot()