% SUMJOIN sums time-series signals togther
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: SUMJOIN sums time-series signals togther. The start time of
%              each signal is taken in to account when summing. The signals
%              are zero-padded at non-overlapping times.
%
%         s1:   |. . + + | 0 0 0 0 0
%         s2:    0 0 | + + + + | 0 0
%         s3:    0 0 0 | + + + + . |
%
% CALL:        b = sumjoin(a1, a2, pl)
%
% EXAMPLES:
%
% a1 = ao(plist('tsfcn', 'randn(size(t))', 'fs', 10, 'nsecs', 10, ...
%               't0', 10000))
% a2 = ao(plist('tsfcn', 't', 'fs', 10, 'nsecs', 15, 't0', 15000))
% b  = sumjoin(a1,a2);
% iplot(a1,a2,b);
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'sumjoin')">Parameters Description</a>
%
% VERSION:     $Id: sumjoin.m,v 1.19 2011/04/08 08:56:12 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = sumjoin(varargin)
  
  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end
  
  %   if nargout == 0
  %     error('### join cannot be used as a modifier. Please give an output variable.');
  %   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              = utils.helper.collect_objects(varargin(:), 'plist', in_names);
  
  % Copy inputs
  bs = copy(as, nargout);
  
  % Combine plists
  pl = parse(pl, getDefaultPlist);
  
  % loop over AOs to get maximum and minimum end time
  tEnd    = 0;
  tStart  = 1e20;
  fs      = bs(1).data.fs;
  inhists = [];
  for j=1:numel(bs)
    % Only get the data type we want
    if isa(bs(j).data, 'tsdata')
      % check sample rate
      if bs(j).data.fs ~= fs
        error('### All input time-series must have the same sample rate.');
      end
      % check sampling
      if ~bs(j).data.evenly()
        error('### This method only works on regularly sampled time-series.');
      end
      % get start and stop time
      x1 = bs(j).data.getX(1);
      ts = x1 + bs(j).data.t0.utc_epoch_milli/1000;
      te = bs(j).data.nsecs + ts;
      if te > tEnd
        tEnd = te;
      end
      if ts < tStart
        tStart = ts;
      end
      % store input history
      inhists = [inhists bs(j).hist];
    else
      error('### It is not a time-series', bs(j).name);
    end
  end
  
  % clear errors
  bs.clearErrors;

  % Zero pad and sum each signal
  name = 'sumjoin(';
  yall = zeros((tEnd - tStart)*fs,1);
  for kk=1:numel(bs)
    if isa(bs(kk).data, 'tsdata')
      
      name = sprintf('%s%s, ', name, ao_invars{kk});
      
      x1   = bs(kk).x(1)   + bs(kk).t0.utc_epoch_milli/1000;
      xn   = bs(kk).x(end) + 1/fs;
      post = zeros((tEnd - xn - x1)*fs,1);
      pre  = zeros((x1 - tStart)*fs,1);
      y    = [pre; bs(kk).y; post];
      yall = yall + y;
    end
  end
  xall = linspace(0, (tEnd - tStart) -1/fs, (tEnd - tStart)*fs)';
  name = [name(1:end-2) ')'];
  
  % Fix up this AO
  out = bs(1);
  out.data.setXY(xall, yall);
  out.data.fixNsecs;
  out.data.collapseX;
  out.name = name;
  out.data.setT0(time(tStart));
  out.hist = [];
  out.addHistory(getInfo('None'), pl, ao_invars, inhists);
  
  % Set output
  varargout{1} = out;
  
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.op, '$Id: sumjoin.m,v 1.19 2011/04/08 08:56:12 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.EMPTY_PLIST;
end

