%% POP path beam measurements

% data reported in alog 84307
% periscope bottom to dichroic splitter = 482 mm
% periscope distance = 319 mm
% from HAM3 curved mirror to top of periscope = 17734.169 mm + 1219.2 mm

close all
clear all

% reported as distances from the dichroic mirror
zs = [52 147 884 918 893 904 1118] * 1e-3 ;
wx = [6243 6202 870 279 721 483 3280] * 1e-6 / 2 ;
wy = [6404 6365 874 284 726 490 3372] * 1e-6 / 2 ;

popMeas = beamPath ;

% pop components from dichroic on
popMeas.addComponent(component.flatMirror(0, 'dichroic'))
popMeas.addComponent(component.flatMirror(360e-3, 'PM1'))
popMeas.addComponent(component.lens(334e-3,589e-3, 'L2'))
popMeas.addComponent(component.flatMirror(767e-3, 'M15'))

% adding in diode locations to calculate the beam width on the diodes
% my original design had the POP LSC diode at 143 mm from the beamsplitter
% and the POP WFS at 212 mm from the beamsplitter
% if you run this, you will find that the beam is pretty good for POP LSC
% (desired ~250 um in radius)
% and too large for POP WFS (desired ~350 um in radius)
% popMeas.addComponent(component(eye(2), 767e-3 + 140e-3, 'POP LSC'))
% popMeas.addComponent(component(eye(2), 767e-3 + 200e-3, 'POP WFS'))

% I made a small adjustment to these distances, again from the HR side of
% M15. I think if the actual distance is +- a few mm in either direction
% it's probably fine, but maybe there is a more rigorous way to calculate
% that.
% 140 mm for POP LSC
% 200 mm for POP WFS
%

popMeas.addComponent(component(eye(2), 767e-3 + 140e-3, 'POP LSC'))
popMeas.addComponent(component(eye(2), 767e-3 + 200e-3, 'POP WFS'))

qin = beamq ;
qin.q = -1 + 1i ;
popMeas.seedq = qin ;
popMeas.seedz = 0 ;

popXfit = popMeas.fitBeamWidth(zs,wx) ;
popYfit = popMeas.fitBeamWidth(zs,wx) ;

zdomain = -0.05:0.01:1.2 ;
figure(1)
subplot(2,1,1)
hold on
popXfit.plotBeamWidth(zdomain, 'r')
plot(zs,wx,'ko')
ylabel('Beam Width [m]')
xlim([-0.05,1.2])
title('POP Path, horizontal')
popMeas.plotComponents(zdomain)
hold off
subplot(2,1,2)
hold on
popXfit.plotGouyPhase(zdomain, 'r')
popMeas.plotComponents(zdomain)
ylabel('Gouy Phase [deg]')
xlabel('axial dimension, z [m]')
xlim([-0.05,1.2])
hold off

figure(2)
subplot(2,1,1)
hold on
popYfit.plotBeamWidth(zdomain, 'r')
plot(zs,wy,'ko')
ylabel('Beam Width [m]')
title('POP Path, vertical')
xlim([-0.05,1.2])
popMeas.plotComponents(zdomain)
hold off
subplot(2,1,2)
hold on
popYfit.plotGouyPhase(zdomain, 'r')
popMeas.plotComponents(zdomain)
ylabel('Gouy Phase [deg]')
xlabel('axial dimension, z [m]')
xlim([-0.05,1.2])
hold off


disp(['The beam size at the', ' ', popXfit.components(end).label, ' ', 'is', ' ', num2str(popXfit.qPropagate(popXfit.components(end).z).beamWidth*1e6),'um'])
disp(['The beam size at the', ' ', popXfit.components(end-1).label, ' ', 'is', ' ', num2str(popXfit.qPropagate(popXfit.components(end-1).z).beamWidth*1e6),'um'])
disp(['The gouy phase separation between the PM1 and vac POP WFS is' , ' ', num2str(popXfit.gouySeparation('PM1', 'POP WFS'))])
disp(['The gouy phase separation between the PM1 and LSC RFPD is' , ' ', num2str(popXfit.gouySeparation('PM1', 'POP LSC'))])

