function y=gpolate(x)

% gpolate(x)
%             extra/interpolate using an inverse distance method
%             replaces nans in a 2-D array with sensible values
%
%       x     is the 2-D array to be filled

y=x; in=find(isnan(x));
[m n]=size(x); X=1:n; Y=1:m; [X Y]=meshgrid(X,Y);
X=X+sqrt(-1)*Y; Y=X; X(in)=nan*ones(size(in));

for i=1:length(in)
  ix=closest(Y(in(i)),X,4);
  d=abs(X(ix)-Y(in(i))); d=d.^(-2);
  y(in(i))=sum(x(ix).*d)/sum(d);
end
