[17802] | 1 | Index: ../trunk-jpl/src/m/interp/interp.py
|
---|
| 2 | ===================================================================
|
---|
| 3 | --- ../trunk-jpl/src/m/interp/interp.py (revision 17696)
|
---|
| 4 | +++ ../trunk-jpl/src/m/interp/interp.py (revision 17697)
|
---|
| 5 | @@ -1,13 +1,14 @@
|
---|
| 6 | # module for inperpolating/smoothing data
|
---|
| 7 | import numpy as npy
|
---|
| 8 | from scipy.interpolate import CloughTocher2DInterpolator, Rbf
|
---|
| 9 | +from scipy.spatial import cKDTree
|
---|
| 10 | try:
|
---|
| 11 | import matplotlib.pyplot as plt
|
---|
| 12 | except ImportError:
|
---|
| 13 | print 'could not import matplotlib, no plotting functions enabled.\
|
---|
| 14 | Set plotonly=False in function call'
|
---|
| 15 |
|
---|
| 16 | -def MeshSplineToMesh2d(x,y,data,xi,yi,tol=1e-6,**kwargs):#{{{
|
---|
| 17 | +def MeshSplineToMesh2d(x,y,data,xi,yi,tol=1e-6,fill_nans=False,**kwargs):#{{{
|
---|
| 18 | '''
|
---|
| 19 | Piecewise cubic, C1 smooth, curvature-minimizing interpolant in 2D.
|
---|
| 20 | The interpolant is guaranteed to be continuously differentiable,
|
---|
| 21 | @@ -20,6 +21,7 @@
|
---|
| 22 | data: data to be interpolated (same length as x,y)
|
---|
| 23 | xi,yi: coordintes to interpolate data onto
|
---|
| 24 | tol: tolerance for gradient estimation (default 1e-6)
|
---|
| 25 | + fill_nans: fill nan's (holes) in data using the spline fit?
|
---|
| 26 | **kwargs: optional keywork arguments:
|
---|
| 27 | maxiter: maximum iterations in gradient estimation
|
---|
| 28 |
|
---|
| 29 | @@ -56,8 +58,9 @@
|
---|
| 30 | # mask out any nan's in the data and corresponding coordinate points
|
---|
| 31 | mask=npy.isnan(subdata)
|
---|
| 32 | ind=npy.nonzero(mask)[0]
|
---|
| 33 | - if len(ind):
|
---|
| 34 | - print "WARNING: input data grid contains nan values. Spline interpolation may be questionable."
|
---|
| 35 | + if len(ind) and fill_nans:
|
---|
| 36 | + print "WARNING: filling nans using spline fit through good data points,\
|
---|
| 37 | + which may or may not be appropriate. Check results carefully."
|
---|
| 38 | subdata=npy.delete(subdata,ind)
|
---|
| 39 | points=npy.delete(points,ind,axis=0)
|
---|
| 40 |
|
---|
| 41 | @@ -67,10 +70,20 @@
|
---|
| 42 | spline=CloughTocher2DInterpolator(points,subdata,tol)
|
---|
| 43 |
|
---|
| 44 | interpdata=spline(xi,yi)
|
---|
| 45 | +
|
---|
| 46 | + if not fill_nans:
|
---|
| 47 | + # identify nan's in xi,yi using nearest neighbors
|
---|
| 48 | + xyinterp=npy.dstack([xi,yi])[0]
|
---|
| 49 | + xg,yg=npy.meshgrid(subx,suby)
|
---|
| 50 | + xydata=npy.dstack([xg.ravel(),yg.ravel()])[0]
|
---|
| 51 | + tree=cKDTree(xydata)
|
---|
| 52 | + nearest=tree.query(xyinterp)[1]
|
---|
| 53 | + pos=npy.nonzero(npy.isnan(subdata[nearest]))
|
---|
| 54 | + interpdata[pos]=subdata[nearest][pos]
|
---|
| 55 |
|
---|
| 56 | return interpdata
|
---|
| 57 | #}}}
|
---|
| 58 | -def GridSplineToMesh2d(x,y,data,xi,yi,default_value=npy.nan,plotonly=False):#{{{
|
---|
| 59 | +def GridSplineToMesh2d(x,y,data,xi,yi,default_value=npy.nan,plotonly=False,fill_nans=False):#{{{
|
---|
| 60 | '''
|
---|
| 61 | python analog to InterpFromGridToMesh. This routine uses
|
---|
| 62 | scipy.interpolate.CloughTocher2dInterpolator to create a bivariate spline
|
---|
| 63 | @@ -89,10 +102,10 @@
|
---|
| 64 | default_value: default value if points lie outside the convex hull of input
|
---|
| 65 | points (defaults to nan if not specified)
|
---|
| 66 | plotonly: plot the data to be interpolated using imshow (useful for
|
---|
| 67 | - identifying holes in data and problems with interpolation)
|
---|
| 68 | + fill_nans: fill nan's (holes) in data using the spline fit?
|
---|
| 69 |
|
---|
| 70 | Usage:
|
---|
| 71 | - interpdata=GridToMesh(x,y,data,xi,yi,default_value=npy.nan,plotonly=False)
|
---|
| 72 | + interpdata=GridToMesh(x,y,data,xi,yi,default_value=npy.nan,plotonly=False,fill_nans=False)
|
---|
| 73 |
|
---|
| 74 | Examples:
|
---|
| 75 | interpdata=GridToMesh(x_m,y_m,data,md.mesh.x,md.mesh.y,0)
|
---|
| 76 | @@ -129,15 +142,25 @@
|
---|
| 77 | # mask out any nan's in the data and corresponding coordinate points
|
---|
| 78 | mask=npy.isnan(flatsubdata)
|
---|
| 79 | ind=npy.nonzero(mask)[0]
|
---|
| 80 | - if len(ind):
|
---|
| 81 | - print "WARNING: input data grid contains nan values. Spline interpolation may be questionable."
|
---|
| 82 | - flatsubdata=npy.delete(flatsubdata,ind)
|
---|
| 83 | - points=npy.delete(points,ind,axis=0)
|
---|
| 84 | + if len(ind) and fill_nans:
|
---|
| 85 | + print "WARNING: filling nans using spline fit through good data points,\
|
---|
| 86 | + which may or may not be appropriate. Check results carefully."
|
---|
| 87 | + goodsubdata=npy.delete(flatsubdata,ind)
|
---|
| 88 | + goodpoints=npy.delete(points,ind,axis=0)
|
---|
| 89 |
|
---|
| 90 | # create spline and index spline at mesh points
|
---|
| 91 | - spline=CloughTocher2DInterpolator(points,flatsubdata)
|
---|
| 92 | + spline=CloughTocher2DInterpolator(goodpoints,goodsubdata)
|
---|
| 93 | interpdata=spline(xi,yi)
|
---|
| 94 |
|
---|
| 95 | + if not fill_nans:
|
---|
| 96 | + # identify nan's in xi,yi using nearest neighbors
|
---|
| 97 | + xyinterp=npy.dstack([xi,yi])[0]
|
---|
| 98 | + xydata=npy.dstack([xg.ravel(),yg.ravel()])[0]
|
---|
| 99 | + tree=cKDTree(xydata)
|
---|
| 100 | + nearest=tree.query(xyinterp)[1]
|
---|
| 101 | + pos=npy.nonzero(npy.isnan(flatsubdata[nearest]))
|
---|
| 102 | + interpdata[pos]=flatsubdata[nearest][pos]
|
---|
| 103 | +
|
---|
| 104 | return interpdata
|
---|
| 105 | #}}}
|
---|
| 106 | def RadialInterp(x,y,data,**kwargs):#{{{
|
---|