close all
clear all



atsea_path = 'X:/707976_EPBuoyPrototypeAndTesting/Deployments/2022-04-01-Deployment/DecodedResults/';
atsea_filenm{1} = '2022.09.15T12.29.14';
atsea_filenm{2} = '2022.09.14T16.27.55';
atsea_filenm{3} = '2022.09.06T20.15.25';
atsea_filenm{4} = '2022.09.06T19.15.21';
atsea_filenm{5} = '2022.09.06T03.14.17';
atsea_filenm{6} = '2022.09.05T23.14.01';
atsea_filenm{7} = '2022.09.06T17.15.13';
atsea_filenm{8} = '2022.09.06T16.15.09';
atsea_filenm{9} = '2022.09.11T19.23.19';
atsea_filenm{10} = '2022.09.12T13.24.31';
atsea_filenm{11} = '2022.09.13T17.26.23';
atsea_filenm{12} = '2022.09.16T19.31.18';
atsea_filenm{13} = '2022.09.13T20.26.35';
atsea_filenm{14} = '2022.09.15T19.29.42';
BaseFolder = 'AtSeaComparisons';


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');