Scipy interpolation with masked data?

后端 未结 3 1963
忘了有多久
忘了有多久 2021-01-03 02:35

I am trying to interpolate a 2D array that contents masked data. I have used some of the SciPy module\'s methods available, including interp2d, bisplrep/b

3条回答
  •  無奈伤痛
    2021-01-03 03:00

    i typically follow the approach described by @mseifert, but add the following refinement if i am weary of the interpolation error through the masked areas. that seems to be one of your concerns, @hurrdrought? the idea is to propagate the mask to the interpolated result. a simple example for 1D data is:

    def ma_interp(newx,x,y,mask,propagate_mask=True):
        newy = np.interp(newx,x[~mask],y[~mask]) # interpolate data
        if propagate_mask: # interpolate mask & apply to interpolated data
            newmask = mask[:]
            newmask[mask] = 1; newmask[~mask] = 0
            newmask = np.interp(newx,x,newmask)
            newy = np.ma.masked_array(newy, newmask>0.5)
        return newy
    

提交回复
热议问题