Interpolating values across a 2D coordinate grid, from a 1D data file

Viewed 17

I have a 2-D grid, rr, representing distance from an arbitrary point, coord, on that grid:

import numpy as np

coord = (2.41, 3.24)
xx, yy = np.meshgrid(range(5), range(5))
rr = np.sqrt((xx - coord[0]) ** 2. + (yy - coord[1]) ** 2.))

I also have two 1-D arrays of values of r (always sorted in ascending order) and corresponding values for z(r), z_r:

r = np.array([0, 0.5, 1., 1.5])
z_r = np.array([10.2, 5.3, 2.2, 0.4])

My goal is to compute z across the whole of grid rr to give a corresponding grid of values for z, zz.

In reality, the actual grid, rr, may be upto 20000 by 20000 elements meaning that computational efficiency is a real concern, especially when I may want to repeat the above interpolation across hundreds of different rr grids.

So far, I have tried the least efficient approach whereby I iterate through element by element of rr, find the indices of the two neighbouring values within r and take the mean (coarse interpolation I know) of the corresponding values from z_r:

zz = np.zeros_like(rr)
for i_row, row in enumerate(rr):
    for i_col, element in enumerate(row):
        idx = np.searchsorted(r, element)
        if idx < len(r):
            zz[i_row][i_col] = np.mean(z_r[idx - 1:idx + 1])

Does anyone have any other suggestions to increase computational efficiency and speed?

0 Answers
Related