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

%script used for switch to diamond fiber for CLF pre O4 June vent
lambda = 1064e-9;
goo = beamPath;
%waist of OPO
%from T1900649 fig 23   M2 is at 12.48inches
difference_design_to_built_path_length = 12.48*25.4e-3-0.307
goo.seedWaist(379.7e-6/2, 10.697*25.4e-3-difference_design_to_built_path_length); %from T1900649 fig 23  %This is the ideal waist, at 10.697 inches in Lee's coordinates, where M2 is at 12.48 inches
%distaces noted in alog 57960, using 0 as collimator position for now
goo.addComponent(component.lens(0.334,44e-3,'F:L1+150mm ROC')); 
%collimator to M2 of OPO is 205+102mm
goo.addComponent(component.flatMirror(0.205+0.102, 'M2'));  %in out layout M2 is at 0.307mm
%we want to match our beam to the mode of target beam at the collimator. 
col_target = goo.duplicate;
col_target = col_target.branchPath(0);
col_target.deleteComponent('F:L1+150mm ROC');
w_location = col_target.getWaists
w_size = col_target.qPropagate(goo.getWaists).waistSize

%fit data taken by Camilla and Austin
zScan = [210, 337, 464, 591, 718, 845, 972, 1099]*1e-3;
hWidth = [573.8, 476.4, 632.5, 981.8, 1355.2, 1708.5, 2097.9, 2535.1]*1e-6/2;
vWidth = [554.2, 457.4, 633.1, 1006.3, 1375, 1751.1, 2157.6, 2573.1]*1e-6/2;

%new data after adjusting collimator
zScan2 = [0, 4, 8, 12, 16]*25.4e-3 +0.210;
hWidth2 = [1002, 1009, 1029, 1076, 1131]*1e-6/2;
vWidth2 = [1011, 1017, 1053, 1104, 1166]*1e-6/2;

meas = beamPath;
meas.seedWaist(500e-6, 0.3, lambda);
meas_h = meas.fitBeamWidth(zScan, hWidth);
meas_v = meas.fitBeamWidth(zScan, vWidth);


meas2 = beamPath;
meas2.targetWaist(w_size, w_location, lambda);
meas2.seedWaist(500e-6, 0.3, lambda);
meas2_h = meas2.fitBeamWidth(zScan2, hWidth2);
meas2_v = meas2.fitBeamWidth(zScan2, vWidth2);

disp('print overlap with target beam')
meas2_h.targetOverlap
meas2_v.targetOverlap
zdomain = -0.1:.01:1;
figure(1)
goo;
hold on
plot(zScan,hWidth,'bo')
plot(zScan,vWidth,'ro')
plot(zScan2,hWidth2,'bx')
plot(zScan2,vWidth2,'rx')
goo.plotBeamWidth(zdomain,'k')
col_target.plotBeamWidth(zdomain,'m')
meas_h.plotBeamWidth(zdomain, 'b')
meas_v.plotBeamWidth(zdomain, 'r')
meas2_h.plotBeamWidth(zdomain, 'b--')
meas2_v.plotBeamWidth(zdomain, 'r--')

legend('measured beam from collimator horizontal',...
    'measured beam from collimator vertical',...
    'measured beam from collimator after adjustment h',...
    'measured beam from collimator after adjustment v',...
    'OPO mode propagated to collimator',' ',...
    'target collimator beam lens removed',...
    'Location', 'SouthEast')
title('Mode matching of new collimator to OPO')
xlabel('distance from collimator [m]')
ylabel('Beam radius [m]')
saveas(gcf, 'CLF_new_collimator.png')