close all
clear all



BaseFolder = 'RunLengthTests';
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 = load([files.folder '\' files.name]);
  Power = results_data.PC_Voltage.*(results_data.PC_BattCurr+results_data.PC_LoadCurr);

  first = 599;
  for k = 1:29
      last = first+600*k;
      disp([num2str(first) ':' num2str(last)]);
    idx = first:last;
    rms_RPM(i,k) = rms(results_data.PC_RPM(idx));
    mean_Power(i,k) = mean(Power(idx));
  end
disp(num2str(i));
end


for k = 1:29;
    x(k) = k;
    y_RPM(k) = mean(rms_RPM(:,k));
    err_RPM(k) = std(rms_RPM(:,k));
    y_Power(k) = mean(mean_Power(:,k));
    err_Power(k) = std(mean_Power(:,k));
end

figure
ah1 = subplot(2,1,1)
errorbar(x,y_Power,err_Power);
ylabel('Avg Power (W)');
xlabel('length of simulation (minutes)');
grid on;
title('Effect of Simulation Length, Statistics on 50 runs with random wave-phases for each run');

ah2 = subplot(2,1,2)
errorbar(x,y_RPM,err_RPM)
ylabel('RMS RPM');
xlabel('length of simulation (minutes)');
grid on;
