def mesh2grid(v, mesh):
""" Interpolates from an unstructured coordinates (mesh) to a structured
coordinates (grid)
"""
x = mesh[:,0]
z = mesh[:,1]
lx = x.max() - x.min()
lz = z.max() - z.min()
nn = v.size
nx = np.around(np.sqrt(nn*lx/lz))
nz = np.around(np.sqrt(nn*lz/lx))
dx = lx/nx
dz = lz/nz
# construct structured grid
x = np.linspace(x.min(), x.max(), nx)
z = np.linspace(z.min(), z.max(), nz)
X, Z = np.meshgrid(x, z)
grid = stack(X.flatten(), Z.flatten())
# interpolate to structured grid
V = _interp.griddata(mesh, v, grid, 'linear')
# workaround edge issues
if np.any(np.isnan(V)):
W = _interp.griddata(mesh, v, grid, 'nearest')
for i in np.where(np.isnan(V)):
V[i] = W[i]
V = np.reshape(V, (nz, nx))
return V, grid
评论列表
文章目录