我有代表大气的三维数据。现在我想把这个数据插值到一个公共的Z坐标(我的意思应该从函数的doctring中明确)。下面的代码可以正常工作,但是我想知道是否有一种方法可以提高性能。。。在
def interpLevel(grid,value,data,interp='linear'):
"""
Interpolate 3d data to a common z coordinate.
Can be used to calculate the wind/pv/whatsoever values for a common
potential temperature / pressure level.
grid : numpy.ndarray
The grid. For example the potential temperature values for the whole 3d
grid.
value : float
The common value in the grid, to which the data shall be interpolated.
For example, 350.0
data : numpy.ndarray
The data which shall be interpolated. For example, the PV values for
the whole 3d grid.
kind : str
This indicates which kind of interpolation will be done. It is directly
passed on to scipy.interpolate.interp1d().
returs : numpy.ndarray
A 2d array containing the *data* values at *value*.
"""
ret = np.zeros_like(data[0,:,:])
# we need to copy the grid to a new one, because otherwise the flipping
# done below will be messed up
gr = np.zeros_like(grid)
da = np.zeros_like(data)
for latIdx in xrange(grid.shape[1]):
for lonIdx in xrange(grid.shape[2]):
# check if we need to flip the column
if grid[0,latIdx,lonIdx] > grid[-1,latIdx,lonIdx]:
gr[:,latIdx,lonIdx] = grid[::-1,latIdx,lonIdx]
da[:,latIdx,lonIdx] = data[::-1,latIdx,lonIdx]
else:
gr[:,latIdx,lonIdx] = grid[:,latIdx,lonIdx]
da[:,latIdx,lonIdx] = data[:,latIdx,lonIdx]
f = interpolate.interp1d(gr[:,latIdx,lonIdx], \
da[:,latIdx,lonIdx], \
kind=interp)
ret[latIdx,lonIdx] = f(value)
return ret
好吧,这可能只是因为它使用较少的内存,所以会有一个小的加速。在
我所做的就是把gr和da真的除掉。在
除此之外,您是否用大量不同的值调用此函数(即值不同,但其他参数相同)?如果是这样,您可能希望使函数能够处理多个值(为ret添加另一个维度,换句话说,它与值的长度一样长)。然后你可以更好地利用你创建的插值函数。在
最后一个建议是尝试a profiler。它可以让你看到什么是最耗时的。在
相关问题 更多 >
编程相关推荐