% starting from sheila's code for green path FCGS fiber coupling from lho59873 
% and georgia's code from lho 57233

addpath /ligo/home/victoriaa.xu/Documents/MATLAB/alm-master

close all
clear classes

goo = beamPath;

% Define constants
mm       = 1e-3;
cm       = 1e-2;
um       = 1e-6;
in       = 0.0254;

goo.seedWaist(.1e-3,1.3,532e-9);


% Friday 25 Oct 2024 beam profiles
% Start by fitting measured beam that we took, with the beam profiler after GAOM1
% starting from z=0 at the EOM mount edge nearest to the AOM
% ending at front of the beam profiler face
% (ie, not sure this includes distance from beam profiler front edge to profiler face)
zL15 = 1;      % from Georgia, distance of L15 after SHG (m), prob ok
zPBS = zL15 + 3*in;  % from Georgia, distance of L15 after SHG (m), looks legit
zEOM = zL15 + 5*in;  % z=9 for our measurement in real life
z0EOM= zL15 + 7*in;  % EOM mount edge nearest to the AOM, where measurements called z=0
zAOM = z0EOM + 4.5*in - 1.5*in;
zAOMold = z0EOM + 4.5*in;

zScan = z0EOM + [15 32 47 260 305] * mm;
xWidth = [226 230 251 890 1081] * 1e-6/2; % 13.5% A1
yWidth = [346 310 271 622 753] * 1e-6/2;  % 13.5% A2

% fit beam profiles
goox = goo.fitBeamWidth(zScan,xWidth);
gooy = goo.fitBeamWidth(zScan,yWidth);




% % Tuesday 22 Oct 2024 beam profiles
% % starting from z=0 at the EOM mount edge nearest to the AOM
% % ending at front of the beam profiler face
v0_zScan = z0EOM + [255 293 330] * mm;
v0_xWidth = [720 645 720] * 1e-6/2;
v0_yWidth = [571 729 813] * 1e-6/2;



% from georgia for FCGS path
% z1 = [2.5 4 9 10 12 14 16]*0.0254 + 1.076;
% x1 = [460 353 211 300 440 599 800]/2*1e-6;
% y1 = [428 320 224 275 396  525 692]/2*1e-6;
% z2 = [4 5 6 7 8 9]*0.0254 + 1.4955
% x2 = [400 323 245 202 199 224]/2*1e-6
% y2 =  [432 357 273 208 196 210]/2*1e-6


% adding parts for the PUMP ISS path
gooy.addComponent(component.flatMirror(zL15,'ROC75'));
gooy.addComponent(component.flatMirror(zPBS,'PBS'));
gooy.addComponent(component.dielectric(inf,inf,0.025,1.5,zEOM,'EOM'));
gooy.addComponent(component.dielectric(inf,inf,0.025,1.5,zAOM,'GAOM1'));
gooy.addComponent(component.dielectric(inf,inf,0.025,1.5,zAOMold,'GAOM1 (old)'));
% add some of the FCGS to the plot to cross check with photos
gooy.addComponent(component.dielectric(inf,inf,0.025,1.5,1.1522,'FCAOM1'));
gooy.addComponent(component.flatMirror(1.2538, 'FCMG1'))
gooy.addComponent(component.dielectric(inf,inf,0.025,1.5,1.3656,'FCAOM2'))

% from georgia for FCGS path to cross check
goox.addComponent(component.flatMirror(1,'ROC75'));
goox.addComponent(component.flatMirror(1.076,'PBS'));
goox.addComponent(component.dielectric(inf,inf,0.025,1.5,1.1522,'AOM1'));
goox.addComponent(component.flatMirror(1.2538, 'MG1'))
goox.addComponent(component.dielectric(inf,inf,0.025,1.5,1.3656,'AOM2'))
goox.addComponent(component.lens(0.1,1.4955,'lens1'));
goox.addComponent(component.dielectric(inf,inf,0.025,1.5,1.5715,'EOM'));
goox.addComponent(component.lens(100e-3,1.7622,'lens2'));
goox.addComponent(component.lens(50e-3,2.0974,'lens3'));


% plot the results
zplot = 0.9:.005:1.6;

figure(10)
hold on
goox.components;
plot(zScan,yWidth,'rx') %pit
gooy.plotSummary(zplot);
ylim([-5e-4,5e-4])

figure(30)
hold on
gooy.components;
plot(zScan,yWidth,'rx') %pit
gooy.plotSummary(zplot);
ylim([-5e-4,5e-4])

figure(20)
clf
hold on
plot(zScan, xWidth,'bo','LineWidth',2,'MarkerSize',8) %yaw
plot(zScan,yWidth,'rx','LineWidth',2,'MarkerSize',8) %pit
plot(v0_zScan,v0_yWidth,'k*','LineWidth',2,'MarkerSize',8) %old pit
gooy.plotBeamWidth(zplot,'r','LineWidth',1.5)
gooy.plotBeams(zplot,2e-4,'r')
gooy.plotComponents()
yline(750e-6/2,'--')
yline(-750e-6/2,'--')
hold off
grid on
axis tight
% ylim([-5e-4,5e-4])
% xlim([0.9,1.6])
title('Fri 25 Oct 2024. Green beam after SHG. Red = pit. Blue = yaw.')
xlabel('Distance from SHG [m]','FontSize',16)
ylabel('Beam radius [m]','FontSize',16)
set(gca,'FontSize',16)

legend('x meas (fri)','y meas (fri)', 'old y meas (tues)')