function PlotCNRDiff(Band, Compiled);
%
% Band     - (=1) L1 (=2) L2
% 
% Compiled - (=0) Called by Matlab; (=1) Standalone application.
%            If the compiled flag is ommitted it will assume the
%            application is not compiled
%            

FALSE      = 0;
TRUE       = 1;

if( Compiled == FALSE )
  if(Band == 1)  % L1
    cnr = load('L1.cnr');
  else           % L2
    cnr = load('L2.cnr');
  end  
  OutputGraphicsType = '-dpng';
  FileExtension = 'png';
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.
  %
  if(Band == 1)  % L1
    fid = fopen( 'L1.cnr', 'rt' ) ;
  else           % L2
    fid = fopen( 'L2.cnr', 'rt' ) ;
  end  
  cnr = fscanf( fid, '%f', [4, inf] ) ;
  fclose( fid ) ;
  cnr = cnr' ;  % Transpose as fscanf() fills by column.
  OutputGraphicsType = '-dbitmap';
  FileExtension = 'bmp';
end

%
% 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
colour_LUT(17,1) = 128;  
colour_LUT(18,1) = 128; 
colour_LUT(19,1) = 128;

% 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
colour_LUT(17,2) = 255;
colour_LUT(18,2) = 51;
colour_LUT(19,2) = 0;

% 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
colour_LUT(17,3) = 0;
colour_LUT(18,3) = 51;
colour_LUT(19,3) = 255;

% Duplicate the colours
colour_LUT(20:38,:) = colour_LUT(1:19,:);

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

% End of Colour map setup

% figure
colour = 0;
StartTime = cnr(1,1);

% if(Band == 1)  % L1
%   subplot(2,1,1);
% else  
%   subplot(2,1,2);
% end  
hold on

for sv=1:32
  j = find(cnr(:,2) == sv);
  if(length(j) > 0)
    colour = colour + 1;
    SvPRN(colour) = sv;
    % If there is some data for this SV...
    plot((cnr(j,1)- StartTime)./3600,cnr(j,3),'x','color',colour_LUT(colour,:));
  end
end  
grid
if(Band == 2)
  xlabel('Time [Hours]');
end

ylabel('Base - Rover CNR [dBHz]');

if(Band == 1)
  title('Comparison of Base/Rover L1 CNR');
else
  title('Comparison of Base/Rover L2 CNR');
end

% if(Band == 1)
  for i=1:colour
    StringMatrix(i) = {['PRN ',int2str(SvPRN(i))]};
  end
  legend(StringMatrix,0);
% end

if(Band == 1)
  print(OutputGraphicsType,['BaseRoverL1CNR.',FileExtension]);
else
  print(OutputGraphicsType,['BaseRoverL2CNR.',FileExtension]);
end


