function MakePlots

if(exist('History.mat'))
    load('History.mat')
else
    J = 0;
end
j = 0;


%dpath = '134.89.12.189/data/';
%dataDirs = dir([dpath 'R*']);

dpath = 'DecodedResults/'
dataDirs = dir([dpath 'R*']);
%dataDirs = dir([dpath]);
 



for kk = 1:length(dataDirs)
  if(dataDirs(kk).isdir == 1)
     ppath = [dpath dataDirs(kk).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);
            
            MeanLatitude(j) = mean(latitudeGPS);
            MeanLongitude(j) = mean(longitudeGPS);
            
            
            
             
            %Plot details 
            plot_title = [datestr(serialdate(1)) '   -->   ' datestr(serialdate(end))];
            
            figure('Visible','off','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 QuickViewPlots/.'];
 %           eval(cmd);
 
 
            figure('Visible','off','Position',[19 45 1538 1093],'PaperPositionMode','manual','PaperType','usletter','PaperPosition',[0 0 8.5 11]);
            
            ah1 = subplot(2,1,1);
            plot(time-EpochSecs(1),ComputedStdv,time-EpochSecs(1),TargetStdv);
            xlabel('time (seconds)');
            ylabel('Piston Std Dev');
            legend('Computed','Target')
            ah2 = subplot(2,1,2);
            plot(time-EpochSecs(1),ScaleFactor);
            xlabel('time (seconds)');
            ylabel('ScaleFactor');
            
            title([plot_title '   Last Std = ' num2str(ComputedStdv(end))]);
 
            print('-dpdf',['DataPlots/Stdv_' num2str(j)]);
 
 
            close all
         
         end
     end
     
  end
  
end
J = j;

%Output text for website.

LatInitial = 36.744;
LonInitial = -121.8808;

LatLast = mean(latitudeGPS(end));
LonLast = mean(longitudeGPS(end));

DistanceLat = (LatLast-LatInitial)*60*1852;  %In meters
DistanceLon = (LonLast-LonInitial)*60*1852*cos(LatInitial*pi/180);  %In meters
Distance = sqrt(DistanceLat.^2+DistanceLon.^2);

if(Distance > 250)
  message = ['The buoy is ' num2str(Distance,3) ' meters from the anchor site.  Have a nice boat ride.'];
  setpref('Internet','SMTP_Server','mbarimail.mbari.org');
  setpref('Internet','E_mail','hamilton@mbari.org');
  %sendmail('hamilton@mbari.org', 'Anchor Alert from Matlab', message);
  %sendmail('francois@mbari.org', 'Anchor Alert from Matlab', message);
end

height = 0.1;
row = 0;
figure('Visible','off');
axes('Visible','off');
text(0,1-row*height,'PowerBuoy Deployment Data','FontSize',14); row = row+1.1;
text(0,1-row*height,['   Most Recent Data = ' datestr(serialdate(end)) '   (' num2str((now+8/24-serialdate(end))*24*60,3) ' minutes ago)'],'FontSize',14); row = row+1;
text(0,1-row*height,['   Most Recent Location = ' num2str(LatLast,5) 'N   ' num2str(abs(LonLast),5) 'W    (' num2str(Distance,3) ' meters from anchor)'],'FontSize',14); row = row+1;
print('-dpng','WebSitePlots/StatusTxt')
%savefig('WebSitePlots/StatusTxt', gcf, 'png');

%Plot Most Recent Data For Website
plot_title = [datestr(serialdate(1)) '   -->   ' datestr(serialdate(end))];
figure('Visible','on','Position',[19 45 1538 1093]);
            
ah1 = subplot(2,1,1);
plot(time-EpochSecs(1),RectifiedVoltage.*RectifiedCurrent);
set(gca,'FontSize',18);
xlabel('time (seconds)','FontSize',18);
ylabel('Power (W)','FontSize',18);
title([plot_title '     Mean Rectified Power = ' num2str(mean(RectifiedVoltage.*RectifiedCurrent),4) ' W'],'FontSize',18);
            
