function [L1Mean,L2Mean]=cnr_compare(Compiled,colour,type, ...
                                     L1_fig,L2_fig, ...
                                     L1_SigmaFig,L2_SigmaFig, ...
                                     L1MinMax,L2MinMax,MinOrMax);


%
% colour    - to plot the data with
% type      - 0 = plot the weighted mean
%             1 = plot the individual data points
% L1_fig    - Figure handler for L1 plot
% L2_fig    - Figure handler for L2 plot
%           - if the L1 & L2 handlers and missing plots into the
%             current figure with subplot(2,1,1) for L1; subplot(2,1,2)
%             for L2.

TRUE  = 1;
FALSE = 0;
if(nargin < 4)
  OneFigure = TRUE;
  MinOrMax = FALSE;
else
  OneFigure = FALSE;
  if(nargin <= 5)
    MinOrMax = FALSE;
  end  
end

% Default returned value to emtpy in case not loaded. Prevents warning.
L1Mean = [] ;

%
% set the RGB values. The colours are a combination of standard Matlab
% settings, HTML definitions and ones I made up.
%

% R
colour_LUT(1,1) = 0;    % Blue
colour_LUT(2,1) = 255;  % Red
colour_LUT(3,1) = 255;  % Magenta / HTML 4.01 fuschia
colour_LUT(4,1) = 0;    % Green / HTML 4.01 lime
colour_LUT(5,1) = 0;    % Cyan / HTML 4.01 aqua
colour_LUT(6,1) = 0;    % Black
colour_LUT(7,1) = 255;  % Yellow
colour_LUT(8,1) = 0;    % HTML 4.01 Teal
colour_LUT(9,1) = 0;    % HTML 4.01 Green
colour_LUT(10,1) = 128;  % HTML 4.01 Maroon
colour_LUT(11,1) = 0;    % HTML 4.01 Navy
colour_LUT(12,1) = 128;  % HTML 4.01 Olive
colour_LUT(13,1) = 128;  % HTML 4.01 Purple
colour_LUT(14,1) = 255;  % Horrible salmon (my creation)
colour_LUT(15,1) = 192;  % HTML 4.01 Silver
colour_LUT(16,1) = 128;  % HTML 4.01 Gray

% G
colour_LUT(1,2) = 0;    % Blue
colour_LUT(2,2) = 0;    % Red
colour_LUT(3,2) = 0;    % Magenta / HTML 4.01 fuschia
colour_LUT(4,2) = 255;  % Green / HTML 4.01 lime
colour_LUT(5,2) = 255;  % Cyan / HTML 4.01 aqua
colour_LUT(6,2) = 0;    % Black
colour_LUT(7,2) = 255;  % Yellow
colour_LUT(8,2) = 128;  % HTML 4.01 Teal
colour_LUT(9,2) = 128;  % HTML 4.01 Green
colour_LUT(10,2) = 0;    % HTML 4.01 Maroon
colour_LUT(11,2) = 0;    % HTML 4.01 Navy
colour_LUT(12,2) = 128;  % HTML 4.01 Olive
colour_LUT(13,2) = 0;    % HTML 4.01 Purple
colour_LUT(14,2) = 177;  % Horrible salmon (my creation)
colour_LUT(15,2) = 192;  % HTML 4.01 Silver
colour_LUT(16,2) = 128;  % HTML 4.01 Gray

% B
colour_LUT(1,3) = 255;  % Blue
colour_LUT(2,3) = 0;    % Red
colour_LUT(3,3) = 255;  % Magenta / HTML 4.01 fuschia
colour_LUT(4,3) = 0;    % Green / HTML 4.01 lime
colour_LUT(5,3) = 255;  % Cyan / HTML 4.01 aqua
colour_LUT(6,3) = 0;    % Black
colour_LUT(7,3) = 0;    % Yellow
colour_LUT(8,3) = 128;  % HTML 4.01 Teal
colour_LUT(9,3) = 0;    % HTML 4.01 Green
colour_LUT(10,3) = 0;    % HTML 4.01 Maroon
colour_LUT(11,3) = 128;  % HTML 4.01 Navy
colour_LUT(12,3) = 0;    % HTML 4.01 Olive
colour_LUT(13,3) = 128;  % HTML 4.01 Purple
colour_LUT(14,3) = 177;  % Horrible salmon (my creation)
colour_LUT(15,3) = 192;  % HTML 4.01 Silver
colour_LUT(16,3) = 128;  % HTML 4.01 Gray

