我一直在尝试根据观测数据(.nc格式)计算降水场的质心,但一直收到错误消息:"TypeError: 'numpy.float64' object is not iterable"
我已经设法将我的netcdf文件从.nc
转换为xarray数据集,然后提取值以提供一个(1,90,180)数组,然后将该数组转换为其他功能的(90,180)。然后,我尝试计算该数组的质心,但它始终显示错误消息。
from scipy import ndimage
ncobsdata = Dataset('/home/data/20180380293.nc', mode = 'r')
obsdata = xr.open_dataset(xr.backends.NetCDF4DataStore(ncobsdata))
obs = obsdata.rain_total #shape = (1, 90, 180)
obsv = np.squeeze(obs) #I had to do this step to make it (90, 180)
CoM_obsv = ndimage.measurements.center_of_mass(obsv)
我希望得到重心结果,但我一直收到错误消息:
File "_____.py", line 10, in <module>
CoM_obsv = ndimage.measurements.center_of_mass(obsv)
File "________/scipy/ndimage/measurements.py", line 1289, in center_of_mass
return [tuple(v) for v in numpy.array(results).T]
TypeError: 'numpy.float64' object is not iterable
最佳答案
因此,这里发生的是obs
和obsv
变量都存储为xarray.DataArrays-此类是常规numpy数组的包装。要访问基础的np.ndarray,您将需要从对象中调用值:CoM_obsv = ndimage.measurements.center_of_mass(obsv.values)
请注意,您无需为obsv = np.squeeze(obs) #I had to do this step to make it (90, 180)
进行此操作,因为xarray.DataArrays已经存在一个挤压方法。