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

lambda = 1064e-9;

%for the record, we measured some distances after making sure beams were
%centered on optics reasonably well. 

%OPO M1 to A:DC1 112mm
%A:DC1 to A:M1 67mm
%A:M1 to A:M2 30 mm  
%A:M2 to TFP 89mm  M2 is 209mm from OPO M1, TFP is 298mm
%TFP to SFI1 output 261mm    
%total distance from OPO M1 to TFP:298mm, should be 11.912" (303 mm) according to
%Lee's plot
%SFI1 output to A:DC2 212mm
%A:DC2 to A:M3 64mm
%%total distance from OPO M1 to A:M3 is 835mm according to us, should be
%%32.572" (827mm) according to Lee
OPO_to_AM3_meas = 0.835;
OPO_to_AM3_design = 0.827;
%A:M3 to retro reflector is 179mm
%% start by making paths with desired beam parameters with only some lenses installed, starting from option 2 plot attached to LHO alog 58342
Apath = beamPath;  %
%translate Z, Zr from Lee's plot to waist size and location for alm
designZr= 0.1456;
waist_size = sqrt(designZr*lambda/pi);
designZ = -1*-0.05228+0.251*25.4e-3;
Apath.seedWaist(waist_size, designZ, lambda);

%We want the right beam parameter after A:M3,
%so match the beam parameter at A:M3 to design.  it doesn't much matter if
%the real distance from OPO to M3 is closer to the measured or designed
%distance, but for the sake of book keeping we need to stay consistent.
OPO_to_AM3 = OPO_to_AM3_meas;

%If I use measured distance, I get 100% and 99.9% overlap with the measured
%beam after adding A:L1.  If I use design, I get 99.8% and 99.9%.
%% add Lens 1
ApathL1 = Apath.duplicate;
ApathL1.addComponent(component.lens(0.222,229e-3,'A:L1+100mm ROC'));  
%oroingally tried to place curved surface (on retaining ring) 25mm from A:M2, which
%makes it 234mm from OPO M1 according to our measurements.  according to
%Lee's plot, should be 9.212", or 243mm. good
%April 5th we attempted to move it 5mm closer to A:M2, when we measured we
%also are estimating that the curved surface is about 25mm from A:M2,
%although it is defintely closer after the move.  We measured 17mm from the
%corner of the A:M2 mount to the closer side of the lens mount after the
%move. I'm estimating that it's 5mm closer. 
 
%measurement of nanoscan rail placement
%A:M3 to flipper mirror 95mm
%flipper mirror to BS 92mm
%BS to front of nanoscan when placed at 0" on rail: 97mm
zScan = OPO_to_AM3 + 95e-3+92e-3+97e-3 +[0, 1, 4, 7, 10, 14]*25.4e-3;
%meausremetns with L1 in place
hWidthL1 = [1735, 1764, 1911, 2083, 2273, 2455]*1e-6/2;
vWidthL1 = [1710, 1757, 1898, 2053, 2227, 2422]*1e-6/2;

%what the beam should be after L1, from second attachment to 58474
designZrL1= 325.3e-3;
waist_sizeL1 = sqrt(designZrL1*lambda/pi);
designZL1 = -1*-125.6e-3+(9.515)*25.4e-3;
%fit measurement
measL1 = beamPath;
measL1.seedWaist(waist_sizeL1,designZL1, lambda);
measL1h = measL1.fitBeamWidth(zScan,hWidthL1);
measL1v = measL1.fitBeamWidth(zScan,vWidthL1);

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
plot(zScan,hWidthL1,'bo')
plot(zScan,vWidthL1,'rx')
ApathL1.plotBeamWidth(zdomain,'k')
ApathL1.plotWaists(zdomain)
measL1h.plotBeamWidth(zdomain, 'b')

measL1v.plotBeamWidth(zdomain, 'r')

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), '%'))
legend( 'horizontal measurements', 'vertical measurements', 'model',' ', 'vertical fit', ' ', 'horizontal fit', 'Location', 'SouthEast')
saveas(gcf, 'Apath_L1_only.png')


%% add L2, here what we are really aiming for is a beam parameter after A:M3, we don't care as much about the total distance from OPO M1 to A:M3 as the beam parameter.
ApathL1L2 = ApathL1.duplicate;
L2_b4_M3 =30e-3 + 64e-3;% A:DC2 is 64mm in front of A:M3, A:L2 is another ~30mm after move April 5th
ApathL1L2.addComponent(component.lens(0.222,OPO_to_AM3-L2_b4_M3,'A:L2+100mm ROC'));  

%updated from second attachment to 58474, using beam parameter at A:M3
designZrL2= 121.3e-3;
waist_sizeL2 = sqrt(designZrL2*lambda/pi);
designZL2 = -1*-185.2e-3+(OPO_to_AM3);%let's use measured distances to estimate how far A:M3 is from OPO M1, see how that goes
ApathL1L2.targetWaist(waist_sizeL2, designZL2, lambda);

%display the beam we want
Apath_goal = beamPath;
Apath_goal.seedWaist(waist_sizeL2, designZL2, lambda);

%adding beam measured after L2
measL1L2 = beamPath;
measL1L2.seedWaist(waist_sizeL2, designZL2, lambda);
measL1L2.targetWaist(waist_sizeL2, designZL2, lambda);
%measurements with L2 (+100mm ROC) ~50mm from A:DC2 (closer edge of double
%lens mount is 38mm from A:DC2, stage is centered)
%hWidthL1L2 = [594.7,636.1,705.7,769.0,824.5,974.2]*1e-6/2;
%vWidthL1L2 = [596.6,641.7,708.2,773.0,836.6,982.5]*1e-6/2;

%new set of measurements, April 5 2021, L1 moved closer to A:M3 by 5mm,
%A:L2 is now ~30mm from A:DC2. 
zScan = OPO_to_AM3 + 95e-3+92e-3+97e-3 +[0, 3, 6, 9, 12, 15]*25.4e-3;
hWidthL1L2 = [554, 729, 938, 1167, 1390, 1627]*1e-6/2;
vWidthL1L2 = [558, 721, 927, 1134, 1361, 1584]*1e-6/2;
%do fits for measurements taken after L2, calculate overlap with design
measL1L2h = measL1L2.fitBeamWidth(zScan,hWidthL1L2);
measL1L2v = measL1L2.fitBeamWidth(zScan,vWidthL1L2);

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

zdomain = 0:.01:2;
figure(2)
ApathL1L2;
hold on
plot(zScan,hWidthL1L2,'bo')
plot(zScan,vWidthL1L2,'rx')
Apath_goal.plotBeamWidth(zdomain, 'm')
ApathL1L2.plotBeamWidth(zdomain,'k')
measL1L2h.plotBeamWidth(zdomain, 'b')
measL1L2v.plotBeamWidth(zdomain, 'r')
%ApathL1L2.plotWaists(zdomain)
%Apath_goal.plotWaists(zdomain)
%pathList.plotBeamWidth(zdomain, 'c')
%pathList.plotWaists(zdomain)
xlabel('distance from OPO M1 [m]')
ylabel('beam radius [m]')
legend('horizontal measurements', 'vertical measurements','target beam', ' ','model with measured distances', ' ',...
    'horz fit',' ', 'vert fit','Location', 'SouthEast')
title(strcat('L1 + L2 placed horiztonal overlap :',num2str(measL1L2h.targetOverlap*100,3),...
    '%, vertical overlap :', num2str(measL1L2v.targetOverlap*100, 3), '%'))
saveas(gcf, 'Apath_L1_L2.png')


% %% 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(3)
% 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')