title: How to search for a nearest-neighbor point with a value else than NaN in a datacube?
body: <p>I am working with a datacube such as data[x,y,z]. Each point is a velocity through time, and the [x,y] grid corresponds to coordinates. If I pick a point of coordinates x and y, it is likely that the timeseries is incomplete (with some NaNs). I created a function which searches for the closest neighbor with a value, and replaces the NaN of my xy point with it. However I want to know if there is a more efficient way to code something which does the same ?</p>
  <p>Joined to this message is a photo of how the function evaluates the neighbors. The number of each point represents its rank (5 is the 5th neighbor evaluated).<a href="https://i.stack.imgur.com/jPsTz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jPsTz.jpg" alt="How the algorithm evaluates the closest neighbor" /></a></p>
  <p>I tried something like this:</p>
  <p>Let's say that I have a datacube of 10x10x100 (100 is the timeseries):</p>
  <pre><code>import math
  import numpy as np
  
  Vel = np.random.rand(10,10,100)
  Vel[4:7,4:7,0:10] = np.nan
  
  x = 5
  y = 5
  
  Vpoint = Vel[5,5,:]
  
  
  for i in range(0,len(Vpoint)):
          
          xx = x
          yy = y
          
          
          if math.isnan(Vel[xx,yy,i]) == True:
          
              for n in range(0,50):
                  
                  n = n + 1
              
                  if n &gt; 10:
                      raise Exception(&quot;The interpolation is way too far&quot;) 
              
                  xx = x + n
                  yy = y
              
                  if math.isnan(Vel[xx,yy,i]) == False:
                      Vpoint[i] = Vel[xx,yy,i]
                      break
              
                  xx = x-n
                  if math.isnan(Vel[xx,yy,i]) == False:
                      Vpoint[i] = Vel[xx,yy,i]
                      break
              
                  xx = x
                  yy = y + n
                  if math.isnan(Vel[xx,yy,i]) == False:
                      Vpoint[i] = Vel[xx,yy,i]
                      break
                  
                  yy = y-n
                  if math.isnan(Vel[xx,yy,i]) == False:
                      Vpoint[i] = Vel[xx,yy,i]
                      break
              
                  for p in range(1,n):
                      
                      xx = x+p
                      if math.isnan(Vel[xx,yy,i]) == False:
                          Vpoint[i] = Vel[xx,yy,i]
                          break
                      xx = x-p
                      if math.isnan(Vel[xx,yy,i]) == False:
                          Vpoint[i] = Vel[xx,yy,i]
                          break
                      
                      
                      for p in range(1,n):
                          
                          
                          yy = y+n
                          xx = x+p
                          
                          if math.isnan(Vel[xx,yy,i]) == False:
                              Vpoint[i] = Vel[xx,yy,i]
                              break
                          xx = x-p
                          if math.isnan(Vel[xx,yy,i]) == False:
                              Vpoint[i] = Vel[xx,yy,i]
                              break    
                          
                          
                          yy = y-n
                          xx = x+p
                          
                          if math.isnan(Vel[xx,yy,i]) == False:
                              Vpoint[i] = Vel[xx,yy,i]
                              break
                          xx = x-p
                          if math.isnan(Vel[xx,yy,i]) == False:
                              Vpoint[i] = Vel[xx,yy,i]
                              break
                          
                          xx = x+n
                          yy = y+p
                          
                          if math.isnan(Vel[xx,yy,i]) == False:
                              Vpoint[i] = Vel[xx,yy,i]
                              break
                          yy = y-p
                          if math.isnan(Vel[xx,yy,i]) == False:
                              Vpoint[i] = Vel[xx,yy,i]
                              break    
                          
                          
                          xx = x-n
                          yy = y-p
                          
                          if math.isnan(Vel[xx,yy,i]) == False:
                              Vpoint[i] = Vel[xx,yy,i]
                              break
                          yy = y-p
                          if math.isnan(Vel[xx,yy,i]) == False:
                              Vpoint[i] = Vel[xx,yy,i]
                              break
  
          print(n,xx,yy)
  </code></pre>
  <p>Ps: in reality my timeseries is close to 330x300x38000, and the closest non-nan neighbor should change every time.</p>