Using the convenience function ipol.interpolation in order to deal with missing values¶

(1) Exemplified for one dimension in space and two dimensions of the
source value array (could e.g. be two time steps).

In [4]:

# Synthetic observations (e.g. two time steps)src=np.arange(10)[:,None]vals=np.hstack((1.+np.sin(src),5.+2.*np.sin(src)))# Target coordinatestrg=np.linspace(0,20,100)[:,None]# Here we introduce missing values in the second dimension of the source value arrayvals[3:5,1]=np.nan# interpolation using the convenience function "interpolate"idw_result=ipol.interpolate(src,trg,vals,ipol.Idw,nnearest=4)nn_result=ipol.interpolate(src,trg,vals,ipol.Nearest)# Plot resultsfig=pl.figure(figsize=(10,5))ax=fig.add_subplot(111)pl1=ax.plot(trg,idw_result,'b-',label="IDW")pl2=ax.plot(trg,nn_result,'k-',label="Nearest Neighbour")pl3=ax.plot(src,vals,'ro',label="Observations")

(2) Exemplified for two dimensions in space and two dimensions of
the source value array (e.g. time steps), containing also NaN values
(here we only use IDW interpolation)

In [5]:

# Just a helper function for repeated subplotsdefplotall(ax,trgx,trgy,src,interp,pts,title,vmin,vmax):ix=np.where(np.isfinite(pts))ax.pcolormesh(trgx,trgy,interp.reshape((len(trgx),len(trgy))),vmin=vmin,vmax=vmax)ax.scatter(src[ix,0].ravel(),src[ix,1].ravel(),c=pts.ravel()[ix],s=20,marker='s',vmin=vmin,vmax=vmax)ax.set_title(title)pl.axis("tight")

In [6]:

# Synthetic observationssrc=np.vstack((np.array([4,7,3,15]),np.array([8,18,17,3]))).Tvals=np.round(np.random.uniform(size=(len(src),2)),1)# Target coordinatestrgx=np.linspace(0,20,100)trgy=np.linspace(0,20,100)trg=np.meshgrid(trgx,trgy)trg=np.vstack((trg[0].ravel(),trg[1].ravel())).transpose()result=ipol.interpolate(src,trg,vals,ipol.Idw,nnearest=4)# Now introduce NaNs in the observationsvals_with_nan=vals.copy()vals_with_nan[1,0]=np.nanvals_with_nan[1:3,1]=np.nanresult_with_nan=ipol.interpolate(src,trg,vals_with_nan,ipol.Idw,nnearest=4)vmin=np.concatenate((vals.ravel(),result.ravel())).min()vmax=np.concatenate((vals.ravel(),result.ravel())).max()fig=pl.figure(figsize=(8,8))ax=fig.add_subplot(221)plotall(ax,trgx,trgy,src,result[:,0],vals[:,0],'1st dim: no NaNs',vmin,vmax)ax=fig.add_subplot(222)plotall(ax,trgx,trgy,src,result[:,1],vals[:,1],'2nd dim: no NaNs',vmin,vmax)ax=fig.add_subplot(223)plotall(ax,trgx,trgy,src,result_with_nan[:,0],vals_with_nan[:,0],'1st dim: one NaN',vmin,vmax)ax=fig.add_subplot(224)plotall(ax,trgx,trgy,src,result_with_nan[:,1],vals_with_nan[:,1],'2nd dim: two NaN',vmin,vmax)pl.tight_layout()