【问题标题】:Python IndexError Opening .nc filePython IndexError 打开 .nc 文件
【发布时间】:2021-09-11 23:12:00
【问题描述】:

当我尝试打开这个看起来像这样的 .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()

在这里感谢您的帮助!

【问题讨论】:

    标签: python matplotlib python-xarray


    【解决方案1】:

    您已正确打开remote_var 中的Dataset。如果你想要remote_var 的一个子集,你可以试试.sel()

    selectvar = remote_var.sel({'lon':slice(-135, -60), 'lat':slice(25, 50)})
    

    如果不同变量有多个网格,可以使用:

    selectM = remote_var.M.sel({'lon':slice(-135, -60), 'lat':slice(25, 50)})
    
    selectM.plot()
    

    【讨论】:

      猜你喜欢
      • 2018-08-28
      • 2016-05-10
      • 2014-06-25
      • 2023-01-20
      • 2017-10-26
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多