# [SciPy-User] [SciPy-user] 2D Interpolation

Burak1327 burak.o.cankurtaran@alumni.uts.edu...
Thu Jan 7 19:09:09 CST 2010

```Hi Bruce,

I recently received help for what you need. I've listed the code that does
the interpolation from a coarse grid to a finer grid (regular grid). It uses
an image manipulation package, "ndimage" :

# Interpolate
newx,newy,newz = mgrid[0:40:0.5, 0:40:0.5, 0:40:0.5]
coords = array([newx, newy, newz])
pes80 = ndimage.map_coordinates(pes40, coords, order=1)

So, the coarse grid is a 40x40x40 and I'm interpolating it
onto a 80x80x80 grid. This is done with the notation 0:40:0.5.
You can change the 0.5 interval length to a complex number if you want to
specify
the number intervals instead of the interval length.

Basically, the coords array holds the actual positions that the
interpolation should occur at. Obviously, get rid of the third dimension for
2D.

To do higher order interpolation, change the "order" parameter
in the map_coordinates function.

This is a link for a quick tutorial on 2D:
http://www.scipy.org/Cookbook/Interpolation

Thanks
Burak

Bruce Ford wrote:
>
> All,
>
> I'm endeavoring to interpolate global 2.5 degree data (73x144) onto a
> 1 degree grid (181x360).  I'm not sure if I'm barking up the right
> tree for a cubic spline interpolation.
>
> Below is my code based on an example I found at:
> http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html
>
> The process is hanging at the line "tck =
> interpolate.bisplrep(x,y,z,s=0)".
>
> I'm unsure if this is a bug or is there an error in my code (which is
> below)?
>
> Secondly, has anyone done a similar interpolation (from a lower
> resolution regular 2D grid to a higher regular 2D grid) that would
> share a little code?  All my efforts have been fruitless!
>
>
> Bruce
>
> (code follows...)
> *****************************************************************
> import matplotlib
> import matplotlib.pyplot as pyplot #used to build contour and wind barbs
> plots
> import matplotlib.colors as pycolors #used to build color schemes for
> plots
> import numpy.ma as M #matrix manipulation functions
> import numpy as np #used to perform simple math functions on data
> from numpy import *
> import cgi #used to easily parse form variables
> from sys import exit as die #used to kill the python script early
> from netCDF4 import Dataset  #interprets NetCDF files
> import Nio
> from scipy import interpolate
>
> filepath = "/media/BACKUP1/reanal-2/6hr/pgb/pgb.197901"
> grb_file = Nio.open_file(filepath, mode='r', options=None, history='',
> format='grb')
>
> z = grb_file.variables["HGT_2_ISBL_10"][1,1,:,:]
> print z.shape
>
> x,y = np.mgrid[90:-90:73j,0:357.5:144j]
>
> print x.shape  #(73,144)
> print y.shape  #(73,144)
> print z.shape #(73,144)
>
> xnew,ynew = np.mgrid[-90:90:180j,0:359:360j]
> print xnew.shape #(180,360)
> tck = interpolate.bisplrep(x,y,z,s=0)
> #python freezes on the above line
> znew = interpolate.bisplev(xnew[:,0],ynew[0,:],tck)
>
> ---------------------------------------
> Bruce W. Ford
> Clear Science, Inc.
> bruce@clearscienceinc.com
> http://www.ClearScienceInc.com
> 8241 Parkridge Circle N.
> Jacksonville, FL  32211
> Skype:  bruce.w.ford
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>

--
View this message in context: http://old.nabble.com/2D-Interpolation-tp27069693p27069900.html
Sent from the Scipy-User mailing list archive at Nabble.com.

```