function deg = compass2degrees( cellstr )
%compass2degrees - convert compass/wind labels to true degrees.
%
%   deg = compass2degrees( cellstr ) converts the string in 'cellstr'
%   to the 0-359 degrees range or -1 on error. The compass/wind labels 
%   supported is the standard set of 45/2=22.5 degrees step, 16 in total, 
%   i.e., clockwise: N,NNE,NE,ENE,E,ESE,SE,SSE,S,SSW,SW,WSW,W,WNW,NW,NNW.
%
%   The result always point in the middle of each 22.5 degrees step, thus
%   for range calculations it should be divided in half for +/- variation.
%   This means that for example 'NNW', which is -22.5 degrees from North,
%   refers to the angle between 326.25 and 348.75 degrees.
%
%   Example:
%         deg=compass2degrees('NNW');
%         >> 
%             deg = 337.5
%
%   Ref: Harris Georgiou - <a href="matlab:web('http://xgeorgio.info')">xgeorgio.info</a> (CC-BY-SA) 26-Jul-2016

%... initialization / internal constants ...
q2=45;  q4=45/2;

switch char(cellstr)
    case 'N'
        deg=0;
    case 'NNE'
        deg=q4;
    case 'NE'
        deg=q2;
    case 'ENE'
        deg=q2+q4;
    case 'E'
        deg=90;
    case 'ESE'
        deg=90+q4;
    case 'SE'
        deg=90+q2;
    case 'SSE'
        deg=90+q2+q4;
    case 'S'
        deg=180;
    case 'SSW'
        deg=180+q4;
    case 'SW'
        deg=180+q2;
    case 'WSW'
        deg=180+q2+q4;
    case 'W'
        deg=270;
    case 'WNW'
        deg=270+q4;
    case 'NW'
        deg=270+q2;
    case 'NNW'
        deg=270+q2+q4;
    otherwise
        deg=-1;
end    
        
end
