[SciPy-User] 2D bilinear interpolation from low to high resolution

Marston Sheldon Johnston marston at chalmers.se
Tue Jul 10 20:04:13 EDT 2012


Hi,

I've been trying to copy and modify a Scipy Cookbook function that does a 2d spline interpolation using the same data limits but with higher resolution. In my function I use a bilinear interpolation. 
I've struggled to get this working and now that I have the results are horrible and incorrect. Would love some help with this if anyone has some experience with this particular subject.
I assume the lat and lon must be ascending, since it is not in the original data, I have moved the data around. I've tried this without sorting the lats and the results are the similar: garbled! The data is a slice of an array. The slice is centered at lat0 lon0 and is ±10 degrees on all 4 sides forming a 20x20 degree window. The initial resolution is 1.125 and the new resolution is 0.703125. 

The input dictionary and data is posted below.

def Low2HighRes(plist,ivals):                                                                 
    """N-D interpolation for equally-spaced data"""                                           
    latsort = np.argsort(plist['xlats'])                                                      
    lonsort = np.argsort(plist['xlons'])                                                      
    ix,jy = np.meshgrid(plist['newx'],plist['newy'])                                          
    coords = scipy.array([ix,jy])                                                             
    vals = ivals[latsort,:]                                                                
    nvals = scipy.ndimage.map_coordinates(vals,coords,order=1,mode='nearest')          
    return nvals

The new resolution is 0.703125:
'newx': array([-9.84375 , -9.140625, -8.4375  , -7.734375, -7.03125 , -6.328125,
                        -5.625   , -4.921875, -4.21875 , -3.515625, -2.8125  , -2.109375,
                        -1.40625 , -0.703125, -0.      ,  0.703125,  1.40625 ,  2.109375,
                         2.8125  ,  3.515625,  4.21875 ,  4.921875,  5.625   ,  6.328125,
                         7.03125 ,  7.734375,  8.4375  ,  9.140625,  9.84375 ])

'newy': array([-9.84375 , -9.140625, -8.4375  , -7.734375, -7.03125 , -6.328125,
                        -5.625   , -4.921875, -4.21875 , -3.515625, -2.8125  , -2.109375,
                        -1.40625 , -0.703125, -0.      ,  0.703125,  1.40625 ,  2.109375,
                        2.8125  ,  3.515625,  4.21875 ,  4.921875,  5.625   ,  6.328125,
                        7.03125 ,  7.734375,  8.4375  ,  9.140625,  9.84375 ])

original coordinates of the data to be interpolated are lats and lons with a 1.125 resolution:
'modx': array([-10.125,  -9.   ,  -7.875,  -6.75 ,  -5.625,  -4.5  ,  -3.375,
                         -2.25 ,  -1.125,  -0.   ,   1.125,   2.25 ,   3.375,   4.5  ,
                          5.625,   6.75 ,   7.875,   9.   ,  10.125]) 
'mody': array([-10.125,  -9.   ,  -7.875,  -6.75 ,  -5.625,  -4.5  ,  -3.375,
                         -2.25 ,  -1.125,  -0.   ,   1.125,   2.25 ,   3.375,   4.5  ,
                          5.625,   6.75 ,   7.875,   9.   ,  10.125]) 

'lats': array([ 18.50457985,  17.38309053,  16.26160115,  15.14011169,
                      14.01862218,  12.8971326 ,  11.77564298,  10.65415331,
                      9.53266359,   8.41117384,   7.28968406,   6.16819425,
                      5.04670442,   3.92521457,   2.8037247 ,   1.68223483,
                      0.56074494,  -0.56074494,  -1.68223483]), 
'lons': array([ 229.5  ,  230.625,  231.75 ,  232.875,  234.   ,  235.125,
                       236.25 ,  237.375,  238.5  ,  239.625,  240.75 ,  241.875,
                       243.   ,  244.125,  245.25 ,  246.375,  247.5  ,  248.625,  249.75 ]), 

The data:

