% INTERPMISSING interpolate missing samples in a time-series.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% INTERPMISSING interpolate missing samples in a time-series. Missing samples
%               are identified as being those where the time-span between one
%               sample and the next is larger than d/fs where d is a
%               tolerance value. Missing data is then placed in the gap in
%               steps of 1/fs. Obviously this is only really correct for
%               evenly sampled time-series.
%
% CALL:        bs = interpmissing(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', 'interpmissing')">Parameters Description</a>
%
% VERSION:     $Id: interpmissing.m,v 1.30 2011/04/08 08:56:16 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = interpmissing(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 and plists
  [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
  dtol = find(pl, 'd');

  % Get only tsdata AOs
  for j=1:numel(bs)
    if isa(bs(j).data, 'tsdata')

      % capture input history
      ih = bs(j).hist;

      % find missing samples
      t    = [];
      d    = diff(bs(j).data.getX);
      idxs = find(d>dtol/bs(j).data.fs);
      utils.helper.msg(msg.PROC1, 'found %d data gaps', numel(idxs));

      % create new time grid
      count = 0;
      fs    = bs(j).data.fs;
      for k=1:numel(idxs)
        idx = idxs(k);
        if isempty(t)
          t   = bs(j).data.getX(1:idxs(1));
        end
        % now add samples at 1/fs until we are within 1/fs of the next sample
        gap   = bs(j).data.getX(idx+1) - bs(j).data.getX(idx) - 1/fs;
        tfill = [[1/fs:1/fs:gap] + bs(j).data.getX(idx)].';
        count = count + numel(tfill);
        
        if k==numel(idxs)
          t = [t; tfill; bs(j).data.getX(idx+1:end)];
        else
          t = [t; tfill; bs(j).data.getX(idx+1:idxs(k+1))];
        end
      end
      utils.helper.msg(msg.PROC1, 'filled with %d samples', count);

      % now interpolate onto this new time-grid
      if ~isempty(t)
        bs(j).interp(plist('vertices', t, 'method', find(pl, 'method')));
        bs(j).name = sprintf('interpmissing(%s)', ao_invars{j});
        % Add history
        bs(j).addHistory(getInfo('None'), pl, ao_invars(j), ih);
        % clear errors
        bs(j).clearErrors;
      else
        utils.helper.msg(msg.PROC1, 'no missing samples found in %s - no action performed.', ao_invars{j});
      end
    else
      utils.helper.msg(msg.PROC1, 'skipping AO %s - it''s not a time-series AO.', ao_invars{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: interpmissing.m,v 1.30 2011/04/08 08:56:16 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();
  
  % d
  p = param({'d','The time interval tolerance for finding missing samples.'}, {1, {1.5}, paramValue.OPTIONAL});
  pl.append(p);
  
  % Interpolation method
  pli = ao.getInfo('interp').plists;
  p = pli.params(pli.getIndexForKey('method'));
  pl.append(p);
  
end
