Changeset 17697


Ignore:
Timestamp:
04/09/14 17:04:50 (11 years ago)
Author:
cborstad
Message:

CHG: by default, do not fill nan's using the spline fit, rather mark them using nearest neighbors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/interp/interp.py

    r17655 r17697  
    22import numpy as npy
    33from scipy.interpolate import CloughTocher2DInterpolator, Rbf
     4from scipy.spatial import cKDTree
    45try:
    56        import matplotlib.pyplot as plt
     
    89                        Set plotonly=False in function call'
    910
    10 def MeshSplineToMesh2d(x,y,data,xi,yi,tol=1e-6,**kwargs):#{{{
     11def MeshSplineToMesh2d(x,y,data,xi,yi,tol=1e-6,fill_nans=False,**kwargs):#{{{
    1112        '''
    1213        Piecewise cubic, C1 smooth, curvature-minimizing interpolant in 2D.
     
    2122        xi,yi:          coordintes to interpolate data onto
    2223        tol:                    tolerance for gradient estimation (default 1e-6)
     24        fill_nans:      fill nan's (holes) in data using the spline fit?
    2325        **kwargs:       optional keywork arguments:
    2426                                        maxiter: maximum iterations in gradient estimation
     
    5759        mask=npy.isnan(subdata)
    5860        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."
    6164        subdata=npy.delete(subdata,ind)
    6265        points=npy.delete(points,ind,axis=0)
     
    6871
    6972        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]
    7083
    7184        return interpdata
    7285#}}}
    73 def GridSplineToMesh2d(x,y,data,xi,yi,default_value=npy.nan,plotonly=False):#{{{
     86def GridSplineToMesh2d(x,y,data,xi,yi,default_value=npy.nan,plotonly=False,fill_nans=False):#{{{
    7487        '''
    7588        python analog to InterpFromGridToMesh.  This routine uses
     
    90103                                                points (defaults to nan if not specified)
    91104        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?
    93106
    94107        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)
    96109
    97110        Examples:
     
    130143        mask=npy.isnan(flatsubdata)
    131144        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)
    136150
    137151        # create spline and index spline at mesh points
    138         spline=CloughTocher2DInterpolator(points,flatsubdata)
     152        spline=CloughTocher2DInterpolator(goodpoints,goodsubdata)
    139153        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]
    140163
    141164        return interpdata
Note: See TracChangeset for help on using the changeset viewer.