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

lambda = 1064e-9;

%translate Z, Zr from Lee's plot Fig 19 (pg 32) of T1900649 to waist size and location for alm
designZr= 1.190;
waist_size = sqrt(designZr*lambda/pi);
designZ = -0.1618;
Dpath = beamPath;
Dpath.seedWaist(waist_size, designZ, lambda);
design_lens_position = 1.092;
Dpath.addComponent(component.lens(0.222, design_lens_position, 'ROC +100mm d'))

meas_path = beamPath;  %start by making a beam with measured parmeters from Georgia's alog 60204
%I will put all distances from BS1
D_M2_actual = 0.335+0.390;  %distance from BS1 to D:M2 based on ruler measurements in chamber (not very precise)
meas_path.seedWaist(160e-6, D_M2_actual+ 0.5535, lambda);

%some useful distances
DM2_to_lens = design_lens_position - D_M2_actual
%Lens_to_BS1 = 1.168 - design_lens_position
%lens_to_DM3 = 1.320 - design_lens_position
DM2_to_DM3 = 1.32 -D_M2_actual
%back propagate through the lens to see what beam we get
back_meas = meas_path.branchPath(1);
back_meas.addComponent(component.lens(0.222, D_M2_actual + 0.185,'ROC +100mm'))

%make some measurements with the lens removed
scan1 = 165e-3 + [0, 1, 2, 3]*25.4e-3 ; %1st rail location 6" was 165 mm from DM2
scan2 = 292e-3 + [0, 1, 2, 3]*25.4e-3 ;%2nsd rail location 6" was 292mm from DM2
zScan = D_M2_actual + [scan1, scan2] ;  %fill in vector with distances from D:M2
hWidth_noLens = [1648, 1661, 1671, 1683, 1713, 1720, 1736, 1754]*1e-6/2;
vWidth_noLens = [1634, 1642, 1653, 1689, 1710, 1723, 1756, 1762]*1e-6/2;
meas_noLens = beamPath;
meas_noLens.seedWaist(waist_size, designZ, lambda);
meas_nLh = meas_noLens.fitBeamWidth(zScan, hWidth_noLens);
meas_nLv = meas_noLens.fitBeamWidth(zScan, vWidth_noLens);
meas_nLh.targetWaist(waist_size, designZ, lambda);
meas_nLv.targetWaist(waist_size, designZ, lambda);
disp('measurement overlap with no lens (h, v)')
meas_nLh.targetOverlap
meas_nLv.targetOverlap

%this seems like poor agreement between our measurement and the design.
%Alog 59333 seems to say that the mode matching of B path is good. 
zdomain = 0:.01:2;
figure(1)
meas_path;
hold on
%meas_path.plotBeamWidth(zdomain,'k')
%back_meas.plotBeamWidth(zdomain,'r')
Dpath.plotBeamWidth(zdomain,'b')
%back_meas.plotWaists(zdomain)
meas_nLv.plotBeamWidth(zdomain, 'k')
plot(zScan, vWidth_noLens, 'xk')
xlabel('Distance from B:BS1 [m]')
ylabel('Beam radius [m]')
title('blue design, black measurement')
saveas(gcf, 'D_path_beam_propagation.png')