close all
clear all


hourly_data_file = '../HourlyResults/HourlyAverages.mat'

decoded_data_path = '../DecodedResults/';


load(hourly_data_file);

idx = 307:length(HourlyData.baseFileDateNum);
jdx = find(HourlyData.baseFileDateNum > datenum(2025,6,3,0,0,0));

j = 0;
for i = idx
  j = j+1;
  data = load([decoded_data_path HourlyData.baseFilename{i} '.mat']);
  DateNum{j} = data.Time_DateNum;
  RPM{j} = data.PC_RPM;
  Range{j} = data.SC_Range;
  PistonSpeed{j} = [diff(data.SC_Range)./diff(DateNum{j}*24*3600) NaN];
  disp(['Loaded ' num2str(j) ' of ' num2str(length(idx))]);
  length(DateNum{j})
  length(RPM{j})
  length(Range{j})
  length(PistonSpeed{j})
end




fh1 = figure
ah1 = subplot(2,1,1)
hold all
for j = 1:length(DateNum)
  plot(DateNum{j},PistonSpeed{j},'b-');
end
xlabel("Time");
ylabel("PistonSpeed");
title([HourlyData.baseFilename{1} ' - ' HourlyData.baseFilename{end}]);
grid on
datetick('x');

ah2 = subplot(2,1,2)
hold all
for j = 1:length(DateNum)
  plot(DateNum{j},RPM{j},'b-');
end
xlabel("Time");
ylabel("PistonSpeed");
title([HourlyData.baseFilename{1} ' - ' HourlyData.baseFilename{end}]);
grid on
datetick('x');



figure
hold all
for j = 1:length(DateNum)-length(jdx)
  plot(PistonSpeed{j},RPM{j},'b.');
end

for j = length(DateNum)-length(jdx):length(DateNum)
  plot(PistonSpeed{j},RPM{j},'k.');
end
title([HourlyData.baseFilename{1} ' - ' HourlyData.baseFilename{end}]);


grid on
xlabel('Piston Speed (ips)');
ylabel('RPM');


for j = 1:length(DateNum)
    mean_DateNum(j) = mean(DateNum{j});
    kdx = find(~isnan(PistonSpeed{j}));
    stdDevPistonSpeed(j) = std(PistonSpeed{j}(kdx));
    kdx = find(~isnan(RPM{j}));
    stdDevRPM(j) = std(RPM{j}(kdx));
end

figure
ah1 = subplot(2,1,1)
plot(mean_DateNum,stdDevPistonSpeed);
datetick('x');
ylabel('Std Dev Piston Speed');
grid on;


ah2 = subplot(2,1,2)

plot(mean_DateNum,stdDevRPM);
grid on;
datetick('x');
ylabel('Std Dev RPM');



figure
plot(stdDevPistonSpeed(1:length(DateNum)-length(jdx)),stdDevRPM(1:length(DateNum)-length(jdx)),'b.')
hold all
plot(stdDevPistonSpeed(length(DateNum)-length(jdx):length(DateNum)),stdDevRPM(length(DateNum)-length(jdx):length(DateNum)),'k.')
xlabel('Std Dev Piston Speed');
ylabel('Std Dev RPM');
title([HourlyData.baseFilename{1} ' - ' HourlyData.baseFilename{end}]);
grid on;

