%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Now read the matlab configuration file
fid = fopen( 'pass2pass.config.txt', 'r' );
PolygonFile = fscanf(fid,'%s',1);
dataSet     = fscanf(fid,'%s',1);
runNum      = fscanf(fid,'%d',1);
engine      = fscanf(fid,'%s',1);
fclose(fid);

%
% Ag care more about pass-2-pass accuracy than absolute accuracy. To
% analyze this we load in the position data and truth along with a
% polygon file that defines the area of use in the field. We know we
% will over/undershoot on the turns what really matters is the area of
% the field where the crop is located. The data gets filtered to only
% include points within the polygon. We then time shift the data and
% compute the 95% offset for different shifts. This data is then written
% to a text file
%

dut_29 = load('pos.txt');
dut_data = [dut_29(:,2) , dut_29(:,11) , dut_29(:,12) , dut_29(:,13) ];

truth = load('truth.txt');
truth_data = truth(:,1:4); 


i = find(PolygonFile == '/');
dataFile = PolygonFile( (i(end)+1):end );
% Load in the polygon defining the field
poly = load(['stations/',dataFile,'.txt']);

closedPoly = poly;
len = length(poly);
closedPoly(len+1,:) = closedPoly(1,:);

fig = figure('visible','off');
plot(dut_data(:,3),dut_data(:,2),'bx');
hold on
plot(closedPoly(:,1),closedPoly(:,2),'r','linewidth',3);
legend('DUT Data','Active Area','location','best')
print('-dpng',[dataSet,'-field.png'],'-S960,720');

dt = [60, 120, 300, 600, 900, 1800]; % Timeshifts (in seconds)

[ fig, horiz_passToPass_error, per_95 ] = passToPassRegression( dut_data, truth_data, poly, dt );

figure(fig);
print('-dpng',[dataSet,'-correlation.png'],'-S960,720');

% Quick debug of the 95 percentiles - this will get archived in the
% regression system and we'll see how this changes over time with
% different versions of Stinger's PVT
fid = fopen( 'Pass2Pass.txt', 'w' );
fprintf(fid,'%d ', runNum);

for i=1:length(per_95)
  fprintf(fid,'%.3f ', per_95(i));
end
fprintf(fid,'%s\n',engine);
fclose(fid);


