我有两个来自 WRF 运行的 netcdf 文件,一个包含每小时数据,另一个较小的文件包含坐标(XLAT 和 XLONG)。我正在尝试根据某些坐标检索数据的子集。
其中一个变量的示例是温度“T2”,其维度 (1,1015,1359) 分别为(时间、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
最佳答案
我不确定为什么它不起作用,但我认为这符合您的要求并且更干净:
import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt
# By indexing at 0 along first dimension, we eliminate the time
# dimension, which only had size 0 anyway.
lons = b.variables['XLONG'][0]
lats = b.variables['XLAT'][0]
temp = a.variables['T2'][0]
lat_bnds, lon_bnds = [49,53], [-125,-115]
# Just AND together all of them and make a big mask
subset = ((lats > lat_bnds[0]) & (lats < lat_bnds[1]) &
(lons > lon_bnds[0]) & (lons < lon_bnds[1]))
# Apply mask--should apply to trailing dimensions...I think
T_subset = temp[subset]
关于python - WRF netcdf 文件 - 基于 python 中的坐标边界从数据集中取出较小的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35297771/