%% MEASURED q Parameters at WFS locations  - v12 of D1000313

%% Beam parameters at WFSA location 
% X - hor
% 
% ans = 
% 
%   beamq
% 
%   Properties:
%                     q: -0.321418663931785 +     0.337763685711306i
%                lambda: 1.064e-06
%             waistSize: 0.000338222390829693
%                waistZ: -0.321418663931785
%       divergenceAngle: 0.00100135806523256
%     radiusOfCurvature: -0.676358560668764
%             beamWidth: 0.000466888784448756
%         rayleighRange: 0.337763685711306
% 
%   Methods
% 
% Y - ver
% 
% ans = 
% 
%   beamq
% 
%   Properties:
%                     q: -0.267005638758058 +      0.32492922337271i
%                lambda: 1.064e-06
%             waistSize: 0.000331734212725438
%                waistZ: -0.267005638758058
%       divergenceAngle: 0.00102094298962123
%     radiusOfCurvature: -0.662424254981593
%             beamWidth: 0.00042936814509686
%         rayleighRange: 0.32492922337271
% 
%   Methods
% 
%% Beam parameters at WFSB location
% X - hor
% 
% ans = 
% 
%   beamq
% 
%   Properties:
%                     q: 0.248581336068215 +     0.337763685711306i
%                lambda: 1.064e-06
%             waistSize: 0.000338222390829693
%                waistZ: 0.248581336068215
%       divergenceAngle: 0.00100135806523256
%     radiusOfCurvature: 0.707522900989158
%             beamWidth: 0.000419946445691841
%         rayleighRange: 0.337763685711306
% 
%   Methods
% 
% Y - ver
% 
% ans = 
% 
%   beamq
% 
%   Properties:
%                     q: 0.302994361241942 +      0.32492922337271i
%                lambda: 1.064e-06
%             waistSize: 0.000331734212725438
%                waistZ: 0.302994361241942
%       divergenceAngle: 0.00102094298962123
%     radiusOfCurvature: 0.65144639107126
%             beamWidth: 0.000453584396023948
%         rayleighRange: 0.32492922337271
% 
%   Methods
% 
% Gouy separation between WFSs in X(hor) is 79.9313
% Gouy separation between WFSs in Y(ver) is 82.4105



%% Here below are as-built parameters

% Here I use the as predicted IO beam as input beam to show how the
% telescope is built 
% Input Parameters right before RM1 (before curved surface) as predicted by Paul propagating back to HAM1
% measurements that Chris did on ISCT1
qx=(3.17438 + 13.4501i); % IO predicted - not measured
qy=(2.72977 + 13.2139i); % IO predicted - not measured

% LLO curved mirror positions relative distances (RM1-RM2-M5)

% Current position as built at LLO (v9 of D1000313), uncertainty - half cm
L = [0.805 0.988];

% Nominal telescope parameters
%L = [0.831 0.855];

% Radius of curvatures (nominal values are 1.7m, -0.6, 1.7m, 
% uncertainty on these ROCs is a few cm)
R = [1.75 -0.6 1.75];

% Sled parameters    
f1 = 333.6e-3;      % r=150mm fused silica lens
f2 = -166.8e-3;     % r=-75mm fused silica lens
   
reflWFS = beamPath;
qin = beamq;
qin.q = qx %x
reflWFS.seedq = qin;
reflWFS.seedz = 0;



reflWFS.addComponent(...
     component.curvedMirror(R(1), 0+1e-6, 'RM1'));
reflWFS.addComponent(...
      component.curvedMirror(R(2), L(1),'RM2'));
reflWFS.addComponent(...
      component.curvedMirror(R(3), sum(L),'M5'));

    
% Nominal Parameters    
%     z_L101 = 2.686;
%     z_L202 = 2.889;
%     z_WFSA = 3.256;
%     z_WFSB = 3.625;

% LLO Parameters
     
%Current position as built (from solidworks v9)
z_L101 = reflWFS.component('M5').z + (724 + 336)*1e-3 + 4.5*25.4*1e-3;
% Sled, as built: G1300837
z_L202 = z_L101 + 203e-3;

% Measured by Keiko before moving WFSA
% These measurements
% https://alog.ligo-la.caltech.edu/aLOG/index.php?callRep=10105 corresponds
% to WFSA location z = zL102 + 0.38.
% z_WFSA = z_L202 + 0.38;
% z_WFSB = z_WFSA + 0.37;

% Current distances
% WFSA has then be moved about 20cm closer to L102 (z = zL102 + 0.18), to improve Gouy phase
% separation
z_WFSA = z_L202 + 0.38 - 0.20 ;
z_WFSB = z_WFSA + 0.37 + 0.20 ;
    
    
reflWFS.addComponent(...
        component.lens(f1, z_L101, 'L101'));
reflWFS.addComponent(...
        component.lens(f2, z_L202, 'L102'));
         
reflWFS.addComponent(...
        component.flatMirror(z_WFSA, 'ASC-REFL\_A'));
reflWFS.addComponent(...
        component.flatMirror(z_WFSB , 'ASC-REFL\_B'));
    

topRow =    {'label','z (m)','BeamWidth (mm)', 'Gouy Phase (dg)'};
lineBreak = {'-----','-----','----', '----'};

gouyASC = reflWFS.gouyPhase([reflWFS.components.z]);
beamWASC = 1e3.*[reflWFS.qPropagate([reflWFS.components.z]).beamWidth];

comp = length(reflWFS.components); 

for k = 1 : comp
    leg3{k} = sprintf('%s ',num2str(gouyASC(k)));
    leg4{k} = sprintf('%s ',num2str(beamWASC(k)));
end
    
outputASC = [topRow; lineBreak; [{reflWFS.components.label}.',{reflWFS.components.z}.',leg4.', leg3.']];
    
display(outputASC) 

display(sprintf('Gouy separation between RM1 and RM2 is %g', reflWFS.gouySeperation('RM1', 'RM2')))
display(sprintf('Gouy separation between M5 and RM2 is %g', reflWFS.gouySeperation('M5', 'RM2')))
display(sprintf('Gouy separation between M5 and RM1 is %g', reflWFS.gouySeperation('M5', 'RM1')))
display(sprintf('RM1 - RM2 distance is %g',reflWFS.component('RM2').z - reflWFS.component('RM1').z ))
display(sprintf('RM2 - M5 distance is %g',reflWFS.component('M5').z - reflWFS.component('RM2').z ))
display(sprintf('M5 - L101 distance is %g',reflWFS.component('L101').z - reflWFS.component('M5').z ))
display(sprintf('Distance between L102 and L101 %g',  reflWFS.component('L102').z - reflWFS.component('L101').z))  
display(sprintf('Distance between L102 and WFSA %g',  reflWFS.component('ASC-REFL\_A').z - reflWFS.component('L102').z))  
display(sprintf('Distance between L102 and WFSB %g', reflWFS.component('ASC-REFL\_B').z - reflWFS.component('L102').z))   
display(sprintf('Gouy separation between WFS WFS is %g', reflWFS.gouySeperation('ASC-REFL\_B', 'ASC-REFL\_A')))    


figure(10)
zz = -.5:1e-2:5;
reflWFS.plotSummary(zz)
shg
display(reflWFS.components);

