% a script to check the measured mode out of ulaser systems collimator
% against 532 pump mode matching solution in figure 21 of https://dcc.ligo.org/DocDB/0163/T1900649/008/FCLayout_T1900649-v8.pdf
addpath C:\Users\sheila.dwyer\Documents\MATLAB\alm-master
close all
clear classes

lambda = 532e-9;
%start by fitting measured beam from collimator
goo = beamPath;
zScan = ((22-[2, 5, 9, 14, 18, 22])+13.25)*25.4e-3;
hWidth = [898, 832, 772, 712, 673, 640]*1e-6;
vWidth = [810, 748, 680, 620, 592, 568]*1e-6;  %second value was written as 1390um in my notebook, probably a trascription error
goo.seedWaist(300e-6,-0.5, lambda);

gooh = goo.fitBeamWidth(zScan,hWidth);
goov = goo.fitBeamWidth(zScan,vWidth);

%to make alm print out the actual waist size and location

horz_w_location = gooh.getWaists
vert_w_location = goov.getWaists


%make a path for what the beam out of collimator is by design
designbeam = beamPath;
designZr= 0.6431;
designbeam.seedWaist(sqrt(designZr*lambda/pi), 0, lambda);

zdomain = -2:.01:2;%linspace(min(zScan),max(zScan),100);
figure(1)
gooh
hold on
gooh.plotBeamWidth(zdomain,'b')
goov.plotBeamWidth(zdomain,'r')
designbeam.plotBeamWidth(zdomain,'k')
plot(zScan,hWidth,'bo')
gooh.plotWaists(zdomain,'b')
plot(zScan,vWidth,'ro')

goov.plotWaists(zdomain,'r')
designbeam.plotWaists(zdomain,'k')
hold off
legend('blue horizontal', 'red vertical', 'black T1900649 figure 21', 'Location', 'SouthEast')
xlabel('propagation distance from collimator meters')
ylabel('beam radius 13.5% meters')
saveas(gcf, 'ulaser_collimator_mode.png')

%add lenses that are designed to each of these three paths
gooh.addComponent(component.lens(0.1628,10*25.4e-3,'G:L1+75mm ROC'));   
gooh.addComponent(component.lens(-0.334,11.25*25.4e-3,'G:L2-150mm ROC')); 

goov.addComponent(component.lens(0.1628,10*25.4e-3,'G:L1+75mm ROC'));   
goov.addComponent(component.lens(-0.334,11.25*25.4e-3,'G:L2-150mm ROC')); 

designbeam.addComponent(component.lens(0.1628,10*25.4e-3,'G:L1+75mm ROC'));   
designbeam.addComponent(component.lens(-0.334,11.25*25.4e-3,'G:L2-150mm ROC')); 

%target from figure 21 of FC layout document
target_z = (11.25)*25.4e-3+0.211;
target_Zr = 0.1171;
target_waist = sqrt(target_Zr*lambda/pi);

gooh.targetWaist(target_waist, target_z, lambda);
goov.targetWaist(target_waist, target_z, lambda);
designbeam.targetWaist(target_waist, target_z, lambda);

figure(2)
gooh
hold on
gooh.plotBeamWidth(zdomain,'b')
gooh.plotBeams(zdomain,.5e-3,'b')
goov.plotBeamWidth(zdomain,'r')
goov.plotBeams(zdomain,-.5e-3,'r')
designbeam.plotBeamWidth(zdomain,'k')
%designbeam.plotBeams(zdomain,-.5e-3,'k')
hold off
xlabel('propagation distance from collimator meters')
ylabel('beam radius 13.5% meters')
legend('blue horizontal', 'red vertical', 'black T1900649 figure 21', 'Location', 'SouthWest')
saveas(gcf, 'ulaser_532_to_OPO.png')