title: Fast interpolation over 3D array for 3D origin x
body: <p>This problem is similar to a former problem answered <a href="https://stackoverflow.com/questions/13488631/fast-interpolation-over-3d-array">Fast interpolation over 3D array</a>, but cannot solve my problem.</p>
  
  <p>I have a 4D array with dimensions of (time,altitude,latitude, longitude), marked as <code>y.shape=(nt, nalt, nlat, nlon)</code>. The x is altitude and change with (time, latitude, longtitude), which means <code>x.shape = (nt, nalt, nlat, nlon)</code>. I want to interpolate in altitude for every (nt, nlat, nlon). The interpolated x_new should be 1d, not change with (time, latitude, longtitude).</p>
  
  <p>I use <code>numpy.interp</code>, same as <code>scipy.interpolate.interp1d</code> and think about the answers in former post. I cannot reduced the loops with those answers.</p>
  
  <p>I can only do like this:</p>
  
  <pre><code># y is a 4D ndarray
  # x is a 4D ndarray
  # new_y is a 4D array
  for i in range(nlon):
      for j in range(nlat):
          for k in range(nt):
              y_new[k,:,j,i] = np.interp(new_x, x[k,:,j,i], y[k,:,j,i])
  </code></pre>
  
  <p>These loops make this interpolation too slow to calculation. Would someone have good ideas? Help will be highly appreciated.</p>