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
Apath = 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;
Apath.seedWaist(waist_size, designZ, lambda);

% measurements with no lens

%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; 

%measurements taken with keita have a different rail placement and spacing.
%Keita rail placement
%2 3/8" on rail is 143mm from BS, BS is 89 mm from flipper, 97mm from flipper to
%A:M3, A:M3 is at 32.572 inches from OPO M1 according to design
zScan_keita =  32.572*25.4e-3+ (97+89)*1e-3 +[143e-3 - (2+3/8)*25.4e-3 ]+ ([2+3/8, 4, 6,9, 12])*25.4e-3; 
%measurements taken with A:L1 removed, just the beam out of the OPO, taken
%with Keita rail position
hWidth_noLens = [3323,3585,3732,3959,4236]*1e-6/2;
vWidth_noLens = [3361,3646,3783,3986,4286]*1e-6/2;

meas_noLens = beamPath;
meas_noLens.seedWaist(waist_size, designZ, lambda);
meas_nLh = meas_noLens.fitBeamWidth(zScan_keita, hWidth_noLens);
meas_nLv = meas_noLens.fitBeamWidth(zScan_keita, vWidth_noLens);
meas_nLh.targetWaist(waist_size, designZ, lambda);
meas_nLv.targetWaist(waist_size, designZ, lambda);


zdomain = -0.1:.01:2;
figure(10)
Apath;
hold on
Apath.plotBeamWidth(zdomain,'k')
Apath.plotWaists(zdomain)
meas_nLh.plotBeamWidth(zdomain, 'b')
plot(zScan,hWidth_noLens,'bo')
plot(zScan,vWidth_noLens,'rx')
xlabel('distance from OPO M1 [m]')
ylabel('beam radius [m]')
title(strcat('no lenses horiztonal overlap with design :',num2str(meas_nLh.targetOverlap*100,3),...
    '%, vertical overlap :', num2str(meas_nLv.targetOverlap*100, 3), '%'))
legend('design', 'design',...
    'horizontal fit', 'horizonal fit',...
    'horizontal measurements','vertical measurements',...
    'Location', 'SouthEast')
saveas(gcf, 'Apath_no_Lens.png')


%% add Lens 1
ApathL1 = Apath.duplicate;
ApathL1.addComponent(component.lens(0.222,234e-3,'A:L1+100mm ROC'));  

%meausremetns with L1 in place
hWidthL1 = [1632, 1886, 2080, 2360, 2617]*1e-6/2;
vWidthL1 = [1603, 1826, 2042, 2360, 2545]*1e-6/2;

measL1 = beamPath;
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


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')


%% replace L1 with 150mm ROC
%ApathL1_150 = Apath.duplicate;
%ApathL1_150.addComponent(component.lens(0.222,234e-3,'A:L1+100mm ROC'));
%%f=334mm

%meausremetns with L1 replaced by ROC 150mm, use zScan_Keita
hWidthL1_150 = [1939, 2012, 2108, 2247, 2397]*1e-6/2;
vWidthL1_150 = [1956, 2030, 2120, 2259, 2392]*1e-6/2;

measL1_150 = beamPath;
measL1_150.seedWaist(300e-6,0.5, lambda);
measL1150h = measL1_150.fitBeamWidth(zScan_keita,hWidthL1_150);
measL1150v = measL1_150.fitBeamWidth(zScan_keita,vWidthL1_150);
%keep the target the same as with 100 ROC lens
measL1150h.targetWaist(waist_sizeL1, designZL1, lambda);
measL1150v.targetWaist(waist_sizeL1, designZL1, lambda);
disp('measurement overlap with target with L1 150 only (h, v)')
measL1150h.targetOverlap
measL1150v.targetOverlap


