function [internal, external] = setangles(mode, preInternal, preExternal, ...
    varargin)

% SETANGLES - set signal internal and external angles
%
% [internal, external] = setangles(mode, preInternal, preExternal)
%   preInternal and preExternal drawn from file and cannot be 'OPTIMAL'
% [internal, external] = setangles(mode, preInternal, preExternal, nSim)
%   preInternal and preExternal directly input into makelist and cannot
%   be 'OPTIMAL' 
% [internal, external] = setangles(mode, preInternal, preExternal, nSim, ...
%       detId)
%   preInternal and preExternal can be 'OPTIMAL'
%
% mode            operating mode of makelist
% preInternal     intermediate internal angles
% preExternal     intermediate external angles
% nSim            (optional) number of angles to generate
% detId           (optional) detector identifier
%
% internal  determined internal angles
% external  determined external angles

% $Id: setangles.m,v 1.2 2004/05/10 20:26:14 stuver Exp $

% DETERMINE arguments for setinternal and setexternal based on varargin
% GET internal and external from setinternal and setexternal

[internal, external] = deal([], []);

if length(varargin) == 0 
    % preInternal and preExternal drawn from file and cannot be 'OPTIMAL'
    % Therefore, do not need nSim or detID
    internal = setinternal(mode, preInternal);
    external = setexternal(mode, preExternal);
elseif length(varargin) == 1
    % preInternal and preExternal directly input into makelist and cannot
    % be 'OPTIMAL'
    % Therefore, do not need detID but do need nSim
    nSim = varargin{1};
    internal = setinternal(mode, preInternal, nSim);
    if isempty(internal)
        error('makelist -> setangles: setinternal returned empty internal')
    end
    external = setexternal(mode, preExternal, nSim);
    if isempty(external)
        error('makelist -> setangles: setexternal returned empty external')
    end
    
elseif length(varargin) == 2
    % preInternal and preExternal can be 'OPTIMAL'
    % Therefore, detId is needed.  nSim may or may not be; if not, assume
    % varargin{1} still exists
    nSim = varargin{1};
    detId = varargin{2};
    internal = setinternal(mode, preInternal, nSim);
    if isempty(internal)
        error('makelist -> setangles: setinternal returned empty internal')
    end
    external = setexternal(mode, preExternal, nSim, detId);
    if isempty(external)
        error('makelist -> setangles: setexternal returned empty external')
    end
else
    error(['makelist -> setangles: input is (mode, preInternal, '...
            'preExternal) | (mode, preInternal, preExternal, nSim) | '...
            '(mode, preInternal, preExternal, nSim, detId)'])
end

return