Changeset 17697
- Timestamp:
- 04/09/14 17:04:50 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/m/interp/interp.py
r17655 r17697 2 2 import numpy as npy 3 3 from scipy.interpolate import CloughTocher2DInterpolator, Rbf 4 from scipy.spatial import cKDTree 4 5 try: 5 6 import matplotlib.pyplot as plt … … 8 9 Set plotonly=False in function call' 9 10 10 def MeshSplineToMesh2d(x,y,data,xi,yi,tol=1e-6, **kwargs):#{{{11 def MeshSplineToMesh2d(x,y,data,xi,yi,tol=1e-6,fill_nans=False,**kwargs):#{{{ 11 12 ''' 12 13 Piecewise cubic, C1 smooth, curvature-minimizing interpolant in 2D. … … 21 22 xi,yi: coordintes to interpolate data onto 22 23 tol: tolerance for gradient estimation (default 1e-6) 24 fill_nans: fill nan's (holes) in data using the spline fit? 23 25 **kwargs: optional keywork arguments: 24 26 maxiter: maximum iterations in gradient estimation … … 57 59 mask=npy.isnan(subdata) 58 60 ind=npy.nonzero(mask)[0] 59 if len(ind): 60 print "WARNING: input data grid contains nan values. Spline interpolation may be questionable." 61 if len(ind) and fill_nans: 62 print "WARNING: filling nans using spline fit through good data points,\ 63 which may or may not be appropriate. Check results carefully." 61 64 subdata=npy.delete(subdata,ind) 62 65 points=npy.delete(points,ind,axis=0) … … 68 71 69 72 interpdata=spline(xi,yi) 73 74 if not fill_nans: 75 # identify nan's in xi,yi using nearest neighbors 76 xyinterp=npy.dstack([xi,yi])[0] 77 xg,yg=npy.meshgrid(subx,suby) 78 xydata=npy.dstack([xg.ravel(),yg.ravel()])[0] 79 tree=cKDTree(xydata) 80 nearest=tree.query(xyinterp)[1] 81 pos=npy.nonzero(npy.isnan(subdata[nearest])) 82 interpdata[pos]=subdata[nearest][pos] 70 83 71 84 return interpdata 72 85 #}}} 73 def GridSplineToMesh2d(x,y,data,xi,yi,default_value=npy.nan,plotonly=False ):#{{{86 def GridSplineToMesh2d(x,y,data,xi,yi,default_value=npy.nan,plotonly=False,fill_nans=False):#{{{ 74 87 ''' 75 88 python analog to InterpFromGridToMesh. This routine uses … … 90 103 points (defaults to nan if not specified) 91 104 plotonly: plot the data to be interpolated using imshow (useful for 92 identifying holes in data and problems with interpolation)105 fill_nans: fill nan's (holes) in data using the spline fit? 93 106 94 107 Usage: 95 interpdata=GridToMesh(x,y,data,xi,yi,default_value=npy.nan,plotonly=False )108 interpdata=GridToMesh(x,y,data,xi,yi,default_value=npy.nan,plotonly=False,fill_nans=False) 96 109 97 110 Examples: … … 130 143 mask=npy.isnan(flatsubdata) 131 144 ind=npy.nonzero(mask)[0] 132 if len(ind): 133 print "WARNING: input data grid contains nan values. Spline interpolation may be questionable." 134 flatsubdata=npy.delete(flatsubdata,ind) 135 points=npy.delete(points,ind,axis=0) 145 if len(ind) and fill_nans: 146 print "WARNING: filling nans using spline fit through good data points,\ 147 which may or may not be appropriate. Check results carefully." 148 goodsubdata=npy.delete(flatsubdata,ind) 149 goodpoints=npy.delete(points,ind,axis=0) 136 150 137 151 # create spline and index spline at mesh points 138 spline=CloughTocher2DInterpolator( points,flatsubdata)152 spline=CloughTocher2DInterpolator(goodpoints,goodsubdata) 139 153 interpdata=spline(xi,yi) 154 155 if not fill_nans: 156 # identify nan's in xi,yi using nearest neighbors 157 xyinterp=npy.dstack([xi,yi])[0] 158 xydata=npy.dstack([xg.ravel(),yg.ravel()])[0] 159 tree=cKDTree(xydata) 160 nearest=tree.query(xyinterp)[1] 161 pos=npy.nonzero(npy.isnan(flatsubdata[nearest])) 162 interpdata[pos]=flatsubdata[nearest][pos] 140 163 141 164 return interpdata
Note:
See TracChangeset
for help on using the changeset viewer.