array([[  2.91407923e-03,   3.39587848e-03,   1.21381157e-03,
          3.29374452e-04,   7.41946205e-05,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   4.87387297e-05,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          3.60107806e-05,   1.17035036e-03,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00],
       [  2.40930752e-03,   4.95893275e-03,   2.63592694e-03,
          1.06014498e-03,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   1.30197592e-03,   1.14706755e-02,
          1.85952216e-04,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00],
       [  1.04772742e-03,   2.78711016e-03,   3.14659718e-03,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   6.02249238e-05,   4.03569084e-06,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00],
       [  0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   1.24175097e-06,
          0.00000000e+00,   0.00000000e+00,   2.44314520e-04,
          2.39037072e-05,   3.38377140e-05,   3.95187264e-04,
          0.00000000e+00],
       [  0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          2.48350193e-06,   7.45050602e-06,   1.17966347e-05,
          0.00000000e+00,   1.55218879e-06,   1.86262650e-06,
          2.60457280e-04,   0.00000000e+00,   4.62552271e-05,
          9.49318637e-04,   9.99299111e-04,   4.70123850e-02,
          8.09109434e-02],
       [  0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   1.17966347e-05,
          7.91616258e-05,   1.24175097e-06,   0.00000000e+00,
          6.20875483e-07,   4.59447874e-05,   5.22777205e-04,
          0.00000000e+00,   4.52431990e-03,   1.75440796e-02,
          8.04008991e-02,   1.31909043e-01,   2.24047899e-01,
          1.60426781e-01],
       [  0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          9.31313252e-07,   1.14861969e-05,   2.39968387e-04,
          4.34612866e-06,   2.17306433e-06,   1.44353558e-04,
          1.58664735e-03,   2.86217406e-02,   1.11849174e-01,
          9.77677181e-02,   1.43859955e-02,   7.32049495e-02,
          6.55011237e-02,   3.44568849e-01,   4.58411306e-01,
          2.49592885e-01],
       [  0.00000000e+00,   1.61427633e-05,   4.99183894e-04,
          3.17937918e-02,   4.85810265e-02,   1.03917181e-01,
          1.25399157e-01,   1.80325225e-01,   2.97502756e-01,
          5.07521629e-01,   7.01690793e-01,   7.83944130e-01,
          3.59588116e-01,   4.82781321e-01,   2.19913769e+00,
          1.45968425e+00,   7.70302236e-01,   6.41377449e-01,
          1.36474028e-01],
       [  1.13744393e-03,   6.07185205e-03,   2.85994828e-01,
          1.50236040e-01,   7.42231190e-01,   1.22813118e+00,
          6.46776557e-01,   5.81912160e-01,   1.87843180e+00,
          1.92207563e+00,   1.52547717e+00,   1.32381713e+00,
          3.73675466e-01,   7.55523860e-01,   2.65545297e+00,
          1.27209759e+00,   1.13974738e+00,   1.16467550e-01,
          2.09415719e-01],
       [  1.38720661e-01,   3.32264960e-01,   4.35250461e-01,
          7.92550027e-01,   1.22075701e+00,   8.08324039e-01,
          3.90933961e-01,   3.94844234e-01,   4.14039224e-01,
          1.87421179e+00,   1.89138579e+00,   1.72843981e+00,
          5.95286727e-01,   1.14342570e+00,   1.61977232e+00,
          4.52039897e-01,   2.81218112e-01,   1.66340306e-01,
          9.93605703e-02],
       [  9.74169195e-01,   9.17763233e-01,   5.85691392e-01,
          4.46551383e-01,   2.51251876e-01,   1.49007021e-02,
          2.15577297e-02,   1.01625830e-01,   9.69323218e-02,
          2.31756687e-01,   8.57786715e-01,   1.20130097e-02,
          4.75826561e-02,   1.08127639e-01,   9.12918925e-01,
          1.77337572e-01,   2.07490385e-01,   8.76067802e-02,
          7.50075638e-01],
       [  7.03467131e-01,   5.82458824e-02,   9.40253865e-03,
          5.74371964e-03,   2.25005276e-03,   5.12843195e-04,
          3.22482735e-03,   6.15287630e-04,   0.00000000e+00,
          0.00000000e+00,   3.08016338e-03,   7.09070871e-03,
          1.34970576e-01,   2.69089937e-01,   5.28816462e-01,
          7.75388122e-01,   9.10060704e-02,   3.86802018e-01,
          5.62999070e-01],
       [  1.45083088e-02,   1.42366753e-03,   2.19852012e-03,
          8.08690325e-04,   2.21124804e-03,   1.78905274e-03,
          1.85610738e-03,   2.66821240e-03,   1.23740488e-03,
          7.45671510e-04,   1.78346480e-03,   6.83180336e-03,
          2.30602473e-02,   6.35242537e-02,   5.68188019e-02,
          7.31670763e-03,   6.97802007e-03,   1.26199163e-02,
          2.35131755e-02],
       [  6.02249238e-05,   2.92432378e-04,   1.13030383e-03,
          5.85175178e-04,   1.46557658e-03,   1.88808236e-03,
          3.10065225e-03,   3.55264964e-03,   4.15862398e-03,
          7.17980415e-03,   4.59075347e-03,   2.15481054e-02,
          4.47589159e-03,   6.03925623e-03,   9.55558382e-03,
          1.08743245e-02,   4.59385756e-03,   6.76350715e-03,
          1.05188731e-02],
       [  1.71982509e-04,   7.66781232e-05,   2.50212825e-03,
          4.58050938e-03,   3.75257153e-03,   3.77368135e-03,
          3.04756756e-03,   4.28559305e-03,   1.21471193e-02,
          1.85669716e-02,   2.26231515e-02,   2.37028543e-02,
          7.72741623e-03,   1.21803358e-02,   7.17204344e-03,
          1.18307828e-03,   9.51678026e-03,   8.88597034e-03,
          1.38688069e-02],
       [  7.85935298e-03,   1.02338903e-02,   2.00772509e-02,
          1.29666738e-02,   8.32718238e-03,   7.63056008e-03,
          6.14076946e-03,   9.90855228e-03,   1.00889169e-02,
          1.48749352e-02,   1.88460555e-02,   8.26913025e-03,
          9.51088127e-03,   7.60696689e-03,   1.25758338e-03,
          4.51376487e-04,   1.18463056e-03,   2.09514447e-03,
          2.51206243e-03],
       [  2.33238097e-02,   2.73073465e-02,   3.22693810e-02,
          1.88162532e-02,   5.83653990e-03,   3.47224623e-03,
          1.80892076e-03,   9.19796061e-03,   8.73230398e-03,
          1.45570468e-02,   1.55864581e-02,   1.42422626e-02,
          1.35748219e-02,   1.19139794e-02,   3.64764361e-03,
          5.92625700e-04,   2.55024619e-03,   1.44043122e-03,
          1.51959271e-03],
       [  4.65045050e-02,   4.04065773e-02,   2.90905014e-02,
          1.47327557e-02,   6.68620877e-03,   5.12005016e-03,
          4.09746822e-03,   5.41186146e-03,   1.08640799e-02,
          1.43946875e-02,   1.87681355e-02,   2.52960213e-02,
          1.79439243e-02,   1.72994547e-02,   7.33191893e-03,
          4.66867350e-03,   1.00023043e-03,   9.08961752e-04,
          2.14481447e-03],
       [  7.23621100e-02,   4.10851948e-02,   8.70250165e-03,
          5.37367724e-03,   2.11873767e-03,   3.15994583e-03,
          2.98547978e-03,   5.60216000e-03,   5.73099125e-03,
          9.40564275e-03,   1.04456097e-02,   1.35018695e-02,
          2.56229118e-02,   2.75842566e-02,   2.80958600e-02,
          3.02949995e-02,   4.40449081e-03,   1.03189505e-03,
          1.60216924e-03]], dtype=float32)

Appreciate any help with getting this function to work.
/M


More information about the SciPy-User mailing list