function [y]=interpNaN(x);
%
% function [y]=interpNaN(x);
% 
% This function replaces NaNs in the input data set x by interpolating 
% between the good points before and after.  If the first and/or last 
% point is a NaN they will be replaced with the first/last good point 
% after/before them.  This program has been written to try to replace 
% a subroutine in the tukey53H.m program provided by Tom Shay at UNC 
% since he didn't include it when he sent it to us.  
%
%

inan=find(isnan(x));
inotnan=find(~isnan(x));

y=x;
dummy=[1:(length(x))]';

while length(inan)~=0

  if inan(length(inan))==length(x)
    y(length(x))=x(inotnan(length(inotnan)));
    inan(length(inan))=[];
    fprintf('Warning:  Last point was a NaN!!.\n');
    if length(inan)==0;return;end
  end

  if inan(1)==1
    y(1)=x(inotnan(1));
    inan(1)=[];
    fprintf('Warning:  First point was a NaN!!.\n');
    if length(inan)==0;return;end
  end

  inotnan=find(~isnan(y));
  inan=find(isnan(y));

  y(inan)=interp1(dummy(inotnan),y(inotnan),dummy(inan));

  inan=[];

end




