2d interpolation on griddata with Nans results in NaNs, even after masking out Nans in original data before performing interpolating?

  python, scipy

I have a even grid with data containing Nans, I want to fill this data with Nan by interpolated values. Before interpolation, I mask out the positions with Nans. The resulted interpolated data still has Nans. How to fix this issue? I use cubic interpolation

z data:

enter image description here

x=np.linspace(0,xmax,num=np.shape(z)[0])
y=np.linspace(0,ymax,num=np.shape(z)[1])  
xgrid,ygrid=np.meshgrid(x,y) 
zphas = z.copy() # 2D data with Nans to be interpolated 
zphas = np.ma.masked_invalid(zphas) #mask invalid values
x1 = xgrid[~zphas.mask] # apply same mask to xgrid
y1 = ygrid[~zphas.mask] # apply same mask to ygrid
newarr = zphas[~zphas.mask] # apply same mask to data
zphas1 = interpolate.griddata((x1, y1), newarr.ravel(),(xgrid, ygrid),method='cubic')

This results in interpolated zphas1, but it has Nans. How to fix this? inter2D is not useful, because it does not deal with Nans

enter image description here

Source: Python Questions

LEAVE A COMMENT