clear all
results=importdata('IMC_align_calibrate_data.txt');
data=results.data;
data=data';

% Load finesse bit
load('Finesse_model_results');

tolerance=1e-11;
showplot=0;
A_guess=-3e-4;
xoff_guess=0;
yoff_guess=1;

MC1Poff=data(:,1);
MC1PIM4T=(data(:,2)/max(data(:,2)));
MC1PMC2T=(data(:,3)/max(data(:,3)));
MC1PMCrefl=-((1-data(:,4))/max(1-data(:,4))-1);

[AfitMC1PIM4T,hfitMC1PIM4T,yoff,fit] = fit_parabola_vertex(MC1Poff,MC1PIM4T,A_guess,xoff_guess,yoff_guess,tolerance,showplot);
[AfitMC1PMC2T,hfitMC1MC2T,yoff,fit] = fit_parabola_vertex(MC1Poff,MC1PMC2T,A_guess,xoff_guess,yoff_guess,tolerance,showplot);
[AfitMC1Pmodel,hfitmodel,yoff,fit] = fit_parabola_vertex(MC1Pmodeloff,MC1Ptrans,A_guess,xoff_guess,yoff_guess,tolerance,showplot);

MC1Yoff=data(:,5);
MC1YIM4T=(data(:,6)/max(data(:,6)));
MC1YMC2T=(data(:,7)/max(data(:,7)));
MC1YMCrefl=-((1-data(:,8))/max(1-data(:,8))-1);

[AfitMC1YIM4T,hfitMC1YIM4T,yoff,fit] = fit_parabola_vertex(MC1Yoff,MC1YIM4T,A_guess,xoff_guess,yoff_guess,tolerance,showplot);
[AfitMC1YMC2T,hfitMC1YMC2T,yoff,fit] = fit_parabola_vertex(MC1Yoff,MC1YMC2T,A_guess,xoff_guess,yoff_guess,tolerance,showplot);
[AfitMC1Ymodel,hfitmodel,yoff,fit] = fit_parabola_vertex(MC1Ymodeloff,MC1Ytrans,A_guess,xoff_guess,yoff_guess,tolerance,showplot);

MC2Poff=data(:,9);
MC2PIM4T=(data(:,10)/max(data(:,10)));
MC2PMC2T=(data(:,11)/max(data(:,11)));
MC2PMCrefl=-((1-data(:,12))/max(1-data(:,12))-1);

[AfitMC2PIM4T,hfitMC2PIM4T,yoff,fit] = fit_parabola_vertex(MC2Poff,MC2PIM4T,A_guess,xoff_guess,yoff_guess,tolerance,showplot);
[AfitMC2PMC2T,hfitMC2PMC2T,yoff,fit] = fit_parabola_vertex(MC2Poff,MC2PMC2T,A_guess,xoff_guess,yoff_guess,tolerance,showplot);
[AfitMC2Pmodel,hfitmodel,yoff,fit] = fit_parabola_vertex(MC2Pmodeloff,MC2Ptrans,A_guess,xoff_guess,yoff_guess,tolerance,showplot);

MC2Yoff=data(:,13);
MC2YIM4T=(data(:,14)/max(data(:,14)));
MC2YMC2T=(data(:,15)/max(data(:,15)));
MC2YMCrefl=-((1-data(:,16))/max(1-data(:,16))-1);

[AfitMC2YIM4T,hfitMC2YIM4T,yoff,fit] = fit_parabola_vertex(MC2Yoff,MC2YIM4T,A_guess,xoff_guess,yoff_guess,tolerance,showplot);
[AfitMC2YMC2T,hfitMC2YMC2T,yoff,fit] = fit_parabola_vertex(MC2Yoff,MC2YMC2T,A_guess,xoff_guess,yoff_guess,tolerance,showplot);
[AfitMC2Ymodel,hfitmodel,yoff,fit] = fit_parabola_vertex(MC2Ymodeloff,MC2Ytrans,A_guess,xoff_guess,yoff_guess,tolerance,showplot);

MC3Poff=data(:,17);
MC3PIM4T=(data(:,18)/max(data(:,18)));
MC3PMC2T=(data(:,19)/max(data(:,19)));
MC3PMCrefl=-((1-data(:,20))/max(1-data(:,20))-1);