zdomain = -0.1:.01:2;
figure(11)
ApathL1;
hold on
plot(zScan,hWidthL1,'bo')
plot(zScan,vWidthL1,'rx')
plot(zScan_keita,hWidthL1_150,'go')
plot(zScan_keita,vWidthL1_150,'cx')
ApathL1.plotBeamWidth(zdomain,'k')
ApathL1.plotWaists(zdomain)
measL1h.plotBeamWidth(zdomain, 'b')
measL1150h.plotBeamWidth(zdomain, 'g')
measL1150h.plotWaists(zdomain)
xlabel('distance from OPO M1 [m]')
ylabel('beam radius [m]')
legend('horizontal measurement, ROC = 100mm', 'vert meas, ROC = 100mm', ...
    'horiz meas, ROC = 150mm', 'vert meas, ROC = 150',...
    'design beam', 'design beam','fit 100mm horiz', 'fit 100mm',...
    'fit 150mm horiz',...
    'Location', 'SouthEast')
title(strcat('L1 only horiztonal overlap :',num2str(measL1h.targetOverlap*100,3),...
    '%, vertical overlap :', num2str(measL1v.targetOverlap*100, 3), '%'))
saveas(gcf, 'Apath_L1_only_add_150mm.png')

disp('waist location with 150mm lens installed')
loc_h = measL1150h.getWaists()

%% add L2

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;
M3_to_waist_ZM1 = designZL3 - 32.572*25.4e-3;
ApathL3.targetWaist(waist_sizeL3, designZL3, lambda);


%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;


% These measurements were taken with A:L1 moved closer the A:M2.  In the
% lab, I measured from the corner of the A:M2 mount to the closer edge of
% the lens mount, this was 23mm in the first round of measurements, and
% estimated that the lens was 25mm from A:M2.  This time, the edge of the
% lens mount to corner of A:M2 mount is 17mm, but with Keita we more
% carefully estimated that the lens is ~6mm from the edge of the lens
% mount, so about 23mm from A:M2 (still, 6mm closer to A:M2 than earlier measurement set).  

%use zscan_keita
%hWidthL3 = [889, 939.6, 1024, 1146, 1294]*1e-6/2;
%vWidthL3 = [687, 770.5, 872.6, 1058, 1249.5]*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 = 26.89*25.4e-3 + 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)')
measTFPh.targetOverlap
measTFPv.targetOverlap


%also, it is useful to propagate the path all the way back through the
%retro reflection, to see if measured beam parameter on FC side of the
%platform agrees with measurement on B path side.
%for this path we will have 0 be the location of ZM1 (figure 17)
prop_FC_to_B = beamPath;
prop_FC_to_B.addComponent(component.lens(0.778,9.85*25.4e-3,'A:L3+350mm ROC')); 
prop_FC_to_B.addComponent(component.lens(0.334,11.1*25.4e-3,'A:L2+150mm ROC')); 
%thin film polarizer is 26.89" from ZM1 (0.683meters)
prop_FC_to_Bh = prop_FC_to_B.duplicate;
prop_FC_to_Bv = prop_FC_to_B.duplicate;

%need to get info from measurements on FC side
%in figure 16, ZM1 is at 38.802 inches
h_w_location = measL3h.getWaists -38.802*25.4e-3
v_w_location = measL3v.getWaists -38.802*25.4e-3;

h_w_size = measL3h.qPropagate(measL3h.getWaists).waistSize;
v_w_size = measL3v.qPropagate(measL3v.getWaists).waistSize;

prop_FC_to_Bh.seedWaist(h_w_size, h_w_location, lambda);
prop_FC_to_Bv.seedWaist(v_w_size, v_w_location, lambda);

%% plotting



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

zdomain = [0:0.03:2];
figure(4)
SFITFP;
hold on
SFITFP.plotBeamWidth(zdomain,'k')
SFITFP.plotWaists(zdomain)
measTFPh.plotBeamWidth(zdomain,'b')
prop_FC_to_Bh.plotBeamWidth(zdomain, 'g')
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), '%'))
legend('design', 'design', 'measured after TFP')
saveas(gcf, 'Apath_L1_L2_L3_retro.png')