python 中大型二维阵列的高效重新网格化
Efficient re-gridding of large 2-D arrays in python
我正在尝试找到将二维数组的一部分重新网格化为更精细网格的最有效方法。为了让我的程序保持通用,我更喜欢使用像 numpy 或 scipy 这样的标准包的解决方案,但这可能超出了他们的能力。
预期的输入数据是 geotiff DEM(数字高程模型)文件,使用 GDAL 导入,并转换为 numpy 数组。一个问题是许多输入文件没有 CRS 信息。
在我下面的 MWE 中重新网格化到 2x2 网格(用于演示)这在我的本地机器上需要 96 秒。实际的精细网格会大很多,精细网格会在循环中创建多次。我承认这个 MWE 可能是执行此操作效率最低的方法!
import numpy as np
import scipy.interpolate as interp
import time
nx,ny = 1600,2400 # size of input data
z_in = np.random.normal(size=(nx,ny)) # make input data
x_in,y_in = np.mgrid[0:nx,0:ny]*0.0932248 # make x,y coordinate arrays for input data
# make new (finer) x,y coordinate arrays. none of these x,y coordinates
# necessarily overlap directly with those of the input dataset.
nx_new,ny_new = 2,2 # size of fine array (in reality, this will be much larger (up to 1000x1000))
x_new,y_new = np.mgrid[0:nx_new,0:ny_new]
x_new = x_new*0.01 + 60
y_new = y_new*0.01 + 85
# regrid the data
starttime=time.time()
flattened_xy_in = np.array([x_in.flatten(),y_in.flatten()]).transpose()
fine_DEM_z = interp.griddata(flattened_xy_in, z_in.flatten(), (x_new, y_new), method='cubic')
print('that took '+str(time.time()-starttime)+' seconds...')
您的输入是位于规则矩形网格上的数据,因此使用 griddata
可用于插入 非结构化 数据是在浪费资源。相反,使用 RectBivariateSpline
是有意义的,如果您已经在网格上有数据,它可以大大加快插值速度。
我正在尝试找到将二维数组的一部分重新网格化为更精细网格的最有效方法。为了让我的程序保持通用,我更喜欢使用像 numpy 或 scipy 这样的标准包的解决方案,但这可能超出了他们的能力。
预期的输入数据是 geotiff DEM(数字高程模型)文件,使用 GDAL 导入,并转换为 numpy 数组。一个问题是许多输入文件没有 CRS 信息。
在我下面的 MWE 中重新网格化到 2x2 网格(用于演示)这在我的本地机器上需要 96 秒。实际的精细网格会大很多,精细网格会在循环中创建多次。我承认这个 MWE 可能是执行此操作效率最低的方法!
import numpy as np
import scipy.interpolate as interp
import time
nx,ny = 1600,2400 # size of input data
z_in = np.random.normal(size=(nx,ny)) # make input data
x_in,y_in = np.mgrid[0:nx,0:ny]*0.0932248 # make x,y coordinate arrays for input data
# make new (finer) x,y coordinate arrays. none of these x,y coordinates
# necessarily overlap directly with those of the input dataset.
nx_new,ny_new = 2,2 # size of fine array (in reality, this will be much larger (up to 1000x1000))
x_new,y_new = np.mgrid[0:nx_new,0:ny_new]
x_new = x_new*0.01 + 60
y_new = y_new*0.01 + 85
# regrid the data
starttime=time.time()
flattened_xy_in = np.array([x_in.flatten(),y_in.flatten()]).transpose()
fine_DEM_z = interp.griddata(flattened_xy_in, z_in.flatten(), (x_new, y_new), method='cubic')
print('that took '+str(time.time()-starttime)+' seconds...')
您的输入是位于规则矩形网格上的数据,因此使用 griddata
可用于插入 非结构化 数据是在浪费资源。相反,使用 RectBivariateSpline
是有意义的,如果您已经在网格上有数据,它可以大大加快插值速度。