% Scale into the range 0-1
colour_LUT = colour_LUT./255;

if(colour > 16)
  sprintf('Colour out of range\n\n')
  exit ;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Copyright 2000 Trimble Navigation Ltd
%
% Stuart Riley 12/13/2000
%
% Takes the parsed DAT file outputs and produces a graph of CNR against
% elevation.
% Assumes that the current active figure is where we want to plot the
% graph.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if( Compiled == FALSE )
  cnr = load('stats.cnr');
else
  %
  % The compiled version of Matlab does not support 
  % the "load" command or allow graphics to be output in .png format.
  % The code below solves the load problem. The graphics are saved as
  % .bmp and converted to .png using external freeware utilities.
  %
  fid = fopen( 'stats.cnr', 'rt' ) ;
  cnr = fscanf( fid, '%f', [11, inf] ) ;
  fclose( fid ) ;
  cnr = cnr' ;  % Transpose as fscanf() fills by column.
end

% Format = elevation numberEpochs MeanCNR
L1 = [cnr(:,1) cnr(:,2) cnr(:,4) cnr(:,6) cnr(:,8) cnr(:,9)];
L2 = [cnr(:,1) cnr(:,3) cnr(:,5) cnr(:,7) cnr(:,10) cnr(:,11)];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                        Plot the L1 data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Don't want to duplicate the following data
if(MinOrMax == 0)

  if(OneFigure == TRUE)
    subplot(2,1,1)
  else
    % figure(L1_fig);
    set(0, 'CurrentFigure', L1_fig);
  end

  if(type == 1)
    plot(L1(:,1), L1(:,3),'x','color',colour_LUT(colour,:),'linewidth',2);
  end
  hold on

  %
  % Calculate and plot the weighted mean for L1
  %
  k = 1;
  for i=2.5:5:87.5
    j=find(L1(:,1) == i);
    m(k) = sum(L1(j,2).* L1(j,3)) / sum(L1(j,2));
    k = k + 1;
  end
  if(type == 0)
    plot(2.5:5:87.5,m,'color',colour_LUT(colour,:),'linewidth',2);
  end
  grid on
  xlabel('Elevation Angle [Degree]');
  ylabel('L1 CNR [dBHz]');
  max_L1 = max(L1(:,3));

  L1Out(1) = sum(L1(:,2));
  L1Out(2) = sum(L1(:,3).*L1(:,2));
  L1Mean   = L1Out(2) / L1Out(1);
  
  if(OneFigure ~= TRUE)
    max_mp = ceil(max_L1/5)*5;
    axis([0 90 30 max_mp])
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %                        Plot the L2 data
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  if(OneFigure == TRUE)
    subplot(2,1,2)
  else
    % figure(L2_fig);
    set(0, 'CurrentFigure', L2_fig);
  end
  
  if(type == 1)
    plot(L2(:,1), L2(:,3),'x','color',colour_LUT(colour,:),'linewidth',2);
  end
  hold on
  
  %
  % Calculate and plot the weighted mean of the MP2
  %
  k = 1;
  for i=2.5:5:87.5
    j=find(L2(:,1) == i);
    m(k) = sum( L2(j,2).* L2(j,3)) / sum(L2(j,2));
    k = k + 1;
  end
  if(type == 0)
    plot(2.5:5:87.5,m,'-','color',colour_LUT(colour,:),'linewidth',2);
  end
  grid on
  xlabel('Elevation Angle [Degree]');
  ylabel('L2 CNR [dBHz]');
  max_L2 = max(L2(:,3));
  L2Out(1) = sum(L2(:,2));
  L2Out(2) = sum(L2(:,3).*L2(:,2));
  L2Mean   = L2Out(2) / L2Out(1);
  
  if(OneFigure == TRUE)
    if(max_L1 > max_L2)
      max_mp = ceil(max_L1/5)*5;
    else  
      max_mp = ceil(max_L2/5)*5;
    end
    axis([0 90 10 max_mp])
    subplot(2,1,1)
    axis([0 90 10 max_mp])
  else
    max_mp = ceil(max_L2/5)*5;
    axis([0 90 10 max_mp])
  end
  


 if(nargin > 5)  
  %
  % Now plot the sigmas - L1
  %
  clear m
  if(OneFigure ~= TRUE)
    %    figure(L1_SigmaFig);
    set(0, 'CurrentFigure', L1_SigmaFig);
    if(type == 1)
      plot(L1(:,1), L1(:,4),'x','color',colour_LUT(colour,:),'linewidth',2);
    end
  
    hold on
  
    %
    % Calculate and plot the weighted mean for L1
    %
    k = 1;
    for i=2.5:5:87.5
      j=find(L1(:,1) == i);
      m(k) = sum(L1(j,2).* L1(j,4)) / sum(L1(j,2));
      k = k + 1;
    end
    if(type == 0)
      plot(2.5:5:87.5,m,'color',colour_LUT(colour,:),'linewidth',2);
    end
    grid on
    xlabel('Elevation Angle [Degree]');
    ylabel('L1 CNR Sigma [dBHz]');
    max_L1 = max(L1(:,3));
  
    max_mp = ceil(max_L1/5)*5;
    axis([0 90 0 max_mp])
  end
  
  %
  % Now plot the sigmas - L2
  %
  
  if(OneFigure ~= TRUE)
    %    figure(L2_SigmaFig);
    set(0, 'CurrentFigure', L2_SigmaFig);
    if(type == 1)
      plot(L2(:,1), L2(:,4),'x','color',colour_LUT(colour,:),'linewidth',2);
    end
  
    hold on
  
    %
    % Calculate and plot the weighted mean for L2
    %
    k = 1;
    for i=2.5:5:87.5
      j=find(L2(:,1) == i);
      m(k) = sum(L2(j,2).* L2(j,4)) / sum(L2(j,2));
      k = k + 1;
    end
    if(type == 0)
      plot(2.5:5:87.5,m,'color',colour_LUT(colour,:),'linewidth',2);
    end
    grid on
    xlabel('Elevation Angle [Degree]');
    ylabel('L2 CNR Sigma [dBHz]');
    max_L2 = max(L2(:,3));
  
    max_mp = ceil(max_L2/5)*5;
    axis([0 90 0 max_mp])
  end
 end
