Woops, nevermind, I just looked at your other post.  I suppose I should listen to Mr. Kern before speaking up! :) I guess you're not projecting before interpolation?  You'll definitely need to do that in order to avoid the "dot" problems you mentioned there.   Projecting your data into a polar stereographic (or similar) projection before gridding (and basing the grid on the projected coordinates) should fix most of your problems.

I had assumed you were talking about the spline gridding artifacts present away from data points in the figure you showed in this post.  Those are much harder to avoid.

On Tue, Aug 11, 2009 at 2:11 PM, Joe Kington <jkington@wisc.edu> wrote:
It looks like it's definitely projected before interpolation.  You'll need more control that mlab.griddata can give you to make a grid with fewer artifacts

If you want more control over interpolation, you're going to need to look into more flexible methods than spline (which is what I think griddata uses).

Interpolation is 75% art and 25% science.  Basically, if you know roughly what your interpolated data should look like, you can make it look like that, without many artifacts, but it's going to take some work.  No one interpolation method works best everywhere (spline is a good first choice for smooth data, though, which is why it's used so much).

Actually, a lot of the artifacts can be reduced by fine tuning the search radius and shape.  Unfortunately, I don't think anything currently in scipy gives you the ability to do this (radius, maybe, but not shape... If I'm wrong, please let me know!).  You can do a lot with spline if you can fine tune the search neighborhood, without having to go to more complex methods.

The various kriging methods are the most flexible interpolation methods, but also the most complex and slowest.  However, if you're willing to put in the time you can do a really nice job. 

If you want to look into it, read up on geostatistics.  A fairly nice open source geostats program is SGeMS.  It does support python scripting, though I've never done much from that side of it.  I've also never been able to get it to build on linux, but the windows executable works perfectly in wine.   If you don't want to take that route, I've got several python kriging routines written (with weave, etc, so they're reasonably fast).  I can send them your way if you like. Unfortunately they're in pretty rough shape, so it's probably best to use something more refined (and less brittle)...

Sorry, that wasn't at all what you were asking, but if you do want to try more flexible methods, it will probably pay off. 

Anyway, as far as masking your data goes, the simplest thing to do would be to mask any interpolated points more than x distance away from a data point.  That shouldn't be too hard... (Though I can't think of how to do it in just a couple of lines...)  Something along these lines should work, but I haven't actually tested it...

# Input: dataX, dataY, gridX, gridY, grid, maxDist
from scipy import spatial

# Assumes dataX and dataY are row vectors
dataXY = np.vstack((dataX, dataY)).T
dataQuadtree = spatial.KDTree(dataXY)

xx,yy = np.meshgrid(gridX, gridY)
xx,yy = xx.flatten, yy.flatten
gridPoints = np.vstack((xx,yy)).T

dists, indexes = dataQuadtree.query(gridPoints, k=1, distance_upper_bound=maxDist)
mask = dists < maxDist
mask = mask.reshape(grid.shape)
grid = np.ma.masked_array(grid, mask)

(That may not run, I'm probably doing something stupid somewhere, but it's the general idea, anyway...)

Hope that helps,
-Joe


On Tue, Aug 11, 2009 at 8:54 AM, John [H2O] <washakie@gmail.com> wrote:

Answering my own question, but seeking comments.

Apparently matplotlib.mlab.griddata does exactly what I need.

However, I would be interested in know more about how to use this, and to
exert a little tighter control over it. One issue, as can be seen below, is
that there are clearly artifacts. I would like to create a masking array,
based on my raw data array, but as it has a highly irregular shape, this
doesn't seem trivial. Suggestions?

http://www.nabble.com/file/p24918109/fyi.png



--
View this message in context: http://www.nabble.com/2d-interpolation%2C-non-regular-lat-lon-grid-tp24909685p24918109.html
Sent from the Scipy-User mailing list archive at Nabble.com.

_______________________________________________
SciPy-User mailing list
SciPy-User@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user