function list = pregenlist(varargin)

% PREGENLIST - alternate listing function for pre-generated waveform
%
% list = pregenlist(inputFileName, startSamp, external, detId);
%   Generate list; 1 simulation
% list = pregenlist(nSim, inputFileName, startSamp, external, detId);
%   Generate list; nSim times
%
% nSim              number of simulations to generate
% inputFileName     1x1 or 1x2 cell containing a string with name of input 
%                   file (must include file extention) and an optional
%                   field path for *.ilwd files 
% startSamp         1x2 vector defining range of allowed start times in
%                   samples wrt the beginning of data set
% external          struct or vector with sky location & polarization angle 
%                   or string 'RANDOM' or 'OPTIMAL'; if struct, must have 
%                   fields x, phi and psi
% detId             (optional) detector identifier string; only needed for
%                   'OPTIMAL' external angles
%
% list              Cell array containing simulations list.  Each row is a
%                   simulation. Each component of the row represent:
%                   [simId ampl startSamp int1 int2 ext1 ext2 ext3], in this 
%                   order. N.B. startSamp is now a scalar - start time has 
%                   been determined
%
% SEE ALSO: MAKELIST, GRAVEN

% $Id: pregenlist.m,v 1.10 2005/04/08 18:07:09 stuver Exp $

% INITIALIZE output variable
% CHECK varargin for error
% SET simId to inputFileName (parse if needed)
% SET ampl to -1 to mark log file as using pre-generated waveform
% SET start time
% SET int1 and int2 (internal angles) to optimal orientation
% SET external angles
% ASSEMBLE list
% RETURN list

% Initialize list
list = {};

% Check varargin for error
if length(varargin) ~= 4 & length(varargin) ~= 5
    error(['pregenlist: varargin can only be [inputFileName, ' ...
            'startSamp, external, detId] or [nSim, inputFileName, ' ...
            'startSamp, external, detId]'])
elseif length(varargin) == 4
    [inputFileName, startSamp, external, detId] = deal(varargin{:});
    % Determine number of simulations in the list
    nSim = size(inputFileName, 1); 
elseif length(varargin) == 5
    [nSim, inputFileName, startSamp, external, detId] = deal(varargin{:});
    inputFileName = repmat(inputFileName, nSim, 1);
    startSamp = repmat(startSamp, nSim, 1);
    external = repmat(external, nSim, 1);
end

for iSim = 1:nSim
    % Set simId to be the inputFileName
    if strcmp(class(inputFileName(iSim,:)), 'cell') & ...
            length(inputFileName(iSim,:)) == 1
        simId{iSim} = [inputFileName{iSim,1}];
        if ~strcmp(class(simId{iSim}), 'char')
            simId{iSim}
            error('pregenlist: simId must be a string')
        end
    elseif strcmp(class(inputFileName(iSim,:)), 'cell') & ...
            length(inputFileName(iSim,:)) == 2
        simId{iSim}= [inputFileName{iSim,1} ',' inputFileName{iSim,2}];
        if ~strcmp(class(simId{iSim}), 'char')
            simId{iSim}
            error('pregenlist: simId must be a string')
        end
    elseif strcmp(class(inputFileName(iSim,:)), 'cell') & ...
            length(inputFileName(iSim,:)) == 4
        simId{iSim}= [inputFileName{iSim,1} ',' inputFileName{iSim,2} ';' inputFileName{iSim,3} ',' inputFileName{iSim,4}];
        if ~strcmp(class(simId{iSim}), 'char')
            simId{iSim}
            error('pregenlist: simId must be a string')
        end
    elseif strcmp(class(inputFileName(iSim,:)), 'char')
        simId{iSim} = [inputFileName(iSim,:)];
    else
        inputFileName{iSim}
        error('pregenlist: simId must be either cell or char class')
    end

    % Set ampl
    ampl(iSim) = -1;
    % This -1 in the ampl holder of the list is the flag that will mark the
    % log file as being produced for a pre-generated waveform.

    % Pick start time
    [startSamp1(iSim), startSamp2(iSim)] = deal(startSamp(iSim,1), ...
        startSamp(iSim,2));
    nuStartSamp(iSim) = pickstime(startSamp1(iSim), startSamp2(iSim));
    if isempty(startSamp(iSim))
        error('pregenlist: pickstime returned empty startSamp')
    end

    % Assume optimal orientation angles for the source (this would leave the
    % time series unchanged if run through makett)
    int1(iSim) = 1;
    int2(iSim) = 0;

    % Set external angles
    if ~strcmp(class(external), 'cell')
        nuExternal(iSim,:) = setexternal(-2, external(iSim,:), 1, detId);
    elseif strcmp(class(external), 'cell')
        nuExternal(iSim,:) = setexternal(-1, external{iSim}, 1, detId);
    end

    if isempty(external)
        error('pregenlist: setexternal returned empty external')
    end

end

% Assemble list
for k=1:size(simId,2)
    list(k,:) = {simId{:,k}, ampl(k), nuStartSamp(k), int1(k), int2(k),...
        nuExternal(k,1), nuExternal(k,2), nuExternal(k,3)};
end

return