end

%
% Now plot the min/max
%
if(nargin > 5)
 clear m
 if(OneFigure ~= TRUE)
  %    figure(L1MinMax);
  set(0, 'CurrentFigure', L1MinMax);
  if(type == 1)
    plot(L1(:,1), L1(:,5+MinOrMax),'x','color',colour_LUT(colour,:),'linewidth',2);
  end

  hold on

  %
  % Calculate and plot the weighted mean for L1
  %
  k = 1;
  for i=2.5:5:87.5
    j=find(L1(:,1) == i);
    m(k) = sum(L1(j,2).* L1(j,5+MinOrMax)) / sum(L1(j,2));
    k = k + 1;
  end
  if(type == 0)
    plot(2.5:5:87.5,m,'color',colour_LUT(colour,:),'linewidth',2);
  end
  grid on
  xlabel('Elevation Angle [Degree]');
  ylabel('L1 CNR Min/Max [dBHz]');
  max_L1 = max(L1(:,3));

  max_mp = ceil(max_L1/5)*5;
  axis([0 90 0 max_mp])
 end

 %
 % Now plot the sigmas - L2
 %

 if(OneFigure ~= TRUE)
  %    figure(L2MinMax);
  set(0, 'CurrentFigure', L2MinMax);
  if(type == 1)
    plot(L2(:,1), L2(:,5+MinOrMax),'x','color',colour_LUT(colour,:),'linewidth',2);
  end

  hold on

  %
  % Calculate and plot the weighted mean for L2
  %
  k = 1;
  for i=2.5:5:87.5
    j=find(L2(:,1) == i);
    m(k) = sum(L2(j,2).* L2(j,5+MinOrMax)) / sum(L2(j,2));
    k = k + 1;
  end
  if(type == 0)
    plot(2.5:5:87.5,m,'color',colour_LUT(colour,:),'linewidth',2);
  end
  grid on
  xlabel('Elevation Angle [Degree]');
  ylabel('L2 CNR Sigma [dBHz]');
  max_L2 = max(L2(:,3));

  max_mp = ceil(max_L2/5)*5;
  axis([0 90 0 max_mp])
 end
end

