def test_generate_grid_coords(): r"""Test generate grid coordinates function.""" x = list(range(10)) y = list(range(10)) bbox = get_boundary_coords(x, y) gx, gy = generate_grid(3, bbox) truth = [[0.0, 0.0], [4.5, 0.0], [9.0, 0.0], [0.0, 4.5], [4.5, 4.5], [9.0, 4.5], [0.0, 9.0], [4.5, 9.0], [9.0, 9.0]] pts = generate_grid_coords(gx, gy) assert_array_almost_equal(truth, pts)
def test_generate_grid(): r"""Test generate grid function.""" x = list(range(10)) y = list(range(10)) bbox = get_boundary_coords(x, y) gx, gy = generate_grid(3, bbox) truth_x = np.array([[0.0, 4.5, 9.0], [0.0, 4.5, 9.0], [0.0, 4.5, 9.0]]) truth_y = np.array([[0.0, 0.0, 0.0], [4.5, 4.5, 4.5], [9.0, 9.0, 9.0]]) assert_array_almost_equal(gx, truth_x) assert_array_almost_equal(gy, truth_y)
def define_grid(x, y, hres, buffer, lbound, rbound, tbound, bbound): r"""Define the grid for an analysis. Parameters ---------- x: float x coordinate y: float y coordinate hres: float The horizontal resolution of the generated grid. Default 50000 meters. buffer: float How many meters to add to the bounds of the grid. Default 1000 meters. lbound : float number of meters to trim off of left side of box rbound : float number of meters to trim off of right side of box tbound : float number of meters to trim off the top part of the box bbound : float number of meters to trim off of the bottom part of the box Returns ------- grid_x: (N, 2) ndarray Meshgrid for the resulting interpolation in the x dimension grid_y: (N, 2) ndarray Meshgrid for the resulting interpolation in the y dimension ndarray img: (M, N) ndarray 2-dimensional array representing the interpolated values for each grid. """ grid_x, grid_y = points.generate_grid(hres, points.get_boundary_coords(x, y), buffer) grid_x, grid_y = points.trim_grid(grid_x, grid_y, lbound, rbound, tbound, bbound) return (grid_x, grid_y)
def interpolate(x, y, z, interp_type='linear', hres=50000, buffer=1, minimum_neighbors=3, gamma=0.25, kappa_star=5.052, search_radius=None, rbf_func='linear', rbf_smooth=0): r"""Interpolate given (x,y), observation (z) pairs to a grid based on given parameters. Parameters ---------- x: float x coordinate y: float y coordinate z: float observation value interp_type: str What type of interpolation to use. Available options include: 1) "linear", "nearest", "cubic", or "rbf" from Scipy.interpolate. 2) "natural_neighbor", "barnes", or "cressman" from Metpy.mapping . Default "linear". hres: float The horizontal resolution of the generated grid. Default 50000 meters. buffer: float How many meters to add to the bounds of the grid. Default 1000 meters. minimum_neighbors: int Minimum number of neighbors needed to perform barnes or cressman interpolation for a point. Default is 3. gamma: float Adjustable smoothing parameter for the barnes interpolation. Default 0.25. kappa_star: float Response parameter for barnes interpolation, specified nondimensionally in terms of the Nyquist. Default 5.052 search_radius: float A search radius to use for the barnes and cressman interpolation schemes. If search_radius is not specified, it will default to the average spacing of observations. rbf_func: str Specifies which function to use for Rbf interpolation. Options include: 'multiquadric', 'inverse', 'gaussian', 'linear', 'cubic', 'quintic', and 'thin_plate'. Defualt 'linear'. See scipy.interpolate.Rbf for more information. rbf_smooth: float Smoothing value applied to rbf interpolation. Higher values result in more smoothing. Returns ------- grid_x: (N, 2) ndarray Meshgrid for the resulting interpolation in the x dimension grid_y: (N, 2) ndarray Meshgrid for the resulting interpolation in the y dimension ndarray img: (M, N) ndarray 2-dimensional array representing the interpolated values for each grid. """ grid_x, grid_y = points.generate_grid(hres, points.get_boundary_coords(x, y), buffer) if interp_type in ['linear', 'nearest', 'cubic']: points_zip = np.array(list(zip(x, y))) img = griddata(points_zip, z, (grid_x, grid_y), method=interp_type) elif interp_type == 'natural_neighbor': img = interpolation.natural_neighbor(x, y, z, grid_x, grid_y) elif interp_type in ['cressman', 'barnes']: ave_spacing = np.mean((cdist(list(zip(x, y)), list(zip(x, y))))) if search_radius is None: search_radius = ave_spacing if interp_type == 'cressman': img = interpolation.inverse_distance( x, y, z, grid_x, grid_y, search_radius, min_neighbors=minimum_neighbors, kind=interp_type) elif interp_type == 'barnes': kappa = calc_kappa(ave_spacing, kappa_star) img = interpolation.inverse_distance( x, y, z, grid_x, grid_y, search_radius, gamma, kappa, min_neighbors=minimum_neighbors, kind=interp_type) elif interp_type == 'rbf': # 3-dimensional support not yet included. # Assign a zero to each z dimension for observations. h = np.zeros((len(x))) rbfi = Rbf(x, y, h, z, function=rbf_func, smooth=rbf_smooth) # 3-dimensional support not yet included. # Assign a zero to each z dimension grid cell position. hi = np.zeros(grid_x.shape) img = rbfi(grid_x, grid_y, hi) else: raise ValueError('Interpolation option not available. ' 'Try: linear, nearest, cubic, natural_neighbor, ' 'barnes, cressman, rbf') return grid_x, grid_y, img