function [msg,pulse,scan,comment,type,antenna] = expparts(s)

%[MSG,PULSE,SCAN,COMMENT,TYPE,ANTENNA] = EXPPARTS(NAME)
%
% Parse an EISCAT experiment name string, as specified 
% in the document
%
% TvE: Experiment Name Conventions Proposal to SAC 16/06/01
%
% If there are no errors, MSG is returned empty, else it
% contains an error message. The MSG should always be checked
% by the caller, because if there is an error, all the other
% returned values are undefined (ill-defined).
%
% Those parts that are not defined in NAME are returned empty.
%
% Note1. Leading and trailing blanks are removed from NAME.
% Note2. Blanks and other 'non-standard' characters are allowed
%        where not explicitly forbidden in the Proposal.
%
% See test_expparts.m for a test environment, and fullexp.m
% for an inverse transformation.
%
% 19-Jun-2001 Jm

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

msg = '';
pulse = '';
scan = '';
comment = '';
type = '';
antenna = '';

% remove possible leading and trailing blanks

s = deblank(s(end:-1:1));
s = deblank(s(end:-1:1));

% (1) handle some special cases

if isempty(s)
    msg = 'empty';
    return;
end

if length(s) > 32
    msg = 'too long';
    return;
end

if s(1) == '_' | s(1) == '@'
    msg = 'PULSE missing';
    return;
end

% (2) locate all special characters in the string

K0 = find(s == '@');
K = find(s == '_');
K1 = union(K0,K);


% (3) everyting up to but not including the first special is the PULSE

if ~isempty(K1)
    pulse = s(1:K1(1)-1);
else
    pulse = s;
end

% (4) Get the mandatory ANTENNA, and at the same time,
% check that only one '@' exists in the string.

if isempty(K0)
    % no '@' found
    msg = 'ANTENNA missing';
    return;
elseif length(K0) > 1
    % More than one '@' found
    msg ='misplaced @';
    return;
end

if K0 == length(s)
    % '@' is the last character in the string
    msg = 'ANTENNA illegal';
    return
end
[msg,antenna] = get_antenna(s(K0+1:end));
if ~isempty(msg)
    return
end

% The string up to the '@' is:

s = s(1:K0-1);

% It must not be empty

if isempty(s)
    % No chars before the '@'
    msg = 'PULSE missing';
    return
end

% if there are now underscores in s , we are done

if isempty(K)
    return;
end

% The char before '@' must not be an underscore

if s(end) == '_'
    msg = 'TYPE illegal';
    return;
end

% There must not be two underscores immediately after PULSE

if length(K) > 1 & K(2) == K(1)+1
    msg = 'COMMENT must start with an upper case letter';
end

% (5) We check for TYPE -- this would be the string after the last '_'

ant_str = s(K(end)+1:end);
[type_msg,type] = get_type(ant_str);
if ~isempty(type_msg) 
    % the last part was not TYPE, but it still could be either
    % COMMENT or SCAN, so this it not an error (yet).
    ;
else
    % we found TYPE at the end of s, chop it from s
    s = s(1:K(end)-1);
end
%msg = '';

% (6) Remove PULSE_ from the beginning of s

s = s(K(1)+1:end);

% (7) Now s may at most contain SCAN and COMMENT

if isempty(s)
    % No SCAN or COMMENT -- we are done
    return
end

% (8) If the first char of s is lower case a-z, we have SCAN.
% Isolate and remove it

if s(1) >= 'a' & s(1) <= 'z'
    [scan,s] = get_word(s);
end

% (9) The remaining part of s can now at most be a COMMENT

if isempty(s)
    % No COMMENT, we are done.
    return
end

% The COMMENT must start with upper case A-Z, but is otherwise
% arbitrary (we have checked earlier that we do not have any '@' here).

if s(1) >= 'A' & s(1) <= 'Z'
    comment = s;
else
    msg = 'COMMENT must start with capical letter';
end
%end expparts


function [msg,type] = get_type(s)
%----------------------------------
msg = ''; 
type = '';
if length(s) < 2
    msg = ['Illegal TYPE (' s ')'];
else
    types = {'CP','FI','FR','GE','NO','NI','SW','UK','EI','3P','SP'};
    K = strmatch(s,types,'exact');
    if isempty(K)
        msg = ['Illegal TYPE (' s ')'];
    else
        type = s;
    end
end
return
%end get_type


function [msg,antenna] = get_antenna(s)
%--------------------------------------
msg = ''; 
antenna = '';
if length(s) < 3
    msg = ['Illegal ANTENNA (' s ')'];
else
    antnames = {'uhf','kir','sod','vhf0','vhf1','vhf2','32m','42m'};
    k = strmatch(s,antnames,'exact');
    if isempty(k)
        msg = ['Illegal ANTENNA (' s ')'];
    else
        antenna = s;
    end
end
return
%end get_antenna

    
function [w,u] = get_word(s)
%---------------------------
% w = anything in s up to but not including first '_' 
% u = anything in s after the first '_'

if length(s) == 0
    w = '';
    u = '';
    return
end
K = find(s == '_');
if isempty(K)
    w = s;
    u = '';
else
    w = s(1:K(1)-1);
    u = s(K+1:end);
end
return
%end get_word        
