function data = clean(data, bad, arg)

% data = clean(data, bad, arg)
% This function takes an array, and converts invalid numbers to NaNs.
%   data:       array to be cleaned
%   bad:        value that corresponds to an invalid entry
%   arg:        = 1 if data>bad is test for invalid
%               = 2 if data<bad is test for invalid
%               otherwise use test abs(data-bad) < arg for invalid...

if arg == 1
    nok = find(data > bad);
    data(nok) = NaN;
elseif arg == 2
    nok = find(data < bad);
    data(nok) = NaN;
else
    nok = find(abs(data-bad) < arg);
    data(nok) = NaN;
end;

return