%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% generatePlots.m
% 
% Script to plot a variety of data regarding the position solution. 
% 
% Vivek Nadkarni
% March 7, 2008
% Trimble
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [statistics] = generatePlots( posData, key, parameters )

close all; 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Set up controls
ctrl.saveMemory = 1;
ctrl.printPlots = 1;
ctrl.viewPlots = 0;

ctrl.plotPos = 1;
ctrl.plotVel = 1;
ctrl.plotHist = 1;
ctrl.plotClk = 1;
ctrl.plotVarious = 1;
if (ctrl.viewPlots)
  % Useful for interactive mode
  ctrl.plotsVisible = 'on';
  % Enable figure toolbars when we want to view plots.
  set(0,'defaultFigureToolbar','auto') ;
else
  % Useful for compiled mode
  ctrl.plotsVisible = 'off';
  % Prevent Matlab from running out of resources
  set(0,'defaultFigureToolbar','none') ;
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Initialize useful variables

% Set up time scales for plots
[ abscissa, statistics ] = computeTimeScale(posData,key);

% Select Segments of different tracking types
[ segments, statistics ] = ... 
    selectSegments( posData, key, parameters, statistics  ); 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Position Plots
[ posErr, statistics ] = ...
    plotPosError ( posData, key, parameters, segments, ctrl, ...
                   abscissa, statistics );

% Position Error Histogram 
for autoScale = 0:1
  plotPVHist ( posErr, 'Position', 'meters', segments, ctrl, ...
               ctrl.plotPos, 'Poshist.png', autoScale, ...
               parameters.posLimitMeters );
end

% Cleanup
if ctrl.saveMemory
  clear posErr;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Velocity Plots
[ velErr, statistics ] = ...
    plotVelError ( posData, key, parameters, segments, ctrl, ...
                   abscissa, statistics );  

% Velocity Error Histogram 
for autoScale = 0:1
  plotPVHist ( velErr, 'Velocity', 'meters/second', segments, ctrl, ...
               ctrl.plotVel, 'Velhist.png', autoScale, ...
               parameters.velLimitMetersPerSecond );
end

% Cleanup
if ctrl.saveMemory
  clear velErr;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Clock Offset Plots
plotClockError ( posData, key, parameters, ctrl, abscissa );
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Various Attributes
plotVariousAttributes( posData, key, ctrl, abscissa );

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

end