ah2 = subplot(2,1,2);
plot(time-EpochSecs(1),PistonDistance);
set(gca,'FontSize',18);
xlabel('time (seconds)','FontSize',18);
ylabel('Piston Location (inches)','FontSize',18);
savefig('WebSitePlots/MostRecent', gcf, 'jpeg');



Jmax = min(25,J)-1;


fh = figure('Visible','off','Position',[177 138 1033 577],'PaperOrientation','portrait','PaperPosition',[0 0 8.5 11]);
subplot(4,1,1)
plot(1:J,MeanPistonDistance);
ylabel('PistonDistance Mean');
subplot(4,1,2)
plot(1:J,MaxPistonDistance);
ylabel('PistonDistance Maximum');
subplot(4,1,3)
plot(1:J,MinPistonDistance);
ylabel('PistonDistance Minimum');
subplot(4,1,4)
plot(1:J,StdDevPistonDistance);
ylabel('PistonDistance Standard Deviation');
print('-dpdf','DataPlots/PistonDistancePlot');


fh = figure('Visible','off','Position',[177 138 1033 577],'PaperOrientation','portrait','PaperPosition',[0 0 8.5 11]);
subplot(4,1,1)
plot(J-Jmax:J,MeanPistonDistance(J-Jmax:J));
ylabel('PistonDistance Mean');
subplot(4,1,2)
plot(J-Jmax:J,MaxPistonDistance(J-Jmax:J));
ylabel('PistonDistance Maximum');
subplot(4,1,3)
plot(J-Jmax:J,MinPistonDistance(J-Jmax:J));
ylabel('PistonDistance Minimum');
subplot(4,1,4)
plot(J-Jmax:J,StdDevPistonDistance(J-Jmax:J));
ylabel('PistonDistance Standard Deviation');
print('-dpdf','DataPlots/PistonDistancePlot_Last24Hours');

figure('Visible','off')
plot(1:J,MeanRectifiedPower,1:J,MeanConvertedPower);
legend('Rectified Power (Hourly Mean)','Converted Power (Hourly Mean)');
ylabel('Watts');
xlabel('Hours into Deployment');
title('Hourly Averages of Captured Electrical Power - Entire Deployment');
print('-dpdf','DataPlots/MeanRectifiedPowerPlot');
savefig('WebSitePlots/MeanRectifiedPowerPlot', gcf, 'png');


figure('Visible','off')
plot(J-Jmax:J,MeanRectifiedPower(J-Jmax:J),J-Jmax:J,MeanConvertedPower(J-Jmax:J));
legend('Rectified Power (Hourly Mean)','Converted Power (Hourly Mean)');
ylabel('Watts');
xlabel('Hours into Deployment');
title('Hourly Averages of Captured Electrical Power - Last 24 Hours');
print('-dpdf','DataPlots/MeanRectifiedPowerPlot_Last24Hours');
savefig('WebSitePlots/MeanRectifiedPowerPlot_Last24Hrs', gcf, 'png');


figure('Visible','off')
plot(1:J,MeanBatteryVoltage);
ylabel('Volts');
print('-dpdf','DataPlots/MeanBatteryVoltagePlot');

figure('Visible','off')
plot(J-Jmax:J,MeanBatteryVoltage(J-Jmax:J));
ylabel('Volts');
print('-dpdf','DataPlots/MeanBatteryVoltagePlot_Last24Hrs');

figure('Visible','off')
plot(1:J,MeanBatteryPower);
ylabel('Watts');
print('-dpdf','DataPlots/MeanBatteryPowerPlot');

figure('Visible','off')
plot(J-Jmax:J,MeanBatteryPower(J-Jmax:J));
ylabel('Watts');
print('-dpdf','DataPlots/MeanBatteryPowerPlot_Last24Hrs');


figure('Visible','off')
plot(plot(abs(MeanLo),MeanLatitude));
ylabel('Latitude (degrees)');
xlabel('Longitude (degrees)');
print('-dpdf','DataPlots/MeanBuoyPosition');


cmd = ['!cp DataPlots/*.pdf QuickViewPlots/.'];
eval(cmd);

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');







