% REFL in-vacuum path beam propagation - Summary for LHO
% HAM1 drawing v10 LHO - BEFORE moving TTs https://dcc.ligo.org/LIGO-D1000313-v10
% HAM1 drawing v11 LHO - AFTER moving TTs - it doesn't exist yet! 
% LisaBar - October 16


%%%% Beam parameter of the REFL beam right before RM1, coming from HAM2 %%%% 

% "Nominal" beam parameters 
qSam_orig = 1.03+13.1i;  % beam used to design the REFL WFS telescope, based on 
                         % "nominal" parameters and beam perfectly matched
                         % to PRM, and some approximate distances between
                         % HAM1 and HAM2 optical components
                          
qSam_eff = 2.8987 + 13.1440i; % Same as before, but by including "as-buil" HAM2-HAM1 distances

% As measured by Sheila et al, 
% Log entry 7950: https://alog.ligo-wa.caltech.edu/aLOG/index.php?callRep=7950

qX_RM1_Meas = 2.3108 + 9.5028i; % Based on first measurements before TTs with ModeMaster
qY_RM1_Meas = 3.5018 + 9.2085i; % Based on first measurements before TTs with ModeMaster

% This is Lisa's prediction of the beam that we should get from HAM2, based
% on a model from the IMC to HAM1, with "as-built" parameters - 
% N.B.: Paul's original estimate is different because of a typo in his
% code, the distance between the HAM1 edge and M2 is 17" x 2 = 34", not 17"

qH1X_RM1 = 2.8071 + 13.3724i; % Lisa's prediction
qH1Y_RM1 = 2.5071 + 13.0988i; % Lisa's prediction

% qX_RM1 = 2.3928 + 13.3100i; % Paul's prediction 
% qY_RM1 = 2.0738 + 13.0350i; % Paul's prediction


% Build beam path
reflWFS = beamPath;
qin = beamq;

% Define which q you want to use
%%%%%%%%%%%%%%%%%%%%%%%%%
qin.q = qX_RM1_Meas;

reflWFS.seedq = qin;
reflWFS.seedz = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%

%Distances between the curved mirrors

% ROC of the curved mirrors in REFL path
R = [1.7 -0.6 1.7];

% Nominal distances (as in drawing v10) 
% L = [0.831 0.854];

% Hanford distances before TTs motion (very close to nominal)
 L = [0.8195 0.8668];
 dM5_L101 = 1.1938;

% Hanford distances after TTs motion
%  L = [0.8602 0.9317];
%  dM5_L101 = 1.2192;


% WFS REFL sled as built at LHO - T1200555 
% 
f1 = 333.6e-3;      % r=150mm fused silica lens
f2 = -166.8e-3;     % r=-75mm fused silica lens

dL101_L102 = 0.1985; 
dL102_WFSA = 0.135+0.047+0.048+6*25.4e-3; 
dL102_WFSB = 0.135+0.047+0.048+0.368+6*25.4e-3; 


% Design calculations - for comparison - T1000247

% dM5_L101 = 1;
% f1 = 333e-3;      % r=150mm fused silica lens
% f2 = -167e-3;     % r=-75mm fused silica lens
% 
% dL101_L102 = 0.203; 
% dL102_WFSA = 0.367; 
% dL102_WFSB = 0.736; 


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'));

z_L101 = reflWFS.component('M5').z + dM5_L101;
    
reflLSC = reflWFS.duplicate;
         
reflWFS.addComponent(...
        component.lens(f1, z_L101, 'L101'));
reflWFS.addComponent(...
        component.lens(f2, reflWFS.component('L101').z + dL101_L102, 'L102'));
        
reflWFS.addComponent(...
        component.flatMirror(reflWFS.component('L102').z + dL102_WFSA, 'ASC-REFL\_A'));
reflWFS.addComponent(...
        component.flatMirror(reflWFS.component('L102').z + dL102_WFSB, 'ASC-REFL\_B'));
      
%      
% LSC path
% 
% M6 position as installed https://alog.ligo-wa.caltech.edu/aLOG/uploads/8023_20131007141244_IMG_0757.jpg
dM5_M6 = 711*1e-3;
z_BS_LSC = sum(L) + dM5_M6*1e-3;
    
reflLSC.addComponent(...
        component.flatMirror(z_BS_LSC, 'M6'));

% L1 position
z_L1_LSC = z_BS_LSC + 63*1e-3;
    
reflLSC.addComponent(...
        component.lens(222.4e-3, z_L1_LSC, 'L1'));

% LSC REFL diode
z_REFL_Diode = z_L1_LSC + 198*1e-3;
    
     reflLSC.addComponent(...
        component.flatMirror(z_REFL_Diode, 'LSC-REFL\_A'));


comp = length(reflLSC.components);    

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

gouyLSC = reflLSC.gouyPhase([reflLSC.components.z]);
beamWLSC = 1e3.*[reflLSC.qPropagate([reflLSC.components.z]).beamWidth];

for k = 1 : comp
    
    leg1{k} = sprintf('%s ',num2str(gouyLSC(k)));
    leg2{k} = sprintf('%s ',num2str(beamWLSC(k)));
    
end
    
outputLSC = [topRow; lineBreak; [{reflLSC.components.label}.',{reflLSC.components.z}.',leg2.', leg1.']];
    
display(outputLSC) ;
    
comp = length(reflWFS.components); 

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

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

for k = 1 : comp
    
    leg4{k} = sprintf('%s ',num2str(gouyASC(k)));
    leg3{k} = sprintf('%s ',num2str(beamWASC(k)));
    
end
    
outputASC = [topRow; lineBreak; [{reflWFS.components.label}.',{reflWFS.components.z}.',leg3.', leg4.']];
    
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('Gouy separation between WFS is %g', reflWFS.gouySeperation('ASC-REFL\_A', 'ASC-REFL\_B')))
   
%Plot REFL paths

figure(1)
zz = -.5:1e-2:5;
reflWFS.plotSummary(zz)
shg
display(reflWFS.components);
% saveas(1, 'REFL/REFL_WFS_beampath.pdf', 'pdf');
% saveas(1, 'REFL/REFL_WFS_beampath.fig', 'fig');

figure(2)

zz = -.5:1e-2:2.5;
reflLSC.plotSummary(zz)
shg
display(reflLSC.components);
% saveas(1, 'REFL/REFL_LSC_beampath.pdf', 'pdf');
% saveas(1, 'REFL/REFL_LSC_beampath.fig', 'fig');


