Fast 2D, linear interpolation on an integer grid
(a, xi, yi)
| 688 | # ======================== |
| 689 | |
| 690 | def interpgrid(a, xi, yi): |
| 691 | """Fast 2D, linear interpolation on an integer grid""" |
| 692 | |
| 693 | Ny, Nx = np.shape(a) |
| 694 | if isinstance(xi, np.ndarray): |
| 695 | x = xi.astype(int) |
| 696 | y = yi.astype(int) |
| 697 | # Check that xn, yn don't exceed max index |
| 698 | xn = np.clip(x + 1, 0, Nx - 1) |
| 699 | yn = np.clip(y + 1, 0, Ny - 1) |
| 700 | else: |
| 701 | x = int(xi) |
| 702 | y = int(yi) |
| 703 | # conditional is faster than clipping for integers |
| 704 | if x == (Nx - 1): |
| 705 | xn = x |
| 706 | else: |
| 707 | xn = x + 1 |
| 708 | if y == (Ny - 1): |
| 709 | yn = y |
| 710 | else: |
| 711 | yn = y + 1 |
| 712 | |
| 713 | a00 = a[y, x] |
| 714 | a01 = a[y, xn] |
| 715 | a10 = a[yn, x] |
| 716 | a11 = a[yn, xn] |
| 717 | xt = xi - x |
| 718 | yt = yi - y |
| 719 | a0 = a00 * (1 - xt) + a01 * xt |
| 720 | a1 = a10 * (1 - xt) + a11 * xt |
| 721 | ai = a0 * (1 - yt) + a1 * yt |
| 722 | |
| 723 | if not isinstance(xi, np.ndarray): |
| 724 | if np.ma.is_masked(ai): |
| 725 | raise TerminateTrajectory |
| 726 | |
| 727 | return ai |
| 728 | |
| 729 | |
| 730 | def _gen_starting_points(shape): |
no test coverage detected
searching dependent graphs…