close all
clear all



BaseFolder = 'AccuracyChecks';
ResultsFolder = 'latest_batch_results';

results_table = readtable(['../' BaseFolder '/' ResultsFolder '/batch_runs.log']);
table_size = size(results_table);

for i = 1:table_size(1)
  files = dir(['../' BaseFolder '/' ResultsFolder '/' results_table.pblogFilename{i} '/../matlab/*_raw.mat']);
  results_data{i} = load([files.folder '\' files.name]);

disp(num2str(i));
end


A = 0.5;
T = [10];

for i = 1:length(T)
figure
ah1 = subplot(4,1,1)
t = (results_data{i}.XB_Time-results_data{i}.XB_Time(1))*24*3600;
plot(t,results_data{i}.XB_Alt-mean(results_data{i}.XB_Alt)); 
hold all;
plot(t,A*cos(2*pi*t/T(i)));
xlabel('time (s)');
ylabel('meters');
grid on;
title(['A = ' num2str(A) 'm   T = ' num2str(T(i)) 's'])
legend('Heave pos','wave height')

ah2 = subplot(4,1,2)
t = (results_data{i}.SC_Time-results_data{i}.SC_Time(1))*24*3600;
pos = results_data{i}.SC_Range;
plot(t,results_data{i}.SC_Range)
l_pos = length(pos);
ylim([min(pos(l_pos/2:end)) max(pos(l_pos/2:end))]);
xlabel('time (s)');
ylabel('Piston Pos (in)');
grid on;

ah3 = subplot(4,1,3)
t = (results_data{i}.PC_Time-results_data{i}.PC_Time(1))*24*3600;
rpm = results_data{i}.PC_RPM;
plot(t,rpm)
ylim([min(rpm(l_pos/2:end)) max(rpm(l_pos/2:end))]);
xlabel('time (s)');
ylabel('RPM');
grid on;

ah4 = subplot(4,1,4)
t = (results_data{i}.PC_Time-results_data{i}.PC_Time(1))*24*3600;
Power = results_data{i}.PC_Voltage.*(results_data{i}.PC_LoadCurr+results_data{i}.PC_BattCurr);
plot(t,Power)
ylim([min(Power(l_pos/2:end)) max(Power(l_pos/2:end))]);
xlabel('time (s)');
ylabel('Power (W)');
legend(['Mean =' num2str(mean(Power)) 'W']);
grid on;


linkaxes([ah1 ah2 ah3 ah4],'x');

print(['MonoChromaticResults_A' num2str(A) '_T' num2str(T(i)) '.pdf'],'-dpdf','-fillpage');
end
