close all
clear all



NPhases = {200 400 600 800 1000 1200 1400];

BaseFolder = 'NumPhaseConvergence';


result_folders = dir(['../' BaseFolder '/batch_results*']);

k = 600;  %Skip first 60 seconds in averages

for i = 1:length(result_folders)

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



datafile = results_table.pblogFilename;
datafile = datafile{1}(1:end-5);

files = dir(['../' BaseFolder '/' ResultsFolder '/' datafile 'matlab/*_raw.mat']);
sim_filenm{i} = files;
sim_data = load([files.folder '\' files.name]);
atsea_data = load([atsea_path atsea_filenm{i} '_raw.mat']);

atsea_res.meanPower(i) = mean(atsea_data.PC_Voltage.*(atsea_data.PC_BattCurr+atsea_data.PC_LoadCurr));

sim_res.meanPower(i) = mean(sim_data.PC_Voltage(k:end).*(sim_data.PC_BattCurr(k:end)+sim_data.PC_LoadCurr(k:end)));
idx = find(sim_data.PC_RPM < 300 & sim_data.PC_RPM > -300);
sim_res.meanPower(i) = sim_res.meanPower(i) + 30.0*length(idx)/length(atsea_data.PC_RPM);

atsea_res.meanPos(i) = mean(atsea_data.SC_Range);
sim_res.meanPos(i) = mean(sim_data.SC_Range(k:end));
atsea_res.stdDevPos(i) = std(atsea_data.SC_Range);
sim_res.stdDevPos(i) = std(sim_data.SC_Range(k:end));
atsea_res.minPos(i) = min(atsea_data.SC_Range);
sim_res.minPos(i) = min(sim_data.SC_Range(k:end));
atsea_res.maxPos(i) = max(atsea_data.SC_Range);
sim_res.maxPos(i) = max(sim_data.SC_Range(k:end));

atsea_res.rmsRPM(i) = rms(atsea_data.PC_RPM);
sim_res.rmsRPM(i) = rms(sim_data.PC_RPM(k:end));
atsea_res.minRPM(i) = min(atsea_data.PC_RPM);
sim_res.minRPM(i) = min(sim_data.PC_RPM(k:end));
atsea_res.maxRPM(i) = max(atsea_data.PC_RPM);
sim_res.maxRPM(i) = max(sim_data.PC_RPM(k:end));

atsea_res.meanLoadCell(i) = mean(atsea_data.SC_LoadCell);
sim_res.meanLoadCell(i) = mean(sim_data.SC_LoadCellUnCorrected(k:end));
atsea_res.minLoadCell(i) = min(atsea_data.SC_LoadCell);
sim_res.minLoadCell(i) = min(sim_data.SC_LoadCellUnCorrected(k:end));
atsea_res.maxLoadCell(i) = max(atsea_data.SC_LoadCell);
sim_res.maxLoadCell(i) = max(sim_data.SC_LoadCellUnCorrected(k:end));

atsea_res.meanScaleFactor(i) = mean(atsea_data.PC_Scale);
sim_res.meanScaleFactor(i) = mean(sim_data.PC_Scale);

end

atsea_res
sim_res

k = 1;
figure('Position',[217 394 878 1049]);
ah(k) = subplot(4,1,k); k = k+1;
plot(sim_res.meanPower,'x')
hold all
plot(atsea_res.meanPower,'o')
grid on
xlabel('Sim Number')
ylabel('Mean Power (W)')
legend('Sim Results','At Sea Results');

ah(k) = subplot(4,1,k); k = k+1;
plot(sim_res.meanPos,'x')
hold all
plot(atsea_res.meanPos,'o')
grid on
xlabel('Sim Number')
ylabel('Mean Position (in)')
legend('Sim Results','At Sea Results');

ah(k) = subplot(4,1,k); k = k+1;
plot(sim_res.rmsRPM,'x')
hold all
plot(atsea_res.rmsRPM,'o')
grid on
xlabel('Sim Number')
ylabel('RMS RPM')
legend('Sim Results','At Sea Results');

ah(k) = subplot(4,1,k); k = k+1;
plot(sim_res.meanScaleFactor,'x')
hold all
plot(atsea_res.meanScaleFactor,'o')
grid on
xlabel('Sim Number')
ylabel('Scale Factor')
legend('Sim Results','At Sea Results');

print('ComparisonSummary.pdf','-dpdf');