% SELECT select particular samples from the input AOs and return new AOs with only those samples.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: SELECT select particular samples from the input AOs and return
%              new AOs with only those samples.
%
% CALL:        b = select(a, [1 2 3 4]);
%              b = select(a, pl)
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'select')">Parameters Description</a>
%
% VERSION:     $Id: select.m,v 1.42 2011/10/06 05:21:05 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = select(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, pl_invars, rest] = utils.helper.collect_objects(rest(:), 'plist', in_names);
  
  % Decide on a deep copy or a modify
  bs = copy(as, nargout);
  
  % Combine plists
  pl = parse(pl, getDefaultPlist);
  
  % Check for samples in arguments
  samples_in  = [];
  for jj = 1:numel(rest)
    if isnumeric(rest{jj})
      samples_in = [samples_in reshape(rest{jj}, 1, [])];
    end
  end
  
  % Get sample selection from plist
  sam = find(pl, 'samples');
  axis = find(pl, 'axis');
  if isnumeric(sam)
    samples = [samples_in reshape(find(pl, 'samples'), 1, [])];
  elseif isa(sam, 'ao')
    if isa(sam.data, 'tsdata')
      if axis == 'y'
        samples = [samples_in sam.y];
      else
        samples = [samples_in 1:len(sam)];
      end
    elseif isa(sam.data, 'xydata') || isa(sam.data, 'fsdata')
      if axis == 'y'
        samples = [samples_in reshape(sam.y, 1, [])];
      else
        samples = [samples_in reshape(sam.x, 1, [])];
      end
    end
  end
  samples = sort(samples);
  % Set the samples again to the plist because it is possible that the user
  % specified the not in the plist but direct as input.
  if ~isempty(samples_in)
    pl.pset('samples', samples);
  end
  
  % Loop over input AOs
  for jj = 1:numel(bs)
    if isa(bs(jj).data, 'cdata')
      bs(jj).data.setY(bs(jj).data.y(samples));
      if numel(bs(jj).data.dy) > 1
        bs(jj).data.setDy(bs(jj).data.dy(samples));
      end
      if isprop(bs(jj).data, 'enbw')
        if numel(bs(jj).data.enbw) > 1
          bs(jj).data.setEnbw(bs(jj).data.enbw(samples));
        end
      end
    else
      % Get x
      x = bs(jj).data.getX;
      % set new samples
      bs(jj).data.setXY(x(samples), bs(jj).data.y(samples));
      if numel(bs(jj).data.dx) > 1
        bs(jj).data.setDx(bs(jj).data.dx(samples));
      end
      if numel(bs(jj).data.dy) > 1
        bs(jj).data.setDy(bs(jj).data.dy(samples));
      end
      if isprop(bs(jj).data, 'enbw')
        if numel(bs(jj).data.enbw) > 1
          bs(jj).data.setEnbw(bs(jj).data.enbw(samples));
        end
      end
      % if this is tsdata, we may need to do some other steps
      if isa(bs(jj).data, 'tsdata')
        % recompute nsecs
        bs(jj).data.fixNsecs;
      end
    end
    % Set AO name
    bs(jj).name = sprintf('select(%s)', ao_invars{jj});
    % Add history
    bs(jj).addHistory(getInfo('None'), pl, ao_invars(jj), bs(jj).hist);
  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.sigproc, '$Id: select.m,v 1.42 2011/10/06 05:21:05 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 plo = buildplist()
  plo = plist();
  
  p = param({'samples', 'A list of samples to select.'}, paramValue.EMPTY_DOUBLE);
  plo.append(p);
  
  p = param({'axis', 'If the samples are specified with an AO then it is possible from axis you want to select the samples.'}, {1 {'x', 'y'} paramValue.SINGLE});
  plo.append(p);
  
end

% END
% PARAMETERS: 'samples'  - a list of samples to select
%
