clear all

%refl path in HAM1, LHO 
%RM1 is at 0
%mode master was at 40+sqrt(16^2+2^2) inches after M2
M2toModeMaster=(40+sqrt(16^2+2^2))*25.4e-3;
distM2toRM1=(26*2+1)*25.4e-3; %from counting holes on D1000313
RM1toModeMaster=M2toModeMaster-distM2toRM1;

%from Kieta's alog, as built distance
RM2distM=sqrt(32.25^2+1)*25.4e-3;
M5distM=RM2distM+sqrt(33.5^2+6.5^2)*25.4e-3;
L101distM=M5distM+(35+12)*25.4e-3;
L102distM=L101distM+0.1985; % T1200555
WFSAdist=L102distM+0.135+0.047+0.048+6*25.4e-3;
WFSBdist=L102distM+0.135+0.047+0.048+0.368+6*25.4e-3;
MeasXpos=RM1toModeMaster-2.387;
MeasYpos=RM1toModeMaster-3.578;
MeasXWo=3.588e-3/2;
MeasYWo=3.532e-3/2;

%for Aftter TT measurement, mode master was 66 and 3/8 " from M5
ModeMasterAfterPos=M5distM+(66+3/8)*25.4e-3;
MeasXAWo=1.38e-3;
MeasYAWo=1.15e-3;
MeasXApos=ModeMasterAfterPos-4.31;
MeasYApos=ModeMasterAfterPos-4.35;

f1 = 333e-3;      % r=150mm fused silica lens
f2 = -167e-3;     % r=-75mm fused silica lens

asBuilt=beamPath;
asBuilt.addComponent(...
        component.curvedMirror(1.7, 0+1e-6, 'RM1'));
asBuilt.addComponent(...
        component.curvedMirror(-0.6, RM2distM, 'RM2'));
asBuilt.addComponent(...
        component.curvedMirror(1.7, M5distM,'M5'));
asBuilt.addComponent(...
        component.lens(f1, L101distM, 'L101'));
asBuilt.addComponent(...
        component.lens(f2, L102distM, 'L102'));
        
asBuilt.addComponent(...
        component.flatMirror(WFSAdist, 'ASC-REFL\_A'));
asBuilt.addComponent(...
        component.flatMirror(WFSBdist, 'ASC-REFL\_B'));

measX=asBuilt.duplicate;
measY=asBuilt.duplicate;
measX.seedWaist(MeasXWo, MeasXpos);
measY.seedWaist(MeasYWo, MeasYpos);
measX.targetWaist(2.123e-3, -2.469+RM1toModeMaster);% what paul calculated the mode should be
measY.targetWaist(2.123e-3, -2.150+RM1toModeMaster);

%what is overlap between what we measured and what Paul expected?
OverlapX=measX.targetOverlap;
OverlapY=measY.targetOverlap;

PaulsBeamX=asBuilt.duplicate; 
PaulsBeamY=asBuilt.duplicate;
PaulsBeamX.seedWaist(2.123e-3, -2.469+RM1toModeMaster);
PaulsBeamY.seedWaist(2.123e-3, -2.150+RM1toModeMaster);

Paths=[PaulsBeamX, PaulsBeamY, measX, measY];

for ii=1:length(Paths)
    Gouy_Separation_TT_B4(ii)=Paths(ii).gouySeperation('RM1', 'RM2');
    Gouy_Separation_WFS_B4(ii)=Paths(ii).gouySeperation('ASC-REFL\_A', 'ASC-REFL\_B');
end

% try moving TTs

delta=1.6; %inches along table to move RM2 towards HAM2 (keeping RM1 and RM2 positions the same
delta2=1; %distance to move RM2 towards PSl 9 inches max (8)
changeRM2=(sqrt((32.25+delta)^2+1)-sqrt(32.25^2+1))*25.4e-3;
changeM5=(sqrt((33.5+delta+delta2)^2+6.5^2)*25.4e-3-sqrt(33.5^2+6.5^2)*25.4e-3)+changeRM2;
sledMove=delta2*25.4e-3; %+1 inch, -2 inches max

for ii=1:length(Paths)
    Paths(ii).moveComponent('RM2', changeRM2);
    Paths(ii).moveComponent('M5', changeM5);
    Paths(ii).moveComponent('L101', changeM5+sledMove);
    Paths(ii).moveComponent('L102', changeM5+sledMove);
    Paths(ii).moveComponent('ASC-REFL\_A', changeM5+sledMove);
    Paths(ii).moveComponent('ASC-REFL\_B', changeM5+sledMove);
end


for ii=1:length(Paths)
    Gouy_Separation_TT(ii)=Paths(ii).gouySeperation('RM1', 'RM2');
    Gouy_Separation_WFS(ii)=Paths(ii).gouySeperation('ASC-REFL\_A', 'ASC-REFL\_B');
end

figure(1)
zz = -1:1e-2:4.5;
measX.plotSummary(zz)
shg

figure(2)
measY.plotSummary(zz)
shg

figure(11)
PaulsBeamX.plotSummary(zz)
shg

figure(12)
PaulsBeamY.plotSummary(zz)
shg