[AfitMC3PIM4T,hfitMC3PIM4T,yoff,fit] = fit_parabola_vertex(MC3Poff,MC3PIM4T,A_guess,xoff_guess,yoff_guess,tolerance,showplot);
[AfitMC3PMC2T,hfitMC3MC2T,yoff,fit] = fit_parabola_vertex(MC3Poff,MC3PMC2T,A_guess,xoff_guess,yoff_guess,tolerance,showplot);
AfitMC3Pmodel=AfitMC1Pmodel;

MC3Yoff=data(:,21);
MC3YIM4T=(data(:,22)/max(data(:,22)));
MC3YMC2T=(data(:,23)/max(data(:,23)));
MC3YMCrefl=-((1-data(:,24))/max(1-data(:,24))-1);

[AfitMC3YIM4T,hfitMC3YIM4T,yoff,fit] = fit_parabola_vertex(MC3Yoff,MC3YIM4T,A_guess,xoff_guess,yoff_guess,tolerance,showplot);
[AfitMC3YMC2T,hfitMC3YMC2T,yoff,fit] = fit_parabola_vertex(MC3Yoff,MC3YMC2T,A_guess,xoff_guess,yoff_guess,tolerance,showplot);
AfitMC3Ymodel=AfitMC1Ymodel;

figure1=figure()
subplot(2,3,1)
plot(MC1Poff,MC1PIM4T,'g-o')
hold on
plot(MC1Poff,MC1PMC2T,'b-o')
plot(MC1Pmodeloff,MC1Ptrans,'r--')
%plot(MC1Poff,MC1PMCrefl,'b-o')
legend('IM4T','MC2T','Model','Location','South')
title('MC1 Pitch')
grid on
axis tight
xlabel('Alignment offset [\murad]')
ylabel('Normalized transmitted power [W/W]')
hold off
subplot(2,3,4)
plot(MC1Yoff,MC1YIM4T,'g-o')
hold on
plot(MC1Yoff,MC1YMC2T,'b-o')
plot(MC1Ymodeloff,MC1Ytrans,'r--')
%plot(MC1Yoff,MC1YMCrefl,'b-o')
title('MC1 Yaw')
grid on
axis tight
xlabel('Alignment offset [\murad]')
ylabel('Normalized transmitted power [W/W]')
hold off
legend('IM4T','MC2T','Model','Location','South')
subplot(2,3,2)
plot(MC2Poff,MC2PIM4T,'g-o')
hold on
plot(MC2Poff,MC2PMC2T,'b-o')
plot(MC2Pmodeloff,MC2Ptrans,'r--')
%plot(MC2Poff,MC2PMCrefl,'b-o')
legend('IM4T','MC2T','Model','Location','South')
title('MC2 Pitch')
grid on
axis tight
xlabel('Alignment offset [\murad]')
ylabel('Normalized transmitted power [W/W]')
hold off
subplot(2,3,5)
plot(MC2Yoff,MC2YIM4T,'g-o')
hold on
plot(MC2Yoff,MC2YMC2T,'b-o')
plot(MC2Ymodeloff,MC2Ytrans,'r--')
%plot(MC2Yoff,MC2YMCrefl,'b-o')
title('MC2 Yaw')
grid on
axis tight
xlabel('Alignment offset [\murad]')
ylabel('Normalized transmitted power [W/W]')
hold off
legend('IM4T','MC2T','Model','Location','South')
subplot(2,3,3)
plot(MC3Poff,MC3PIM4T,'g-o')
hold on
plot(MC3Poff,MC3PMC2T,'b-o')
plot(MC3Pmodeloff,MC3Ptrans,'r--')
%plot(MC3Poff,MC3PMCrefl,'b-o')
legend('IM4T','MC2T','Model','Location','South')
title('MC3 Pitch')
grid on
axis tight
xlabel('Alignment offset [\murad]')
ylabel('Normalized transmitted power [W/W]')
hold off
subplot(2,3,6)
plot(MC3Yoff,MC3YIM4T,'g-o')
hold on
plot(MC3Yoff,MC3YMC2T,'b-o')
plot(MC3Ymodeloff,MC3Ytrans,'r--')
%plot(MC3Yoff,MC3YMCrefl,'b-o')
title('MC3 Yaw')
grid on
axis tight
xlabel('Alignment offset [\murad]')
ylabel('Normalized transmitted power [W/W]')
hold off
legend('IM4T','MC2T','Model','Location','South')
%plotTFs('AllDOFs_w_model')

