function [id, f, tau, dur] = simparse(simId)

% SIMPARSE - parse simId string for needed signal information
%
% [id, f, tau, dur] = simparse(simId);
%
% simId         string containing signal information
%               N.B. Must be '~' or '_' delimited
%
% id            string desribing type of signal
% f             frequency of signal
% tau           width of Gaussian
% dur           signal duration in seconds
%
% SEE ALSO: MAKEH

% $Id: simparse.m,v 1.6 2004/04/29 21:35:46 stuver Exp $

% FIND indicies of delimiters in simId
% CHECK to make sure the correct number delimiters where found
% ASSIGN variables by the location of the delimiter
% CHECK variables for errors 

[id, f, tau, dur] = deal([]);

I = find(simId == '~' | simId == '_');

errorcheck(simId, I)

% The simulation type string is the information before the first 
% delimiter
id = simId(1:I(1)-1);  

% The frequency is the information between the first delimiter and the 
% second
f = simId(I(1)+1:I(2)-1); % Frequency (string)
f = str2num(f); % Convert string into number
% Check for errors (f must be a positive finite number)
if f < 0 | isinf(f) | isnan(f) | ~isreal(f)
    f
    error('simparse: f must be a real positive finite number')
end

% The tau is the information between the second delimiter and the last
tau = simId(I(2)+1:I(3)-1); % tau (string)
tau = str2num(tau); % Convert string into number
% Check for errors (tau must be a positive finite number)
if tau < 0 | isinf(tau) | isnan(tau) | ~isreal(tau)
    tau
    error('simparse: tau must be a real positive finite number')
end

% The duration is the information after the last delimiter
dur = simId(I(3)+1:end); % Simulation duration (string)
dur = str2num(dur); % Convert string into number
% Check for errors (dur must be a positive finite number)
if dur <= 0 | isinf(dur) | isnan(dur) | ~isreal(dur)
    dur
    error('simparse: dur must be a real finite number > 0')
end

return

% ~~~ END MAIN ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function errorcheck(simId, I)

% ERRORCHECK - check simId for errors and errors out if one is found
%
% errorcheck(simId, I)
%
% simId     string containing signal information
%           N.B. Must be '~' or '_' delimited
% I         locations of delimiters

% Check that delimiters where found
if isempty(I)
    simId
    error('simparse: simId must be ~ delimited or _ delimited')
elseif length(I) ~= 3
    % There must be 3 delimiters
    simId
    error('simparse: There must be 3 delimiters in simId')
end

spacing = diff(I);

% Delimiter 1 and 2 are consecutive
if spacing(1) == 1 
    simId
    error('simparse: Delimiter 1 and 2 are consecutive')
elseif spacing(2) == 1 
    % Delimiter 2 and 3 are consecutive
    simId
    error('simparse: Delimiter 2 and 3 are consecutive')
end

if I(1) == 1 
    % There is a delimiter at the begining of simId
    simId
    error('simparse: There is a delimiter at the begining of simId')
elseif I(end) == length(simId) 
    % There is a delimiter at the end of simId
    simId
    error('simparse: There is a delimiter at the end of simId')
end

return