function [x]=rngdelchk(x,rd,minp)
%
% function rngdelchk(x,rd,minp);
%
%   range and delta check puts "NaN" in bad values
%
%   x = some time series
%  rd = a 3-element vector [min max delta]
% minp= minumum number of bad points to start plotting
%       ie you don't want to look at 1000's of bad point plots
%        but maybe you'd like to see 20.
%  [x] = cleaned x
%
%%%%%%%%%%%%%%%%%%%%%%%%   Range Check & Fix
%
    id1=find(x<rd(1)|x>rd(2));
    if length(id1)>0;
      disp([num2str(length(id1)),' values out of range']);
%
      for i=1:length(id1);
        x(id1(i))=NaN;
      end;
      
    end;
%
%%%%%%%%%%%%%%%%%%%%%%%%%  Delta check
%
    id2=find(abs(diff(x))>rd(3));
    if length(id2)>0;
      disp('');
      disp([num2str(length(id2)),' deltas out of range']);
      disp(['See record # ',num2str(min(id2)),' for first case.']);
      disp([' ']);
    % 
    %   here we plot the problem when we only have a few 
    %
      for i=1:length(id2)-1;
        if length(id2)<minp;
          if id2(i)>11&id2(i)<length(x)-10;
            plot(x(id2(i)-10:id2(i)+10));
            hold;
            plot([NaN*ones(11,1); x(id2(i)+1); NaN*ones(10,1)],'r+');
            hold off;
          end
          pause(1);
        end;
%
%        solve=menu('What to do?','remove i-1 point','remove i point','continue');
%        if solve==1;
%          x(id2(i)+1)=NaN;
%        elseif solve==2;
%          x(id2(i)+1)=NaN;
%        end;
%
% automatic fix
%
         if id2(i+1)-id2(i)==1;
           x(id2(i)+1)=NaN;
         end;
      end;
    end; 
