close all
clear all

V = (5000/2.2)/1025; 

filename = 'sp_156_a5ee801';

g = 9.81;
ff = 0:.002:.58;
ff = ff';
alpha = 8.1e-3;
beta = .74;
w = 2*pi*ff;
df = ff(2)-ff(1);
 Hs = 1:2;
 clear S_Hs;
 for i = 1:length(Hs)
   U19 = sqrt(Hs(i)*g/0.21);
   w0 = g/U19;
   S_Hs(i,:) = 2*pi*(alpha*g^2)*exp(-beta*(w0./w).^4)./(w.^5);
   S_Hs(i,1) = S_Hs(i,2);
 
 
   Pb_PM(i) = BudalLimit(S_Hs(i,:)',ff,V);
 end

 


cmd = (['wc ' filename]);

[status result] = system(cmd);

if(status > 0) 
  error([cmd ' returns error']);
end

n_lines = str2num(strtok(result));
n_LinesPerRecord = 74;
n_Records = n_lines/n_LinesPerRecord;

fid = fopen(filename);

%tline = cell{n_LinesPerRecord,1};

for i = 1:n_Records
    
  for j = 1:n_LinesPerRecord
    tline{j,1} = fgetl(fid); 
  end
  disp(['Processing ' tline{1}]);
  [serialdate(i) freq{i} bw{i} energy{i} Hs(i) Tp(i)] = ParseDataWellSpectrum(tline);
  
  xi = freq{i}(1):.005:freq{i}(end);
  yi_energy = interp1(freq{i},energy{i},xi);
  
  Pb_meas(i) = BudalLimit(yi_energy,xi,V);
  
end

fclose(fid);

load('../History.mat');


% for i = 1:4:n_Records-4
%   figure('Position',[19 45 1538 1093],'PaperPositionMode','manual','PaperType','usletter','PaperPosition',[0 0 11 8.5],'PaperOrientation','landscape');
%   for j = 0:3
%     if ((i+j) <= n_Records)
%       subplot(2,2,j+1)
%       plot(ff,S_Hs,freq{i+j},energy{i+j});
%       xlabel('freq (Hz)');
%       ylabel('Energy (m^2/Hz)');
%       title(['Wave Conditions: ' datestr(serialdate(i+j))]);
%       legend(['Hs = 1m  (P_b = ' num2str(Pb_PM(1),4) ' W)'],['Hs = 2m  (P_b = ' num2str(Pb_PM(2),4) ' W)'],['Measured (P_b  = ' num2str(Pb_meas(i+j),4) ' W']);   
%       PxxHeave = energy{i+j};
%       f = freq{i+j};
%       sdate = serialdate(i+j);
%       PeakPeriod = Tp(i+j);
%       BudalLimit = Pb_meas(i+j);
%       SigWaveHeight = Hs(i+j);
%       
%       save(['WaveData_'  datestr(serialdate(i+j),'mmmm_dd_yyyy_HH-MM') '.mat'],'f','PxxHeave','sdate','PeakPeriod','BudalLimit','SigWaveHeight');
%     
%     end  
%   end
%   print(['WaveConditions_' datestr(serialdate(i),'mmmm_dd_yyyy_HH-MM')],'-dpdf');
%   close all
% 
% end



first_date_idx = 4;
last_date_idx = length(MeanSerialDate);


%xi = MeanSerialDate(first_date_idx):(MeanSerialDate(last_date_idx)-MeanSerialDate(first_date_idx))/500:MeanSerialDate(last_date_idx);
xi = MeanSerialDate(first_date_idx):1/24:MeanSerialDate(last_date_idx);
yi_max = interp1(serialdate,Pb_meas,xi);
yi_meas = interp1(MeanSerialDate(first_date_idx:last_date_idx),MeanRectifiedPower(first_date_idx:last_date_idx),xi);
yi_Hs = interp1(serialdate,Hs,xi);
yi_Tp = interp1(serialdate,Tp,xi);
yi_Controller = interp1(MeanSerialDate(first_date_idx:last_date_idx),MeanController(first_date_idx:last_date_idx),xi);

save('../../November2012DeploymentSummaryData','xi','yi_max','yi_meas','yi_Hs','yi_Tp','yi_Controller');



figure('Position',[19 45 1538 1093],'PaperPositionMode','manual','PaperType','usletter','PaperPosition',[0 0 11 8.5],'PaperOrientation','landscape');
set(gca,'FontSize',20);
subplot(2,1,1)
plot(xi,yi_Hs);
set(gca,'FontSize',16);
title('Nov 13, 2012 through Nov 30, 2012','FontSize',16);
xlabel('Date','FontSize',16);
set(gca,'XTick',[735185 735190 735195 735200 735205]);
datetick('x','keepticks');
ylabel('Significant Wave Height (m)','FontSize',16);
subplot(2,1,2)
plot(xi,yi_Tp);
set(gca,'FontSize',16);
xlabel('Date','FontSize',16);
set(gca,'XTick',[735185 735190 735195 735200 735205]);
datetick('x','keepticks');
ylabel('Peak Wave Period (s)','FontSize',16');


print('WaveCharacteristics','-dpdf');
print('WaveCharacteristics','-dpng');

yi_Controller = ceil(yi_Controller);


idx_resistive = find(yi_Controller == 1);
idx_scheduled = find(yi_Controller == 2);
idx_latching = find(yi_Controller == 3);

figure('Position',[19 45 1538 1093],'PaperPositionMode','manual','PaperType','usletter','PaperPosition',[0 0 11 8.5],'PaperOrientation','landscape');
set(gca,'FontSize',20);
plot(xi(idx_resistive),yi_Controller(idx_resistive),'x',xi(idx_scheduled),yi_Controller(idx_scheduled),'x',xi(idx_latching),yi_Controller(idx_latching),'x');
title('Nov 13, 2012 through Nov 30, 2012');
%ylabel('Controller (1=resistive, 2 = schedule, 3 = latching');
legend('Resistive Controller','Gain Scheduleing','Latching');
xlabel('Date');
datetick('x','keepticks');
print('ControllerInUse','-dpdf');
print('ControllerInUse','-dpng');


figure('Position',[19 45 1538 1093],'PaperPositionMode','manual','PaperType','usletter','PaperPosition',[0 0 11 8.5],'PaperOrientation','landscape');
set(gca,'FontSize',20);
plot(yi_Hs(idx_resistive),yi_meas(idx_resistive),'x',yi_Hs(idx_scheduled),yi_meas(idx_scheduled),'x',yi_Hs(idx_latching),yi_meas(idx_latching),'x');
legend('Resistive Controller','Gain Scheduleing','Latching');
title('Nov 13, 2012 through Nov 30, 2012');
xlabel('Significant Wave Height (m)');
ylabel('Rectified Power (W)');
print('RectifiedPowerVsWaveHeight_ControlSchemeCompare','-dpdf');



figure('Position',[19 45 1538 1093],'PaperPositionMode','manual','PaperType','usletter','PaperPosition',[0 0 11 8.5],'PaperOrientation','landscape');
set(gca,'FontSize',20);
plot(yi_Hs,yi_meas,'x');
title('Nov 13, 2012 through Nov 30, 2012');
xlabel('Significant Wave Height (m)');
ylabel('Rectified Power (W)');
print('RectifiedPowerVsWaveHeight','-dpdf');





figure('Position',[19 45 1538 1093],'PaperPositionMode','manual','PaperType','usletter','PaperPosition',[0 0 11 8.5],'PaperOrientation','landscape');
set(gca,'FontSize',20);
plot(yi_Tp,yi_meas,'x');
title('Nov 13, 2012 through Nov 30, 2012');
xlabel('Peak Period (Tp)');
ylabel('Rectified Power (W)');
print('RectifiedPowerVsPeakPeriod','-dpdf');



figure('Position',[19 45 1538 1093],'PaperPositionMode','manual','PaperType','usletter','PaperPosition',[0 0 11 8.5],'PaperOrientation','landscape');

[AX,H1,H2] = plotyy(xi,yi_meas,xi,yi_Hs);
set(AX(1),'FontSize',20);
set(AX(2),'FontSize',20);
title('Electrical Power and Sig. Wave Height for Nov 13, 2012 through Nov 30, 2012');
legend('Rectified Power','Sig. Wave Height');

xlabel('Date');
datetick('x','keepticks');


set(AX(1),'YLim',[0 1000]);
set(AX(1),'YTick',0:200:800);

set(AX(2),'YLim',[0 4]);
set(AX(2),'YTick',0:.2:4);

set(get(AX(1),'Ylabel'),'String','Rectified Power (W)'); 
set(get(AX(2),'Ylabel'),'String','Sig. Wave Height (m)');
set(get(AX(1),'Ylabel'),'FontSize',20);
set(get(AX(2),'Ylabel'),'FontSize',20);
set(AX(2),'XTick',[]);

print('ElectricalPowerVsWaveHeight','-dpdf');


figure('Position',[19 45 1538 1093],'PaperPositionMode','manual','PaperType','usletter','PaperPosition',[0 0 11 8.5],'PaperOrientation','landscape');
set(gca,'FontSize',20);
plot(xi,yi_meas,xi,yi_max);
title('Electrical Power for Nov 13, 2012 through Nov 30, 2012');
legend('Rectified Power','Max Absorbable Power');
xlabel('Date');
ylabel('Watts');
datetick('x','keepticks');
print('ElectricalPowerComparison','-dpdf');


figure('Position',[19 45 1538 1093],'PaperPositionMode','manual','PaperType','usletter','PaperPosition',[0 0 11 8.5],'PaperOrientation','landscape');
[AX,H1,H2] = plotyy(xi,100*yi_meas./yi_max,xi,yi_Hs);
set(H1,'LineWidth',2)
set(AX(1),'FontSize',20);
set(AX(2),'FontSize',20);
title('Performance for Nov 13, 2012 through Nov 30, 2012');

xlabel('Date');
datetick('x','keepticks');

set(AX(1),'YLim',[0 20]);
set(AX(1),'YTick',0:2:20);

set(AX(2),'YLim',[0 4]);
set(AX(2),'YTick',0:.2:4);

set(get(AX(1),'Ylabel'),'String','Percentage of Theoretical Maximum'); 
set(get(AX(2),'Ylabel'),'String','Sig. Wave Height (m)');
set(get(AX(1),'Ylabel'),'FontSize',20);
set(get(AX(2),'Ylabel'),'FontSize',20);
set(AX(2),'XTick',[]);

print('PercentageOfTheoreticalMax','-dpdf');



figure('Position',[19 45 1538 1093],'PaperPositionMode','manual','PaperType','usletter','PaperPosition',[0 0 11 8.5],'PaperOrientation','landscape');
set(gca,'FontSize',20);
plot(yi_Hs,100*yi_meas./yi_max,'x');
title('June 26, 2012 through July 10, 2012');
xlabel('Significant Wave Height (m)');
ylabel('Percentage of Theoretical Maximum');
print('PercentageOfMaxVsWaveHeight','-dpdf');


figure('Position',[19 45 1538 1093],'PaperPositionMode','manual','PaperType','usletter','PaperPosition',[0 0 11 8.5],'PaperOrientation','landscape');
set(gca,'FontSize',20);
plot(yi_Tp,100*yi_meas./yi_max,'x');
title('Nov 13, 2012 through Nov 30, 2012');
xlabel('Peak Period (s)');
ylabel('Percentage of Theoretical Maximum');
print('PercentageOfMaxVsPeakPeriod','-dpdf');


figure('Position',[19 45 1538 1093],'PaperPositionMode','manual','PaperType','usletter','PaperPosition',[0 0 11 8.5],'PaperOrientation','landscape');
set(gca,'FontSize',20);
scatter3(yi_Hs(10:920),yi_Tp(10:920),100*yi_meas(10:920)./yi_max(10:920));
title('Nov 13, 2012 through Nov 30, 2012');
xlabel('Significant Wave Height (m)');
ylabel('Peak Period (s)');
zlabel('Percentage of Theoretical Maximum');

print('PercentageOfMaxVsPeakPeriodAndWaveHeight','-dpdf');




