%-----------------------------------------------------------------------
% $Id: plotPosError.m,v 1.3 2008/03/25 22:03:07 vnadkar Exp $
% Copyright Trimble 2008
%
% Generate position error data plot
%-----------------------------------------------------------------------

function [ posErr, statistics ] = ...
    plotPosError ( posData, key, parameters, segments, ctrl, ...
                   abscissa, statistics )


if ~ctrl.plotPos
  meanErr = [0,0,0];
  stdErr = [0,0,0];
  posErr = 0;
  disp('Skipping position plots');
  return; 
end


LAT = 1;
LON = 2; 
HGT = 3; 

%% Extract and process position data
pos = posData (:,key.LAT:key.HGT);

% convert delta lat/lon to meters using d2mLat and d2mLon
[d2mLat, d2mLon] = computeD2M(parameters.refLLA);

posErr = zeros(size(posData, 1), 3);
for idx = 1:3 
  posErr(:,idx) = pos(:,idx) - parameters.refLLA(idx);
end
posErr(:,LAT) = posErr(:,LAT) * d2mLat; 
posErr(:,LON) = posErr(:,LON) * d2mLon; 


statistics = computeStatistics( posErr, segments, 'position', ...
                                statistics );
meanErr = statistics.meanPosErr;
stdErr = statistics.stdPosErr; 

for verticalView = 0:1
  for autoScale = 0:1
    plotPVFig ( posErr, meanErr, stdErr, 'm', 'Position', 'meters', ...
                'Poserr.png', segments, ctrl, abscissa, verticalView, ...
                autoScale, parameters.posLimitMeters );
  end
end

end