% INTERP interpolate the values in the input AO(s) at new values.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: INTERP interpolate the values in the input AO(s) at new values
%              specified by the input parameter list.
%
% CALL:        b = interp(a, pl)
%
% INPUTS:      a  - input array of AOs
%              pl - parameter list with the keys 'vertices' and 'method'
%
% OUTPUTS:     b  - output array of AOs
%
% REMARKs:    1) Matrix cdata objects are not supported.
%             2) If a time-series object is interpolated, the sample rate
%             is adjusted to the best fit of the new data.
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'interp')">Parameters Description</a>
%
% VERSION:     $Id: interp.m,v 1.43 2011/08/23 13:49:41 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = interp(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              = 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 parameters
  vertices = find(pl, 'vertices');
  method   = find(pl, 'method');
  
  utils.helper.msg(msg.PROC1, 'using %s interpolation', method);
  
  %-----------------------
  % Loop over input AOs
  for jj = 1:numel(bs)
    %----------------------------
    % Interpolate this vector
    if ~isa(bs(jj).data, 'cdata')
      x = bs(jj).x;
      y = bs(jj).y;
      dy = bs(jj).dy;
      % for tsdata, fsdata and xydata objects
      bs(jj).data.setXY(vertices, interp1(x,y,vertices, method, 'extrap'));
      if isa(bs(jj).data, 'tsdata') 
        % here we have to set the toffset to 0 because that information is
        % stored in the x-values until it is collapsed below. Otherwise we
        % end up with double the toffset.
        bs(jj).data.setToffset(0);
      end
      if ~isempty(dy) && numel(dy) > 1
        bs(jj).data.setDy(interp1(x, dy, vertices, method, 'extrap'));
      end
      if isprop(bs(jj).data, 'enbw')
        if ~isempty(bs(jj).data.enbw) && numel(bs(jj).data.enbw) > 1
          bs(jj).data.setEnbw(interp1(x, bs(jj).data.enbw, vertices, method, 'extrap'));
        end
      end
    else
      % for cdata object
      bs(jj).data.setY(interp1(bs(jj).data.y,vertices, method, 'extrap'));
      if ~isempty(bs(jj).dy) && numel(bs(jj).dy) > 1
        bs(jj).data.setDy(interp1(bs(jj).dy, vertices, method, 'extrap'));
      end
    end
    
    % Adjust sample rate for tsdata
    if isa(bs(jj).data, 'tsdata')
      utils.helper.msg(msg.PROC1, 'adjusting sample rate of new data to best fit');
      [fs, t0, fitted] = tsdata.fitfs(bs(jj).data.getX);
      utils.helper.msg(msg.PROC2, 'got new sample rate of %g Hz', fs);
      utils.helper.msg(msg.PROC2, 'got new t0 %g', t0);
      bs(jj).data.setFs(fs);
      if ~fitted
        bs(jj).data.collapseX;
      end
    end
    % set name
    bs(jj).name = sprintf('%s(%s)', method, ao_invars{jj});
    % Add history
    bs(jj).addHistory(getInfo('None'), pl, ao_invars, bs(jj).hist);
  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: interp.m,v 1.43 2011/08/23 13:49:41 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();
  
  % Vertices
  p = param({'vertices', 'A new set of vertices to interpolate on.'}, paramValue.EMPTY_DOUBLE);
  pl.append(p);
  
  % Method
  p = param({'method', 'Specify the interpolation method.'},{3, {'nearest', 'linear', 'spline', 'cubic'}, paramValue.SINGLE});
  pl.append(p);
  
end