MC1Pscale=sqrt(AfitMC1Pmodel/AfitMC1PMC2T)
MC1Yscale=sqrt(AfitMC1Ymodel/AfitMC1YMC2T)
MC2Pscale=sqrt(AfitMC2Pmodel/AfitMC2PMC2T)
MC2Yscale=sqrt(AfitMC2Ymodel/AfitMC2YMC2T)
MC3Pscale=sqrt(AfitMC3Pmodel/AfitMC3PMC2T)
MC3Yscale=sqrt(AfitMC3Ymodel/AfitMC3YMC2T)

figure1=figure()
subplot(2,3,1)
plot(MC1Poff/MC1Pscale,MC1PIM4T,'g-o')
hold on
plot(MC1Poff/MC1Pscale,MC1PMC2T,'b-o')
plot(MC1Pmodeloff,MC1Ptrans,'r--')
%plot(MC1Poff,MC1PMCrefl,'b-o')
legend('IM4T','MC2T','Model','Location','South')
title('MC1 Pitch')
grid on
axis tight
xaxis(-40,40)
xlabel('Alignment offset [\murad]')
ylabel('Normalized transmitted power [W/W]')
hold off
subplot(2,3,4)
plot(MC1Yoff/MC1Yscale,MC1YIM4T,'g-o')
hold on
plot(MC1Yoff/MC1Yscale,MC1YMC2T,'b-o')
plot(MC1Ymodeloff,MC1Ytrans,'r--')
%plot(MC1Yoff,MC1YMCrefl,'b-o')
title('MC1 Yaw')
grid on
axis tight
xaxis(-40,40)
xlabel('Alignment offset [\murad]')
ylabel('Normalized transmitted power [W/W]')
hold off
legend('IM4T','MC2T','Model','Location','South')
subplot(2,3,2)
plot(MC2Poff/MC2Pscale,MC2PIM4T,'g-o')
hold on
plot(MC2Poff/MC2Pscale,MC2PMC2T,'b-o')
plot(MC2Pmodeloff,MC2Ptrans,'r--')
%plot(MC2Poff,MC2PMCrefl,'b-o')
legend('IM4T','MC2T','Model','Location','South')
title('MC2 Pitch')
grid on
axis tight
xaxis(-40,40)
xlabel('Alignment offset [\murad]')
ylabel('Normalized transmitted power [W/W]')
hold off
subplot(2,3,5)
plot(MC2Yoff/MC2Yscale,MC2YIM4T,'g-o')
hold on
plot(MC2Yoff/MC2Yscale,MC2YMC2T,'b-o')
plot(MC2Ymodeloff,MC2Ytrans,'r--')
%plot(MC2Yoff,MC2YMCrefl,'b-o')
title('MC2 Yaw')
grid on
axis tight
xaxis(-40,40)
xlabel('Alignment offset [\murad]')
ylabel('Normalized transmitted power [W/W]')
hold off
legend('IM4T','MC2T','Model','Location','South')
subplot(2,3,3)
plot(MC3Poff/MC3Pscale,MC3PIM4T,'g-o')
hold on
plot(MC3Poff/MC3Pscale,MC3PMC2T,'b-o')
plot(MC3Pmodeloff,MC3Ptrans,'r--')
%plot(MC3Poff,MC3PMCrefl,'b-o')
legend('IM4T','MC2T','Model','Location','South')
title('MC3 Pitch')
grid on
axis tight
xaxis(-40,40)
xlabel('Alignment offset [\murad]')
ylabel('Normalized transmitted power [W/W]')
hold off
subplot(2,3,6)
plot(MC3Yoff/MC3Yscale,MC3YIM4T,'g-o')
hold on
plot(MC3Yoff/MC3Yscale,MC3YMC2T,'b-o')
plot(MC3Ymodeloff,MC3Ytrans,'r--')
%plot(MC3Yoff,MC3YMCrefl,'b-o')
title('MC3 Yaw')
grid on
axis tight
xaxis(-40,40)
xlabel('Alignment offset [\murad]')
ylabel('Normalized transmitted power [W/W]')
hold off
legend('IM4T','MC2T','Model','Location','South')
%plotTFs('AllDOFs_scaled_w_model')

