
close all
clear all

startDateNum = datenum(2021,8,20,12,0,0);

base_path = getenv('BASE_PATH');
if(isempty(base_path))
  base_path = '../';
end

DecodedFiles_path = [base_path 'DecodedResults/'];
HourlyResults_path = [base_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.BC_Hydrogen = [];
HourlyData.SC_Range = [];
HourlyData.SC_Range_max = [];
HourlyData.SC_Range_min = [];
HourlyData.SC_OffSet = [];
HourlyData.SC_Slope = [];
HourlyData.SC_LoadCell = [];
HourlyData.SC_LoadCell_min = [];
HourlyData.SC_LoadCell_max = [];
HourlyData.BC_VStopCharge = [];
HourlyData.BC_VStopCharge_min = [];
HourlyData.BC_VStopCharge_max = [];
HourlyData.BC_VBalance = [];
HourlyData.BC_VBalance_min = [];
HourlyData.BC_VBalance_max = [];
HourlyData.BC_GroundFault = [];
%HourlyData.BC_GroundFault = [];
HourlyData.PC_Power = [];
HourlyData.PC_Power_min = [];
HourlyData.PC_Power_max = [];
HourlyData.PC_DiffPress =[];
HourlyData.PC_Scale = [];
HourlyData.PC_Retract = [];
HourlyData.Time_DateNum = [];
HourlyData.TC_DoorPosition = [];


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((files(j).datenum > startDateNum) && (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.BC_Hydrogen(i) = MeanResults.BC_Hydrogen;
            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_min(i) = MeanResults.PC_Power_min;
            HourlyData.PC_Power_max(i) = MeanResults.PC_Power_max;
            HourlyData.PC_DiffPress(i) = MeanResults.PC_DiffPress;
            HourlyData.PC_Scale(i) = MeanResults.PC_Scale;
            HourlyData.PC_Retract(i) = MeanResults.PC_Retract;
            HourlyData.SC_OffSet(i) = MeanResults.SC_OffSet;
            HourlyData.SC_Slope(i) = MeanResults.SC_Slope;
	    HourlyData.TC_DoorPosition(i) = MeanResults.TC_DoorPosition;
            HourlyData.SC_LoadCell(i) = MeanResults.SC_LoadCell;
            HourlyData.SC_LoadCell_min(i) = MeanResults.SC_LoadCell_min;
            HourlyData.SC_LoadCell_max(i) = MeanResults.SC_LoadCell_max;
            HourlyData.BC_VBalance(i) = MeanResults.BC_VBalance;
            HourlyData.BC_VBalance_min(i) = MeanResults.BC_VBalance_min;
            HourlyData.BC_VBalance_max(i) = MeanResults.BC_VBalance_max;
            HourlyData.BC_VStopCharge(i) = MeanResults.BC_VStopCharge;
            HourlyData.BC_VStopCharge_min(i) = MeanResults.BC_VStopCharge_min;
            HourlyData.BC_VStopCharge_max(i) = MeanResults.BC_VStopCharge_max;
            
        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.BC_Hydrogen(i) = LastHourlyData.BC_Hydrogen(idx);
            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_min(i) = LastHourlyData.PC_Power_min(idx);
            HourlyData.PC_Power_max(i) = LastHourlyData.PC_Power_max(idx);
            HourlyData.PC_DiffPress(i) = LastHourlyData.PC_DiffPress(idx);
            HourlyData.PC_Scale(i) = LastHourlyData.PC_Scale(idx);
            HourlyData.PC_Retract(i) = LastHourlyData.PC_Retract(idx);
            HourlyData.SC_OffSet(i) = LastHourlyData.SC_OffSet(idx);
            HourlyData.SC_Slope(i) = LastHourlyData.SC_Slope(idx);
	    HourlyData.TC_DoorPosition(i) = LastHourlyData.TC_DoorPosition(idx);
            HourlyData.SC_LoadCell(i) =  LastHourlyData.SC_LoadCell(idx);
            HourlyData.SC_LoadCell_min(i) =  LastHourlyData.SC_LoadCell_min(idx);
            HourlyData.SC_LoadCell_max(i) =  LastHourlyData.SC_LoadCell_max(idx);
            HourlyData.BC_VBalance(i) =  LastHourlyData.BC_VBalance(idx);
            HourlyData.BC_VBalance_min(i) =  LastHourlyData.BC_VBalance_min(idx);
            HourlyData.BC_VBalance_max(i) =  LastHourlyData.BC_VBalance_max(idx);
            HourlyData.BC_VStopCharge(i) =  LastHourlyData.BC_VStopCharge(idx);
            HourlyData.BC_VStopCharge_min(i) =  LastHourlyData.BC_VStopCharge_min(idx);
            HourlyData.BC_VStopCharge_max(i) =  LastHourlyData.BC_VStopCharge_max(idx);
        end
        
    end
    
    
end

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

exit  %End matlab after running (for use from command line)
