clear all
close all

if(exist('History.mat'))
    load('History.mat')
else
    J = 0;
end
j = 0;


dpath = '134.89.12.189/data/';

dataDirs = dir([dpath 'R*']);
  
  
for k = 1:length(dataDirs)
  if(dataDirs(k).isdir == 1)
     ppath = [dpath dataDirs(k).name '/'];
      
     matfiles = dir([ppath 'D*.mat']);

     K = length(matfiles);
 
     for k = 1:K
         j = j+1;
         if(j > J-2) 
            disp(['  Loading ' ppath matfiles(k).name]);
            load([ppath matfiles(k).name]);
            MeanRectifiedPower(j) = mean(RectifiedVoltage.*RectifiedCurrent);
            MeanConvertedCurrent(j) = mean(ConvertedCurrent);
            MeanConvertedPower(j) = mean(ConvertedCurrent.*RectifiedVoltage);
            MeanBatteryCurrent(j) = mean(BatteryCurrent);
            MaxBatteryCurrent(j) = max(BatteryCurrent);
            MeanBatteryVoltage(j) = mean(BatteryVoltage);
            MeanBatteryPower(j) = mean(BatteryCurrent.*BatteryVoltage);
            MaxBatteryPower(j) = max(BatteryCurrent.*BatteryVoltage);
            
            FirstSerialDate(j) = serialdate(1);
            LastSerialDate(j) = serialdate(end);
            MeanSerialDate(j) = mean(serialdate);
            
            MeanLoadCellForce(j) = mean(LoadCellForce);
            MaxLoadCellForce(j) = max(LoadCellForce);
            MinLoadCellForce(j) = min(LoadCellForce);
            StdDevLoadCellForce(j) = std(LoadCellForce);
            
            MeanPistonDistance(j) = mean(PistonDistance);
            MaxPistonDistance(j) = max(PistonDistance);
            MinPistonDistance(j) = min(PistonDistance);
            StdDevPistonDistance(j) = std(PistonDistance);
            
            
            
             
            %Plot details 
            plot_title = [datestr(serialdate(1)) '   -->   ' datestr(serialdate(end))];
            
            figure('Position',[19 45 1538 1093],'PaperPositionMode','manual','PaperType','usletter','PaperPosition',[0 0 8.5 11]);
            
            ah1 = subplot(6,1,1);
            plot(time-EpochSecs(1),RectifiedVoltage);
            xlabel('time (seconds)');
            ylabel('Rectified Voltage (V)');
            
            title([plot_title '     Rectified Power = ' num2str(mean(RectifiedVoltage.*RectifiedCurrent)) ' W']);
            
            ah2 = subplot(6,1,2);
            plot(time-EpochSecs(1),RectifiedCurrent);
            xlabel('time (seconds)');
            ylabel('Rectified Current (A)');
            
            
            ah3 = subplot(6,1,3);
            plot(time-EpochSecs(1),ConvertedCurrent);
            xlabel('time (seconds)');
            ylabel('Converted Current (A)');
            
            ah4 = subplot(6,1,4);
            plot(time-EpochSecs(1),BatteryVoltage);
            xlabel('time (seconds)');
            ylabel('Battery Voltage (V)');
            
            ah5 = subplot(6,1,5);
            plot(time-EpochSecs(1),BatteryCurrent);
            xlabel('time (seconds)');
            ylabel('Battery Current (A)');
            
            ah6 = subplot(6,1,6);
            plot(time-EpochSecs(1),BatteryCurrent.*BatteryVoltage);
            legend(['Mean = ' num2str(mean(BatteryCurrent.*BatteryVoltage)) ' W']);
            xlabel('time (seconds)');
            ylabel('Battery Power (W)');
            
            
            linkaxes([ah1 ah2 ah3 ah4 ah5 ah6],'x');
            
            
            print('-dpdf',['DataPlots/Detail_' num2str(j)]);
            cmd = ['!cp DataPlots/Detail_' num2str(j) '.pdf .'];
            eval(cmd);
            close all
         
         end
     end
     
  end
  
end


J = j;

figure
plot(1:J,MeanRectifiedPower,1:J,MeanConvertedPower);
legend('MeanRectifiedPower','MeanConvertedPower');
ylabel('Watts');
print('-dpdf','DataPlots/MeanRectifiedPowerPlot');

figure
plot(J-24:J,MeanRectifiedPower(J-24:J),J-24:J,MeanConvertedPower(J-24:J));
legend('MeanRectifiedPower','MeanConvertedPower');
ylabel('Watts');
print('-dpdf','DataPlots/MeanRectifiedPowerPlot_Last24Hrs');

figure
plot(1:J,MeanBatteryVoltage);
ylabel('Volts');
print('-dpdf','DataPlots/MeanBatteryVoltagePlot');

figure
plot(J-24:J,MeanBatteryVoltage(J-24:J));
ylabel('Volts');
print('-dpdf','DataPlots/MeanBatteryVoltagePlot_Last24Hrs');

figure
plot(1:J,MeanBatteryPower);
ylabel('Watts');
print('-dpdf','DataPlots/MeanBatteryPowerPlot');

figure
plot(J-24:J,MeanBatteryPower(J-24:J));
ylabel('Watts');
print('-dpdf','DataPlots/MeanBatteryPowerPlot_Last24Hrs');
close all

save('History.mat',...
     'J',...
     'MeanRectifiedPower',...
     'MeanConvertedCurrent',...
     'MeanConvertedPower',...
     'MeanBatteryCurrent',...
     'MaxBatteryCurrent',...
     'MeanBatteryVoltage',...
     'MeanBatteryPower',...
     'MaxBatteryPower',...
     'FirstSerialDate',...
     'LastSerialDate',...
     'MeanSerialDate',...    
     'MeanLoadCellForce',...
     'MaxLoadCellForce',...
     'MinLoadCellForce',...
     'StdDevLoadCellForce',...       
     'MeanPistonDistance',...
     'MaxPistonDistance',...
     'MinPistonDistance',...
     'StdDevPistonDistance');




cmd = '!cp DataPlots/Mean*.pdf .';
eval(cmd);





