% MAX computes the maximum value of the data in the AO.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: MAX computes the maximum value of the data in the AO.
%
% CALL:        ao_out = max(ao_in);
%              ao_out = max(ao_in, pl);
% 
% NOTE: this does not currently work for data3D AOs.
%
% PARAMETERS: 
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'max')">Parameters Description</a>
%
% VERSION:     $Id: max.m,v 1.30 2011/04/08 08:56:16 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = max(varargin)
  
  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end
  
  % 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');
  
  pl = combine(pl, getDefaultPlist);
  
  % Decide on a deep copy or a modify
  bs = copy(as, nargout);
  

  % Apply method to all AOs
  for j=1:numel(bs)
    skip = false;
    % get max value
    if isa(bs(j).data, 'data3D')
      skip = true;
      warning('!!! Skipping AO [%s]: max doesn''t work for 3D data at the moment', bs(j).name);
    elseif isa(bs(j).data, 'data2D')
      switch lower(find(pl, 'axis'))
        case 'x'
          [mx, idx] = max(bs(j).data.getX);
          my = bs(j).data.getY(idx);
        case 'y'
          [my, idx] = max(bs(j).data.getY);
          mx = bs(j).data.getX(idx);
        otherwise
          error('### The axis must one of ''x'' or ''y''.');
      end
      d = xydata(mx, my);
      d.xunits = bs(j).data.xunits;
      d.yunits = bs(j).data.yunits;
      bs(j).data = d;
    else
      my = max(bs(j).data.getY);
      bs(j).data.setY(my);
    end
    
    if ~skip
      % Set new AO name
      bs(j).name = ['max(' ao_invars{j} ')'];
      % append history
      bs(j).addHistory(getInfo('None'), pl, ao_invars(j), bs(j).hist);
      % clear errors
      bs(j).clearErrors(pl);
    end
  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: max.m,v 1.30 2011/04/08 08:56:16 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.AXIS_2D_PLIST;
end

% END
