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 = 'updated_spring';


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

figure
plot(atsea_data.PC_Scale);
hold all
plot(sim_data.PC_Scale);
title(['Run Number ' num2str(i)]);
xlabel('Scale Factor');
SF = sim_data.PC_Scale(1);
idx_PC = find(atsea_data.PC_Scale == SF);

idx_SC = find( (atsea_data.SC_Time>atsea_data.PC_Time(idx_PC(1))) & (atsea_data.SC_Time<atsea_data.PC_Time(idx_PC(end))) );
idx_TC = find( (atsea_data.TC_Time>atsea_data.PC_Time(idx_PC(1))) & (atsea_data.TC_Time<atsea_data.PC_Time(idx_PC(end))) );

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

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(idx_SC));
sim_res.meanPos(i) = mean(sim_data.SC_Range(k:end));
atsea_res.stdDevPos(i) = std(atsea_data.SC_Range(idx_SC));
sim_res.stdDevPos(i) = std(sim_data.SC_Range(k:end));
atsea_res.minPos(i) = min(atsea_data.SC_Range(idx_SC));
sim_res.minPos(i) = min(sim_data.SC_Range(k:end));
atsea_res.maxPos(i) = max(atsea_data.SC_Range(idx_SC));
sim_res.maxPos(i) = max(sim_data.SC_Range(k:end));

atsea_t = 24*3600*atsea_data.SC_Time;
sim_t = 24*3600*sim_data.SC_Time;
atsea_res.meanVel(i) = mean(diff(atsea_data.SC_Range(idx_SC))./diff(atsea_t(idx_SC)));
sim_res.meanVel(i) = mean(diff(sim_data.SC_Range)./diff(sim_t));
atsea_res.rmsVel(i) = rms(diff(atsea_data.SC_Range(idx_SC))./diff(atsea_t(idx_SC)));
sim_res.rmsVel(i) = rms(diff(sim_data.SC_Range)./diff(sim_t));
atsea_res.minVel(i) = min(diff(atsea_data.SC_Range(idx_SC))./diff(atsea_t(idx_SC)));
sim_res.minVel(i) = min(diff(sim_data.SC_Range)./diff(sim_t));
atsea_res.maxVel(i) = max(diff(atsea_data.SC_Range(idx_SC))./diff(atsea_t(idx_SC)));
sim_res.maxVel(i) = max(diff(sim_data.SC_Range)./diff(sim_t));



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

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

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

atsea_res.meanDoorState(i) = mean(atsea_data.TC_StatusFields.ActualPos(idx_TC));
sim_res.meanDoorState(i) = mean(sim_data.TC_StatusFields.ActualPos);

Au = 0.25*pi*5^2; 
Al = Au-0.25*pi*1.5^2;

atsea_GasForce = Al*atsea_data.SC_LowerPressure(idx_SC)-Au*atsea_data.SC_UpperPressure(idx_SC);
sim_GasForce = Al*sim_data.SC_LowerPressure-Au*sim_data.SC_UpperPressure;
atsea_p = polyfit(atsea_data.SC_Range(idx_SC),atsea_GasForce,1);
sim_p = polyfit(sim_data.SC_Range,sim_GasForce,1);
atsea_res.AirSpringSlope(i) = atsea_p(1);
sim_res.AirSpringSlope(i) = sim_p(1);

end

atsea_res
sim_res

N_Plots = 7;
k = 1;
figure('Position',[217 394 878 1049]);
ah(k) = subplot(N_Plots,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(N_Plots,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(N_Plots,1,k); k = k+1;
plot(sim_res.AirSpringSlope,'x')
hold all
plot(atsea_res.AirSpringSlope,'o')
grid on
xlabel('Sim Number')
ylabel('Air Spring Stiffness (lb/in)')
legend('Sim Results','At Sea Results');

ah(k) = subplot(N_Plots,1,k); k = k+1;
plot(sim_res.rmsVel,'x')
hold all
plot(atsea_res.rmsVel,'o')
grid on
xlabel('Sim Number')
ylabel('RMS Piston Vel (in/s)')
legend('Sim Results','At Sea Results');

ah(k) = subplot(N_Plots,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(N_Plots,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');

ah(k) = subplot(N_Plots,1,k); k = k+1;
plot(atsea_res.meanDoorState,'o')
grid on
xlabel('Sim Number')
ylabel('Door State')

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