% CONVERT perform various conversions on the ao.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: CONVERT perform various conversions on the ao.
%
% CALL:        ao = convert(ao, pl)
%
% PARAMETERS:
% 
%              'action' - choose a conversion to make [default: none]
% 
% Possible actions:
% 
%    Unit conversions:
%         's to Hz'   - convert seconds to Hz in the yunits of this AO.
%         'Hz to s'   - convert Hz to seconds in the yunits of this AO.
% 
%    Data conversions:
%         'to cdata'  - convert the data in the AO to a cdata type.
%         'to tsdata' - convert the data in the AO to a tsdata type.
%         'to fsdata' - convert the data in the AO to a fsdata type.
%         'to xydata' - convert the data in the AO to a xydata type.
% 
% 
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'convert')">Parameters Description</a>
%
% VERSION:     $Id: convert.m,v 1.13 2011/07/01 14:40:18 ingo Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = convert(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,rest] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
  [pls,  invars, rest] = utils.helper.collect_objects(rest(:), 'plist');


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

  % Get action to perform
  action = lower(find(pl, 'action'));
  
  if isempty(action)
    % check rest
    for kk=1:numel(rest)
      if ischar(rest{kk})
        action = lower(rest{kk});
        pl.pset('action', action);
      end
    end
  end

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

  % Loop over AOs
  for j=1:numel(bs)
    
    switch lower(action)
      case ''
        % do nothing
      case 's to hz'        
        secondsToHz(bs(j));        
      case 'hz to s'
        HzToSeconds(bs(j));
      case 'to cdata'        
        tocdata(bs(j));        
      case 'to tsdata'
        totsdata(bs(j));
      case 'to fsdata'
        tofsdata(bs(j));
      case 'to xydata'
        toxydata(bs(j));
      otherwise
        error('### Unknown action requested.');
    end
    
    % Set history
    bs(j).addHistory(getInfo('None'), pl, ao_invars(j), bs(j).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

%----------------------------------------------
% Convert to xydata
function toxydata(a)  
  if isa(a.data, 'cdata')
    x = 1:numel(a.data.y);
  else
    x = a.x;
  end
  a.data = xydata(x, a.data.y);
end

%----------------------------------------------
% Convert to fsdata
function tofsdata(a)  
  if isa(a.data, 'cdata')
    x = 1:numel(a.data.y);
  else
    x = a.x;
  end
  a.data = fsdata(x, a.data.y);
end
%----------------------------------------------
% Convert to tsdata
function totsdata(a)  
  if isa(a.data, 'cdata')
    x = 1:numel(a.data.y);
  else
    x = a.x;
  end
  a.data = tsdata(x, a.data.y);
end

%----------------------------------------------
% Convert to cdata
function tocdata(a)  
  a.data = cdata(a.data.y);
end

%----------------------------------------------
% Convert any 's' units to 'Hz' in the yunits
function secondsToHz(a)  
  a.data.yunits.sToHz;
end
%----------------------------------------------
% Convert any 'Hz' units to 's' in the yunits
function HzToSeconds(a)  
  a.data.yunits.HzToS;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                               Local Functions                               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%--------------------------------------------------------------------------
% 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: convert.m,v 1.13 2011/07/01 14:40:18 ingo 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({'action', 'Choose the action to perform.'}, ...
    {1, {'', 's to Hz', 'Hz to s', 'to cdata', 'to tsdata', 'to fsdata', 'to xydata'}, paramValue.SINGLE});
  
end

