% DROPDUPLICATES drops all duplicate samples in time-series AOs.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DROPDUPLICATES drops all duplicate samples in time-series AOs. Duplicates
%                are identified by having a two consecutive time stamps
%                closer than a set tolerance.
%
% CALL:        bs = dropduplicates(as)
%
% INPUTS:      as  - array of analysis objects
%              pl  - parameter list (see below)
%
% OUTPUTS:     bs  - array of analysis objects, one for each input
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'dropduplicates')">Parameters Description</a>
%
% VERSION:     $Id: dropduplicates.m,v 1.24 2011/04/08 08:56:13 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = dropduplicates(varargin)

  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end

  import utils.const.*
  utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
  
  % Collect input variable names
  in_names = cell(size(varargin));
  for ii = 1:nargin,in_names{ii} = inputname(ii);end

  % Collect all AOs
  [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
  [pl, pl_invars] = utils.helper.collect_objects(varargin(:), 'plist', in_names);

  % Decide on a deep copy or a modify
  bs = copy(as, nargout);

  % Combine plists
  pl = parse(pl, getDefaultPlist);

  % Get tolerance
  tol = find(pl, 'tol');

  % Get only tsdata AOs
  for j=1:numel(bs)
    if isa(bs(j).data, 'tsdata')
      d = abs(diff(bs(j).data.getX));
      idx = find(d<tol);
      utils.helper.msg(msg.PROC1, 'found %d duplicate samples', numel(idx));
      % Wipe out x samples
      if ~isempty(bs(j).data.x)
        bs(j).data.x(idx) = [];
      end
      % Wipe out y samples
      bs(j).data.y(idx) = [];
      % Wipe out error
      if numel(bs(j).data.dx) > 1
        bs(j).data.dx(idx) = [];
      end
      if numel(bs(j).data.dy) > 1
        bs(j).data.dy(idx) = [];
      end
      % set name
      bs(j).name = sprintf('dropduplicates(%s)', ao_invars{j});
      % Add history
      bs(j).addHistory(getInfo('None'), pl, ao_invars(j), bs(j).hist);
    else
      warning('!!! Skipping AO %s - it''s not a time-series AO.', ao_invars{j});
      bs(j) = [];
    end
  end

  % Set output
  if nargout == numel(bs)
    % List of outputs
    for ii = 1:numel(bs)
      varargout{ii} = bs(ii);
    end
  else
    % Single output
    varargout{1} = bs;
  end
end

%--------------------------------------------------------------------------
% Get Info Object
%--------------------------------------------------------------------------
function ii = getInfo(varargin)
  if nargin == 1 && strcmpi(varargin{1}, 'None')
    sets = {};
    pl   = [];
  else
    sets = {'Default'};
    pl   = getDefaultPlist;
  end
  % Build info object
  ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: dropduplicates.m,v 1.24 2011/04/08 08:56:13 hewitson Exp $', sets, pl);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------

function plout = getDefaultPlist()
  persistent pl;  
  if exist('pl', 'var')==0 || isempty(pl)
    pl = buildplist();
  end
  plout = pl;  
end

function pl = buildplist()
  pl = plist();
  
  % tol
  p = param({'tol','The time interval tolerance to consider two consecutive samples as duplicates.'}, ...
    {1, {5e-3}, paramValue.OPTIONAL});
  pl.append(p);
  
end


