title: Interpolation and Extrapolation of Randomly Scattered data to Uniform Grid in 3D
body: <p>I have a 256 x 256 x 32 grid of regularly spaced points ranging over x, y, and z and with an associated variable "a". I also have a group of randomly scattered points in a more confined x, y, z space, with an associated variable "b". What I essentially want to do is interpolate and extrapolate my random data to a regularly spaced grid that matches the "a" cube, as shown below:</p>
  
  <p><img src="https://i.stack.imgur.com/oondE.jpg" alt="Visual representation."> </p>
  
  <p>I have used scipy's griddata so far to achieve the interpolation, which seems to work fine, but it cannot handle the extrapolation (as far as I know) and the output sharply truncates to 'nan' values. Whilst researching this problem I came across a couple of people using griddata a second time with 'nearest' as the interpolation method to fill in the 'nan' values. I tried this but the results don't seem reliable. More appropriate looking results are obtained if I use a fill_Value with 'linear' mode, but at the moment it's more a fudge because fill_Value has to be a constant.</p>
  
  <p>I noticed that MATLAB has a ScatteredInterpolant class which seems to do what I want, but I am unable to find an equivalent class in Python, nor figure out how to implement such a routine efficiently in 3D. Any help is greatly appreciated.</p>
  
  <p>The code I am using for the interpolation is below:</p>
  
  <pre><code>x, y, z, b = np.loadtxt(scatteredfile, unpack = True)
  
  # Create cube to match aCube dimensions
  xi = np.linspace(-xmax_aCube, xmax_aCube, 256)
  yi = np.linspace(-ymax_aCube, ymax_aCube, 256)
  zi = np.linspace(zmin_aCube, zmax_aCube, 32)
  
  # Interpolate scattered points
  X, Y, Z = np.meshgrid(xi, yi, zi)
  bCube = griddata((x, y, z), b, (X, Y, Z), method = 'linear')    
  </code></pre>