
close all
clear all

DecodedFiles_path = '../DecodedResults/';
HourlyResults_path = '../HourlyResults/';

if(~exist(DecodedFiles_path,'dir')) mkdir(DecodedFiles_path);  end
if(~exist(HourlyResults_path,'dir')) mkdir(HourlyResults_path);  end

HourlyAveragesFilename = 'HourlyAverages.mat';

HourlyData.baseFilename = {};
HourlyData.baseFileDateNum = [];
HourlyData.BC_Voltage = [];
HourlyData.SC_Range = [];
HourlyData.SC_Range_max = [];
HourlyData.SC_Range_min = [];
HourlyData.BC_GroundFault = [];
HourlyData.PC_Power = [];
HourlyData.PC_Power_max = [];
HourlyData.PC_DiffPress =[];
HourlyData.Time_DateNum = [];



if(~isempty(dir([HourlyResults_path HourlyAveragesFilename])));
    LastHourlyData = load([HourlyResults_path HourlyAveragesFilename]);
    LastHourlyData = LastHourlyData.HourlyData;
else
    LastHourlyData = HourlyData;
end

files = dir([DecodedFiles_path '*.mat']);
i = 0;
for j = 1:length(files)
    if(isempty(strfind(files(j).name,'raw')))
        i = i+1;
        base_filename = files(j).name(1:end-4);
        
        idx = find(strcmp(LastHourlyData.baseFilename,base_filename));
        
        if(isempty(idx) || (files(j).datenum > LastHourlyData.baseFileDateNum(idx)) || i > length(LastHourlyData.baseFileDateNum))  %Need to read in data and compute means, etc.
            disp(['Re-reading data file and recomputing means for ' base_filename]);
            HourlyData.baseFilename{i} = base_filename;
            HourlyData.baseFileDateNum(i) = files(j).datenum;
            MeanResults = ComputeMeans(DecodedFiles_path,base_filename);
            HourlyData.Time_DateNum(i) = MeanResults.Time_DateNum;
            HourlyData.BC_Voltage(i) = MeanResults.BC_Voltage;
            HourlyData.BC_GroundFault(i) = MeanResults.BC_GroundFault;
            HourlyData.SC_Range(i) = MeanResults.SC_Range;
            HourlyData.SC_Range_max(i) = MeanResults.SC_Range_max;
            HourlyData.SC_Range_min(i) = MeanResults.SC_Range_min;
            HourlyData.PC_Power(i) = MeanResults.PC_Power;
            HourlyData.PC_Power_max(i) = MeanResults.PC_Power_max;
            HourlyData.PC_DiffPress(i) = MeanResults.PC_DiffPress;
            
        else  %Nothing's changed for this file so can use previously computed data
            %disp(['Re-use last data for ' base_filename]);
            HourlyData.baseFilename{i} = LastHourlyData.baseFilename{idx};
            HourlyData.baseFileDateNum(i) =  LastHourlyData.baseFileDateNum(idx);
            HourlyData.Time_DateNum(i) = LastHourlyData.Time_DateNum(idx);
            HourlyData.BC_Voltage(i) = LastHourlyData.BC_Voltage(idx);
            HourlyData.BC_GroundFault(i) = LastHourlyData.BC_GroundFault(i);
            HourlyData.SC_Range(i) = LastHourlyData.SC_Range(idx);
            HourlyData.SC_Range_max(i) = LastHourlyData.SC_Range_max(idx);
            HourlyData.SC_Range_min(i) = LastHourlyData.SC_Range_min(idx);
            HourlyData.PC_Power(i) = LastHourlyData.PC_Power(idx);
            HourlyData.PC_Power_max(i) = LastHourlyData.PC_Power_max(idx);
            HourlyData.PC_DiffPress(i) = LastHourlyData.PC_DiffPress(idx);
            
        end
        
    end
    
    
end

if(isOctave)
    save('-mat7-binary',[HourlyResults_path HourlyAveragesFilename],'HourlyData');
else
    save([HourlyResults_path HourlyAveragesFilename],'HourlyData');
end


figure('Visible','off');
plot(HourlyData.Time_DateNum,HourlyData.BC_Voltage);
ylabel('Battery Voltage (V)');
datetick('x','mm/dd HH:MM');
print([HourlyResults_path 'BatteryVoltage.pdf'],'-dpdf');


figure('Visible','off');
plot(HourlyData.Time_DateNum,HourlyData.PC_Power);
hold all
plot(HourlyData.Time_DateNum,HourlyData.PC_Power_max);
legend('Hourly Mean','Hourly Max');
ylabel('Power Converter Power (W)');
datetick('x','mm/dd HH:MM');
print([HourlyResults_path 'Power.pdf'],'-dpdf');

figure('Visible','off');
plot(HourlyData.Time_DateNum,HourlyData.PC_DiffPress);
ylabel('Compensator Pressure (psi)');
datetick('x','mm/dd HH:MM');
print([HourlyResults_path 'Comp_Pressure.pdf'],'-dpdf');


figure('Visible','off');
plot(HourlyData.Time_DateNum,HourlyData.BC_GroundFault);
ylabel('Ground Fault Signal (mA)');
datetick('x','mm/dd HH:MM');
print([HourlyResults_path 'GroundFault.pdf'],'-dpdf');

% figure('Visible','off');
% plot(HourlyData.Time_DateNum,HourlyData.BC_Spare);
% ylabel('Hydrogen Level (%LEL)');
% datetick('x','mm/dd HH:MM');
% print([HourlyResults_path 'HydrogenLEL.pdf'],'-dpdf');

figure('Visible','off');
plot(HourlyData.SC_Range);
hold all
plot(HourlyData.Time_DateNum,HourlyData.SC_Range_max);
plot(HourlyData.Time_DateNum,HourlyData.SC_Range_min);
legend('Mean','Max','Min');
ylabel('Piston Position (in)');
datetick('x','mm/dd HH:MM');
print([HourlyResults_path 'PistonPosition.pdf'],'-dpdf');


