addpath C:\Users\sheila.dwyer\Documents\MATLAB\alm-master
close all
clear classes

lambda = 1064e-9;

%% start by making paths with desired beam parameters with only some lenses installed
ApathL1 = beamPath;  %

%translate Z, Zr from Lee's plot Fig 16 of T1900649 to waist size and location for alm
designZr= 0.1053;
waist_size = sqrt(designZr*lambda/pi);
designZ = -1*-0.05182+0.251*25.4e-3;
ApathL1.seedWaist(waist_size, designZ, lambda);
ApathL1.addComponent(component.lens(0.222,234e-3,'A:L1+100mm ROC'));  

ApathL1L2 = ApathL1.duplicate;
slideL2 =0;% -60e-3; %try moving M2 to see if you can explain the measured beam parameters, set to 0 to get design beam
ApathL1L2.addComponent(component.lens(0.334,(27.452)*25.4e-3+slideL2,'A:L2+150mm ROC'));  
%translate Z, Zr from Lee's plot Fig 16 of T1900649 to waist size and
%location for alm, this could be our target waist
designZrL2= 272.2e-3;
waist_sizeL2 = sqrt(designZrL2*lambda/pi);
designZL2 = -1*-384.2e-3+(27.702)*25.4e-3;
ApathL1L2.targetWaist(waist_sizeL2, designZL2, lambda);

%now add L3
ApathL3 = ApathL1L2.duplicate;
slideL3 =0;% -60e-3; %try moving M2 to see if you can explain the measured beam parameters, set to 0 to get design beam
ApathL3.addComponent(component.lens(0.778,(28.702)*25.4e-3+slideL3,'A:L3+350mm ROC'));  
%translate Z, Zr from Lee's plot Fig 16 of T1900649 to waist size and
%location for alm, this could be our target waist
designZrL3= 120.6e-3;
waist_sizeL3 = sqrt(designZrL3*lambda/pi);
designZL3 = -1*-270.1e-3+(28.952)*25.4e-3;
ApathL3.targetWaist(waist_sizeL3, designZL3, lambda);
%% measurements
measL1 = beamPath;
%0" on rail is 95 mm from BS, 87 mm from flipper, 95mm from flipper to
%A:M3, A:M3 is at 32.572 inches from OPO M1 according to design

zScan = (97+89+92)*1e-3 + 32.572*25.4e-3+ ([0, 5, 10, 15, 20])*25.4e-3; 
%zScan = (95+87+95)*1e-3 + 32.572*25.4e-3+ ([0, 5, 10, 15, 20])*25.4e-3; 

hWidthL1 = [1632, 1886, 2080, 2360, 2617]*1e-6/2;
vWidthL1 = [1603, 1826, 2042, 2360, 2545]*1e-6/2;

measL1.seedWaist(300e-6,0.5, lambda);
measL1h = measL1.fitBeamWidth(zScan,hWidthL1);
measL1v = measL1.fitBeamWidth(zScan,vWidthL1);
designZrL1= 392.9e-3;
waist_sizeL1 = sqrt(designZrL1*lambda/pi);
designZL1 = -1*-43.99e-3+(9.462)*25.4e-3;
measL1h.targetWaist(waist_sizeL1, designZL1, lambda);
measL1v.targetWaist(waist_sizeL1, designZL1, lambda);
disp('measurement overlap with target with L1 only (h, v)')
measL1h.targetOverlap
measL1v.targetOverlap

%adding L2
measL1L2 = beamPath;
measL1L2.seedWaist(300e-6, 1, lambda);

%measurements taken with L2 at 67mm from DC2
%hWidthL1L2 = [739,853,1021,1215,1421]*1e-6/2;
%vWidthL1L2 = [725,829,1013,1200,1415]*1e-6/2; 

%measurements taken with L2 at 63mm from DC2
%hWidthL1L2 = [720,832,1002,1210,1424]*1e-6/2;
%vWidthL1L2 = [704,802,974,1168,1397]*1e-6/2;

%measurements taken with L2 at 58mm from DC2
%hWidthL1L2 = [712, 832, 990, 1196, 1405]*1e-6/2;
%vWidthL1L2 = [700,796,962,1158,1380]*1e-6/2;

%measurements with L2 mount swapped, still L2 about 58mm from A:DC2
hWidthL1L2 = [738, 860, 1030, 1219.6, 1444]*1e-6/2;
vWidthL1L2 = [715, 823, 994, 1189, 1406]*1e-6/2;

%do fits for measurements taken after L2, calculate overlap with design
measL1L2h = measL1L2.fitBeamWidth(zScan,hWidthL1L2);
measL1L2v = measL1L2.fitBeamWidth(zScan,vWidthL1L2);
measL1L2h.targetWaist(waist_sizeL2, designZL2, lambda);
measL1L2v.targetWaist(waist_sizeL2, designZL2, lambda);

disp('measurement overlap with target with L1, L2 (h, v)')
measL1L2h.targetOverlap
measL1L2v.targetOverlap


