function [aos, pls, invars] = collect_inputs(varargin, in_names)
% COLLECT_INPUTS Collect the AOs and Plists.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: COLLECT_INPUTS Collect the AOs and Plists.
%
% CALL:        [aos, pls, invars] = collect_inputs(varargin, in_names);
%
% INPUTS:      varargin: A list of analysis objects (ao's) and 
%                        parameter lists (plist's)
%              in_names: Corresponding variable names of the contents in
%                        varargin
%
% OUTPUTS:     aos:    Collection of all analysis objects
%              pls:    Collection of parameter lists
%              invars: Collection of the ao names of the corresponding ao.
%
% VERSION:     $Id: collect_inputs.m,v 1.1 2007/12/24 19:51:00 hewitson Exp $
%
% HISTORY:     08-05-07 Diepholz
%                 Creation
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% dd1 = dbstack
% dd2 = dbstatus

aos    = [];
invars = {};
pls    = plist();

%% collect input ao's, plist's and ao variable names

for j=1:numel(varargin)

  if isa(varargin{j}, 'ao')
    aos = [aos varargin{j}];

    ao_name = in_names{j};
    if isempty(ao_name)
      ao_name = 'no_ao_name';
    end

    % Memorise the variable name of the corresponding analysis object.
    % If the ao is an array or vector add the index to the variable name
    if numel(varargin{j}) == 1
      invars{end+1} = ao_name;
    else
      for ii=1:numel(varargin{j})
        [I,J] = ind2sub(size(varargin{j}),ii);
        invars{end+1} = sprintf('%s(%d,%d)', ao_name, I, J);
      end
    end
  end
  if isa(varargin{j}, 'plist')
    pls = [pls varargin{j}];
  end
end
