【发布时间】:2016-05-19 18:37:20
【问题描述】:
我有两个来自 WRF 运行的 netcdf 文件,一个包含每小时数据,另一个较小的文件包含坐标(XLAT 和 XLONG)。我正在尝试根据某些坐标检索数据的子集。
变量之一的示例是温度“T2”,其维度 (1,1015,1359) 分别为 (time, south_north, west_east)。
XLAT 和 XLONG 具有相同的尺寸 (1,1015,1359)。
问了一个相同的问题(请参阅netcdf4 extract for subset of lat lon),因为我的纬度/经度尺寸略有不同,脚本对我不起作用,我无法弄清楚原因。我尝试将坐标更改为一维数组,以便与上一个问题类似,但脚本不起作用,并且出现索引错误。
如果有人可以帮助我,那就太棒了!在此先感谢:)
import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt
lons = b.variables['XLONG'][:]
lats = b.variables['XLAT'][:]
lons2d =lons.reshape((1015,1359))
lons1d = lons2d.reshape((1379385))
lats2d =lats.reshape((1015,1359))
lats1d = lats2d.reshape((1379385))
lat_bnds, lon_bnds = [49,53], [-125,-115]
lat_inds = np.where((lats1d > lat_bnds[0]) & (lats1d < lat_bnds[1]))
lon_inds = np.where((lons1d > lon_bnds[0]) & (lons1d < lon_bnds[1]))
T_subset = a.variables['T2'][:,lat_inds,lon_inds]
但是我收到以下错误:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-2-0f8890d3b1c5> in <module>()
25 lon_inds = np.where((lons1d > lon_bnds[0]) & (lons1d < lon_bnds[1]))
26
---> 27 T_subset = a.variables['T2'][:,lat_inds,lon_inds]
28
29
netCDF4/_netCDF4.pyx in netCDF4._netCDF4.Variable.__getitem__(netCDF4/_netCDF4.c:35672)()
/Users/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/netCDF4/utils.pyc in _StartCountStride(elem, shape, dimensions, grp, datashape, put)
197 # Raise error if multidimensional indexing is used.
198 if ea.ndim > 1:
--> 199 raise IndexError("Index cannot be multidimensional")
200 # set unlim to True if dimension is unlimited and put==True
201 # (called from __setitem__)
IndexError: Index cannot be multidimensional
【问题讨论】:
标签: python arrays numpy netcdf weather