def test_dist_2(): r"""Test squared distance function.""" x0 = 0 y0 = 0 x1 = 10 y1 = 10 truth = 200 dist2 = dist_2(x0, y0, x1, y1) assert_almost_equal(truth, dist2)
def test_cressman_point(test_data): r"""Test Cressman interpolation for a point function.""" xp, yp, z = test_data r = 40 obs_tree = cKDTree(list(zip(xp, yp))) indices = obs_tree.query_ball_point([30, 30], r=r) dists = dist_2(30, 30, xp[indices], yp[indices]) values = z[indices] truth = 1.05499444404 value = cressman_point(dists, values, r) assert_almost_equal(truth, value)
def test_barnes_point(test_data): r"""Test Barnes interpolation for a point function.""" xp, yp, z = test_data r = 40 obs_tree = cKDTree(list(zip(xp, yp))) indices = obs_tree.query_ball_point([60, 60], r=r) dists = dist_2(60, 60, xp[indices], yp[indices]) values = z[indices] truth = 4.08718241061 ave_spacing = np.mean((cdist(list(zip(xp, yp)), list(zip(xp, yp))))) kappa = calc_kappa(ave_spacing) value = barnes_point(dists, values, kappa) assert_almost_equal(truth, value)
def inverse_distance(xp, yp, variable, grid_x, grid_y, r, gamma=None, kappa=None, min_neighbors=3, kind='cressman'): r"""Generate an inverse distance weighting interpolation of the given points to the given grid based on either Cressman (1959) or Barnes (1964). The Barnes implementation used here based on Koch et al. (1983). Parameters ---------- xp: (N, ) ndarray x-coordinates of observations. yp: (N, ) ndarray y-coordinates of observations. variable: (N, ) ndarray observation values associated with (xp, yp) pairs. IE, variable[i] is a unique observation at (xp[i], yp[i]). grid_x: (M, 2) ndarray Meshgrid associated with x dimension. grid_y: (M, 2) ndarray Meshgrid associated with y dimension. r: float Radius from grid center, within which observations are considered and weighted. gamma: float Adjustable smoothing parameter for the barnes interpolation. Default None. kappa: float Response parameter for barnes interpolation. Default None. min_neighbors: int Minimum number of neighbors needed to perform barnes or cressman interpolation for a point. Default is 3. kind: str Specify what inverse distance weighting interpolation to use. Options: 'cressman' or 'barnes'. Default 'cressman' Returns ------- img: (M, N) ndarray Interpolated values on a 2-dimensional grid """ obs_tree = cKDTree(list(zip(xp, yp))) grid_points = points.generate_grid_coords(grid_x, grid_y) indices = obs_tree.query_ball_point(grid_points, r=r) img = np.empty(shape=(grid_points.shape[0]), dtype=variable.dtype) img.fill(np.nan) for idx, (matches, grid) in enumerate(zip(indices, grid_points)): if len(matches) >= min_neighbors: x1, y1 = obs_tree.data[matches].T values = variable[matches] dists = triangles.dist_2(grid[0], grid[1], x1, y1) if kind == 'cressman': img[idx] = cressman_point(dists, values, r) elif kind == 'barnes': img[idx] = barnes_point(dists, values, kappa) else: raise ValueError(str(kind) + ' interpolation not supported.') img = img.reshape(grid_x.shape) return img
########################################### # Set up a cKDTree object and query all of the observations within "radius" of each grid point. # # The variable ``indices`` represents the index of each matched coordinate within the # cKDTree's ``data`` list. grid_points = np.array(list(zip(sim_gridx, sim_gridy))) radius = 40 obs_tree = cKDTree(list(zip(xp, yp))) indices = obs_tree.query_ball_point(grid_points, r=radius) ########################################### # For grid 0, we will use Cressman to interpolate its value. x1, y1 = obs_tree.data[indices[0]].T cress_dist = dist_2(sim_gridx[0], sim_gridy[0], x1, y1) cress_obs = zp[indices[0]] cress_val = cressman_point(cress_dist, cress_obs, radius) ########################################### # For grid 1, we will use barnes to interpolate its value. # # We need to calculate kappa--the average distance between observations over the domain. x2, y2 = obs_tree.data[indices[1]].T barnes_dist = dist_2(sim_gridx[1], sim_gridy[1], x2, y2) barnes_obs = zp[indices[1]] ave_spacing = np.mean((cdist(list(zip(xp, yp)), list(zip(xp, yp))))) kappa = calc_kappa(ave_spacing)