%
% PURPOSE: Plot LR power consumption from logged data.
%
% .time is in units of fractional days.
%
% Setup figure positions, etc.
  review;
%  

if exist( 'powerFigPtr' ) ~= 1
  powerFigPtr= figure;
  set( powerFigPtr, 'Name', 'LR Power', 'DeleteFcn', 'clear powerFigPtr' );
  set(gcf,'position',position)
  if(exist('fontsize')) 
    powerFontsize=fontsize;
  else
    powerFontsize=22;
  end
  powerFontweight='normal';
  set(gca,'fontsize',powerFontsize,'fontweigh',powerFontweight)
  set(gcf,'defaultaxesfontsize',powerFontsize,'defaulttextfontsize', ...
      powerFontsize)
else
  figure(powerFigPtr);
end

  time0=ThrusterServo.component_avgCurrent.time(1);
  thrPower = ThrusterServo.component_avgCurrent.value(1:end-3).*...
          ThrusterServo.component_avgVoltage.value/1e3;

%
  plot((ThrusterServo.component_avgCurrent.time(1:end-3)), thrPower,'.');
  hold on
%

  plot(platform_average_power.time, platform_average_power.value,'r.');
  
  
  fs = 1/2.4;
  [bbThr aaThr]= butter(4, .001/(fs/2));
  thrPower = double(thrPower);
  indx = find(isfinite(thrPower));
  ttime = ThrusterServo.component_avgCurrent.time(1:end-3);
  filtThrPower = filtfilt(bbThr, aaThr, thrPower(indx));
  plot(ttime(indx), filtThrPower,'k','linewidth',3);
  grid on;
  dynamicDateTicks;
  hold on;
  ylabel('Watts');
  xlabel('Hours');
  title('LR Auv Power Consumption');
  
  halfFs = (1/60)/2;
  [bbVeh aaVeh]= butter(4,.001/halfFs);
  
  ppower = double(platform_average_power.value);

  pindx = find(isfinite(ppower));

  ptime = platform_average_power.time;
  filtppower = filtfilt(bbVeh ,aaVeh ,ppower(pindx));
  plot(ptime(pindx), filtppower,'r','linewidth',3);
  legend('Thruster Power','Total Power','Filtered Thruster', 'Filtered Total' );
  
  
  
if exist( 'interpPowerFigPtr' ) ~= 1
  interpPowerFigPtr= figure;
  set( interpPowerFigPtr, 'Name', 'Interpolated LR Power', 'DeleteFcn', 'clear interpPowerFigPtr' );
  set(gcf,'position',position)
  if(exist('fontsize')) 
    powerFontsize=fontsize;
  else
    powerFontsize=22;
  end
  powerFontweight='normal';
  set(gca,'fontsize',powerFontsize,'fontweigh',powerFontweight)
  set(gcf,'defaultaxesfontsize',powerFontsize,'defaulttextfontsize', ...
      powerFontsize)
else
  figure(interpPowerFigPtr);
end

  %indx = find(isfinite(thrPower));

  
  thrPowerPtime = interp1( ...
      ThrusterServo.component_avgCurrent.time(indx:end-3), thrPower(indx), platform_average_power.time);
  
  depthPtime = interp1( depth.time, depth.value, platform_average_power.time);
  
  time = platform_average_power.time;
  
  thrPowerPtime = double(thrPowerPtime);
  
  indxPtime = find(isfinite(thrPowerPtime) & isfinite(platform_average_power.value) & depthPtime > 4);

  %hotel = platform_average_power.value(indxPtime) - thrPowerPtime(indxPtime);
  hotel = platform_average_power.value - thrPowerPtime;

  plot(time(indxPtime), thrPowerPtime(indxPtime),'b.');
  hold on
  plot(time(indxPtime), platform_average_power.value(indxPtime),'r.');
  plot(time(indxPtime), hotel(indxPtime),'g.');
  
  grid on;
  dynamicDateTicks;
  ylabel('Watts');
  xlabel('Hours');
  title('Interpolated LR Auv Power Consumption');

  
  halfFsPtime = (1/60)/2;
  
  [bbPtime aaPtime]= butter(4,.001/halfFsPtime);
  
  hotel = double(hotel);
  platform_average_power.value = double(platform_average_power.value);
  
  filtThrPowerPtime = filtfilt(bbPtime ,aaPtime ,thrPowerPtime(indxPtime));
  filtHotelPtime    = filtfilt(bbPtime ,aaPtime ,hotel(indxPtime));
  filtPPower        = filtfilt(bbPtime ,aaPtime ,platform_average_power.value(indxPtime));

  
  plot(time(indxPtime), filtThrPowerPtime,'k')
  plot(time(indxPtime), filtPPower,'r')
  hotelHandle = plot(time(indxPtime), filtHotelPtime, 'g','linewidth',3);
  str = sprintf('Mean Hotel = %.2f Watts.',mean(hotel(indxPtime)));
  text(1.3+depth.time(indxThr(1)), 5, str);
  grid on;
  dynamicDateTicks;
  ylabel('Watts');
  xlabel('Hours');
  title('Interpolated LR Auv Power Consumption');
  legend('Thruster Power','Total Power','Filtered Thruster', 'Filtered Total','Hotel' );
  
  

if exist( 'TsFigPtr' ) ~= 1
  TsFigPtr= figure;
  set( TsFigPtr, 'Name', 'LR Ts', 'DeleteFcn', 'clear TsFigPtr' );
  set(gcf,'position',position)
  if(exist('fontsize')) 
    TsFontsize=fontsize;
  else
    TsFontsize=22;
  end
  TsFontweight='normal';
  set(gca,'fontsize',TsFontsize,'fontweigh',TsFontweight)
  set(gcf,'defaultaxesfontsize',TsFontsize,'defaulttextfontsize', ...
      TsFontsize)
else
  figure(TsFigPtr);
end

  subplot(2,1,1)
  plot(ThrusterServo.component_avgCurrent.time(2:end), ...
      diff(ThrusterServo.component_avgCurrent.time)*86400,'.');
  grid on;
  dynamicDateTicks;
  hold on;
  plot(depth.time(2:end),  diff(depth.time)*86400,'r.')
  ylabel('Thruster, Depth, Seconds');
  xlabel('Hours');
  title('Sampling Rates');
  set(gca,'ylim',[.1 3]);
  legend('Thruster','Depth')
%  
  subplot(2,1,2)
  plot(platform_average_power.time(2:end), diff(platform_average_power.time)*86400,'.');
%  
  grid on;
  dynamicDateTicks;
  hold on;
  ylabel('Pltfm Average Power, Seconds');
  xlabel('Hours');
  
  figure(interpPowerFigPtr);
