Interpolate large irregular grid onto another irregular grid in Python

后端 未结 1 929
小蘑菇
小蘑菇 2021-01-15 17:22

I am trying to interpolate complex values from one irregular grid to another irregular grid using Python. The grids are in 2D and there are 103,113 data points. I am using P

相关标签:
1条回答
  • 2021-01-15 17:43

    Scipy's griddata seems to be able to deal with data sets of this size without problems:

    import numpy as np
    import scipy.interpolate
    
    # old grid
    x, y = np.mgrid[0:1:201j, 0:1:513j]
    z = np.sin(x*20) * (1j + np.cos(y*3))**2   # some data
    
    # new grid
    x2, y2 = np.mgrid[0.1:0.9:201j, 0.1:0.9:513j]
    
    # interpolate onto the new grid
    z2 = scipy.interpolate.griddata((x.ravel(), y.ravel()), z.ravel(), (x2, y2), method='cubic')
    

    The griddata step takes about 5s on an old AMD Athlon.

    If your data is on a grid (i.e., the coordinates corresponding to value z[i,j] are (x[i], y[j])), you can get more speed by using scipy.interpolate.RectBivariateSpline

    z3 = (scipy.interpolate.RectBivariateSpline(x[:,0], y[0,:], z.real)(x2[:,0], y2[0,:])
     + 1j*scipy.interpolate.RectBivariateSpline(x[:,0], y[0,:], z.imag)(x2[:,0], y2[0,:]))
    

    which takes 0.05s. It's much faster, because even if your grid spacings are irregular, a more efficient algorithm can be used as long as the grid is rectangular.

    0 讨论(0)
提交回复
热议问题