%measurements with L2 about 58mm from A:DC2, L3 about 34 mm from A:DC2
measL3 = beamPath;
measL3.seedWaist(200e-6, 1, lambda);

hWidthL3 = [612, 868, 1235, 1544, 1877]*1e-6/2;
vWidthL3 = [594, 857, 1209, 1530, 1875]*1e-6/2;

%do fits for measurements taken after L3, calculate overlap with design
measL3h = measL3.fitBeamWidth(zScan,hWidthL3);
measL3v = measL3.fitBeamWidth(zScan,vWidthL3);
measL3h.targetWaist(waist_sizeL3, designZL3, lambda);
measL3v.targetWaist(waist_sizeL3, designZL3, lambda);

disp('measurement overlap with target with L1, L2, L3 (h, v)')
measL3h.targetOverlap
measL3v.targetOverlap

%% measurement on SFI2 side of SFI1
%figure 17 of T1900649, start from SFI1TFP which is at 26.89 inches in that
%figure, this can be used as a seed and a target to compare to our
%measurement
SFITFP = beamPath;
designZrTFP= 408.2e-3;
waist_sizeTFP = sqrt(designZrTFP*lambda/pi);
designZTFP = -1*-25.95e-3;%+(26.890)*25.4e-3;
SFITFP.seedWaist(waist_sizeTFP, designZTFP, lambda);
measTFP = beamPath;
measTFP.seedWaist(waist_sizeTFP, designZTFP, lambda);
measTFP.targetWaist(waist_sizeTFP, designZTFP, lambda);

zScan_TFP = 310e-3 +[0, 10, 20, 30, 40, 50]*1e-2;
hWidthTFP = [1018, 1181, 1358, 1543, 1716, 1915]*1e-6/2;
vWidthTFP = [1003, 1181, 1357, 1550, 1746, 1958]*1e-6/2;

measTFPh = measTFP.fitBeamWidth(zScan_TFP,hWidthTFP);
measTFPv = measTFP.fitBeamWidth(zScan_TFP,vWidthTFP);

disp('measurement overlap with target on SFI2 side of SFI1 (after TFP) (h, v)')
measL3h.targetOverlap
measL3v.targetOverlap

%% plotting

zdomain = -0.1:.01:2;
figure(1)
ApathL1;
hold on
ApathL1.plotBeamWidth(zdomain,'k')
ApathL1.plotWaists(zdomain)
measL1h.plotBeamWidth(zdomain, 'b')
plot(zScan,hWidthL1,'bo')
plot(zScan,vWidthL1,'rx')
xlabel('distance from OPO M1 [m]')
ylabel('beam radius [m]')
title(strcat('L1 only horiztonal overlap ',num2str(measL1h.targetOverlap*100,3),...
    '%, vertical overlap ', num2str(measL1v.targetOverlap*100, 3), '%'))
saveas(gcf, 'Apath_L1_only.png')

figure(2)
ApathL1L2;
hold on
ApathL1L2.plotBeamWidth(zdomain,'k')
ApathL1L2.plotWaists(zdomain)
measL1L2h.plotBeamWidth(zdomain,'b')
plot(zScan,hWidthL1L2,'bo')
plot(zScan,vWidthL1L2,'rx')
xlabel('distance from OPO M1 [m]')
ylabel('beam radius [m]')
title(strcat('L1 L2 horiztonal overlap ',num2str(measL1L2h.targetOverlap*100,3),...
    '%, vertical overlap ', num2str(measL1L2v.targetOverlap*100, 3), '%'))
saveas(gcf, 'Apath_L1_L2.png')

figure(3)
ApathL3;
hold on
ApathL3.plotBeamWidth(zdomain,'k')
ApathL3.plotWaists(zdomain)
measL3h.plotBeamWidth(zdomain,'b')
plot(zScan,hWidthL3,'bo')
plot(zScan,vWidthL3,'rx')
xlabel('distance from OPO M1 [m]')
ylabel('beam radius [m]')
title(strcat('L1 L2 L3 horiztonal overlap ',num2str(measL3h.targetOverlap*100,3),...
    '%, vertical overlap ', num2str(measL3v.targetOverlap*100, 3), '%'))
saveas(gcf, 'Apath_L1_L2_L3.png')
Awaists = ApathL3.getWaists;
disp("distance from A:M3 to waist used for retro reflector")
retro_waist_fromAM3 = Awaists(3) - 32.572*25.4e-3

figure(4)
SFITFP;
hold on
SFITFP.plotBeamWidth(zdomain,'k')
SFITFP.plotWaists(zdomain)
measTFPh.plotBeamWidth(zdomain,'b')
plot(zScan_TFP,hWidthTFP,'bo')
plot(zScan_TFP,vWidthTFP,'rx')
xlabel('distance from TFP of SFI1 [m]')
ylabel('beam radius [m]')
title(strcat('L1 L2 L3, retro reflected, horiztonal overlap ',num2str(measTFPh.targetOverlap*100,3),...
    '%, vertical overlap ', num2str(measTFPv.targetOverlap*100, 3), '%'))

saveas(gcf, 'Apath_L1_L2_L3_retro.png')