% SORT the values in the AO.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: SORT sorts the values in the input data.
%
% CALL: ao_out = sort(ao_in);
%       ao_out = sort(ao1, pl1, ao_vector, ao_matrix, pl2);
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'sort')">Parameters Description</a>
%
% VERSION: $Id: sort.m,v 1.24 2011/04/08 08:56:13 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = sort (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] = 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
  sdir = find(pl, 'dir');
  dim  = find(pl, 'dim');
  
  %% go through analysis objects
  for j=1:numel(bs)
    % Sort values
    switch lower(dim)
      case 'x'
        if isa(bs(j).data, 'cdata')
          error('### Sort to the x-axis of an AO with cdata is not possible');
        end
        [mx, idx] = sort(bs(j).data.getX, 1, sdir);
        my = bs(j).data.y(idx);
      case 'y'
        [my, idx] = sort(bs(j).y, 1, sdir);
        if ~isa(bs(j).data, 'cdata')
          mx = bs(j).data.getX(idx);
        end
      otherwise
        error('### can''t sort along dimension ''%s''.', dim);
    end
    
    % Sort the error dx and dy and enbw
    if isprop(bs(j).data, 'dx')
      if numel(bs(j).data.dx) > 1
        mdx = bs(j).data.dx(idx);
      else
        mdx = bs(j).data.dx;
      end
    else
      mdx = [];
    end
    if numel(bs(j).data.dy) > 1
      mdy = bs(j).data.dy(idx);
    else
      mdy = bs(j).data.dy;
    end
    if isprop(bs(j).data, 'enbw')
      if numel(bs(j).data.enbw) > 1
        menbw = bs(j).data.enbw(idx);
      else
        menbw = bs(j).data.enbw;
      end
    else
      menbw = [];
    end
    
    % set new data
    if ~isa(bs(j).data, 'cdata')
      bs(j).data.setXY(mx,my);
    else
      bs(j).data.setY(my);
    end
    
    % set new error
    if ~isempty(mdx)
      bs(j).data.setDx(mdx);
    end
    if ~isempty(mdy)
      bs(j).data.setDy(mdy);
    end
    if ~isempty(menbw)
      bs(j).data.setEnbw(menbw);
    end
    
    % set data name
    bs(j).name = sprintf('sort(%s)', ao_invars{j});
    % Add 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

%--------------------------------------------------------------------------
% 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: sort.m,v 1.24 2011/04/08 08:56:13 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();
  
  % Dim
  p = param({'dim', 'Sort on the specified axis.'}, {2, {'x', 'y'}, paramValue.SINGLE});
  pl.append(p);
  
  % Dir
  p = param({'dir', 'Direction of sort.'}, {1, {'ascend', 'descend'}, paramValue.SINGLE});
  pl.append(p);
end
% END


% PARAMETERS:
%
%           'dim'  - sort along the specified dimension: 'x' or 'y'
%                    [default: 'y']
%           'dir'  - select direction of sort: 'ascend' or 'descend'
%                    [default: 'ascend']
%
