%-----------------------------------------------------------------------
% $Id: PlotCNRDiffTime.m,v 1.10 2007/10/09 20:35:18 vnadkar Exp $
% Copyright Trimble 2001
%
% Takes the CNR data from load_pi and plots the base minus rover C/No 
% difference as a function of time.
% 
% Stuart Riley
% 12/13/01
%-----------------------------------------------------------------------

function CNRDiff = PlotCNRDiffTime(CNRBase,CNRRover,phaseDDiff,timeStr,ElMask,dropLegend,svs);

% Get a colour map
colour_LUT = setColourCodes;

%*******************************************************************
% Plot the Base-Rover C/No difference for each satellite in a unique
% colour against time.
%*******************************************************************
figure('Visible','off');
hold on
SVindex = 0;
for SV=1:svs
  % The elevation angle is in columns 34:65 of phaseDDiff
  i = find(isfinite(CNRBase(:,SV)) & (phaseDDiff(:,SV+svs+1) >= ElMask));
  
  if(length(i) > 0)
    j = find(isfinite(CNRRover(i,SV)));
    if(length(j) > 0)
      SVindex          = SVindex + 1;
      SVValue(SVindex) = SV;
      plot( phaseDDiff(i(j),1), ...
                CNRBase(i(j),SV) - CNRRover(i(j),SV), ...
                'x', 'color',colour_LUT(SVindex,:));
      CNRDiff(1,SV) = sum( CNRBase(i(j),SV) - CNRRover(i(j),SV)) / length(j);
      CNRDiff(2,SV) = length(j);
      CNRDiff(3,SV) = std( CNRBase(i(j),SV) - CNRRover(i(j),SV));
    end
  else
    CNRDiff(1,SV) = NaN;
    CNRDiff(2,SV) = NaN;
    CNRDiff(3,SV) = NaN;
  end
end

%*******************************************************************
%    Now form the mean n bins of StepSize epochs and plot it 
%*******************************************************************
index = 1;
StepSize = 120;

% GPS Elevation in columns 34:65, GLONASS elev in columns 26:49
ElevFull = phaseDDiff(:,svs+2:2*svs+1);
% VBN Mods for datasets smaller than 120 seconds
% if (StepSize > length(CNRBase))
%   StepSize = length(CNRBase);
% end
% Initialize meanDiff
meanDiff(1,1) = NaN;
meanDiff(1,2) = NaN;
for k=StepSize:StepSize:length(CNRBase)
  Base  = CNRBase((k-StepSize+1):k,:);
  Rover = CNRRover((k-StepSize+1):k,:);
  Elev  = ElevFull((k-StepSize+1):k,:);
  i = find(isfinite(Base) & (Elev>=ElMask));
  if(length(i) > 0)
    j = find(isfinite(Rover(i)));
    % Mean time
    meanDiff(index,1) = mean(phaseDDiff((k-StepSize+1):k,1));
    % Mean CNR Diff
    meanDiff(index,2) = mean(Base(i(j)) - Rover(i(j)));
  else
    meanDiff(index,1) = NaN;
    meanDiff(index,2) = NaN;
  end
  index = index+1;
end
plot(meanDiff(:,1),meanDiff(:,2),'r-x','linewidth',2);
  

% Form the legend
if(dropLegend == 0)
  for i=1:SVindex
    if(svs == 32)
      StringMatrix(i) = {['PRN ',int2str(SVValue(i))]};
    else
      StringMatrix(i) = {['GLN ',int2str(SVValue(i))]};
    end
  end
  StringMatrix(i+1) = {[int2str(StepSize),' Epoch Mean']};
  legend(StringMatrix,4);
end
  
labelAxes([],[],[],[],1);
grid;
xlabel(['Time [GPS Sec]    ',timeStr],'Interpreter','none');

if(ElMask > 0)
  title(['Only data from epochs with an Elevation angle greater than ',num2str(ElMask,'%.2f'),'Degrees plotted']);
end

