2

I have a set of non-gridded points and a grid with holes in it - represented by an array with NaN's in it (a masked array?). I am trying to "map" the non-gridded points onto this grid and then interpolate across the grid, with the NaN areas serving as boundaries.

griddata interpolates this data okay, but ignores the NaN values, and values on one side of a NaN "wall" affect values on the other side. (I hope this makes sense).

Does anyone know if there is any way to force griddata to take NaN's as boundaries and not allow points on one side of an NaN area from affecting the other?

So far I can get this:

Example Plot

Left plot is griddata carried out on a nonmasked array, the right plot is griddata carried out on a masked array. I was hoping to not see the interpolation carry across the masked areas.

This is a simplified example, and my masked array is in fact a bit more complicated (there is a set of blank islands or "holes" in the mesh/array), so cannot really treat sections in isolation (as would be a solution in this example).

This is what I have so far:

X = np.asarray(cfdx[1:])
Y = np.asarray(cfdy[1:])
Z = np.asarray(errors)


xi = np.linspace(X.min(),X.max(),100)
yi = np.linspace(Y.min(),Y.max(),100)

xii = xi
yii = yi

zii = griddata((X, Y), Z, (xii[None,:], yii[:,None]), method='cubic')

fig = plt.figure(figsize=plt.figaspect(0.5))
ax = fig.add_subplot(121)
CS = plt.contourf(xii,yii,zii,15)

xi[45:50] = np.NaN
yi[45:50] = np.NaN

zi = griddata((X, Y), Z, (xi[None,:], yi[:,None]), method='cubic')

ax2 = fig.add_subplot(122)
CS2 = plt.contourf(xi,yi,zi,15)

plt.show()

Any help would be greatly appreciated!

FP

fp1991
  • 140
  • 1
  • 10

1 Answers1

0

Even though this question is pretty old, I'd like to comment and ask for hints. I am having a similar problem, with the difference that the four blocks might not be completely disconnected, but are linked through 'detours' around the holes (background is the interpolation of groundwater levels).

Thinking about it, what would be necessary is

  1. an algorithm that efficiently calculates distances within the grid between any two locations, reflecting the shortest connection avoiding the gaps.
  2. an interpolation algorithm that allows to explicitly set distances rather than calculating them from data point and target locations only.

As I don't want to code both of these, I'd be most happy if someone could point me towards potentially exisiting solutions for either of the two. I know commercial software that does this kind of interpolation, but would prefer an independent solution.