% TIMESHIFT for AO/tsdata objects, shifts data in time by the specified value in seconds.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: TIMESHIFT for AO/tsdata objects, shifts data in time by the
%              specified value.
%
% This method does no fancy interpolation it just shifts the start time of
% the first sample by the given amount relative to t0.
%
% CALL:        b = timeshift(a, offset)
%              bs = timeshift(a1,a2,a3, offset)
%              bs = timeshift(a1,a2,a3,...,pl)
%              bs = timeshift(as,pl)
%              bs = as.timeshift(pl)
%
% INPUTS:      aN   - input analysis objects
%              as   - input analysis objects array
%              pl   - input parameter list
%
% OUTPUTS:     bs   - array of analysis objects, one for each input
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'timeshift')">Parameters Description</a>
%
% VERSION:     $Id: timeshift.m,v 1.33 2011/09/16 04:58:35 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = timeshift(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, rest] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
  [pl, ~,         rest] = utils.helper.collect_objects(rest(:), 'plist', in_names);
  [offsetIn, ~,   ~]    = utils.helper.collect_objects(rest(:), 'double', in_names);
  
  % Decide on a deep copy or a modify
  bs = copy(as, nargout);
  
  % Combine input and default PLIST
  usepl = combine(pl, getDefaultPlist);
  
  % Get the offset
  if ~isempty(offsetIn)
    offset = offsetIn;
    usepl.pset('offset', offset);
  else
    offset = usepl.find('offset');
  end
  
  % Check input analysis object
  for jj = 1:numel(bs)
    % Which data type do we have
    switch class(bs(jj).data)
      case 'tsdata'
        
        % Add the new offset
        if ~isempty(offset)
          bs(jj).data.setToffset(bs(jj).data.toffset + offset*1000);
        end
        
        % Add history
        bs(jj).addHistory(getInfo('None'), usepl, ao_invars(jj), bs(jj).hist);
        
      case {'fsdata', 'cdata', 'xydata'}
        error('### I don''t work for frequency-series, xy and constant data.');
        
      otherwise
        error('### unknown data type. They can not be addded.')
    end
  end
  
  % Set output
  varargout = utils.helper.setoutputs(nargout, bs);
  
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.helper, '$Id: timeshift.m,v 1.33 2011/09/16 04:58:35 hewitson Exp $', sets, pl);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------
function plout = getDefaultPlist()
  persistent pl;
  if ~exist('pl', 'var') || isempty(pl)
    pl = buildplist();
  end
  plout = pl;
end

function pl = buildplist()
  pl = plist();
  
  % offset
  p = param({'offset', 'Offset in seconds to shift the data in time.'}, paramValue.DOUBLE_VALUE(0));
  pl.append(p);
  
end


