% REFL in-vacuum path beam propagation, HAM1 drawing v10
% https://dcc.ligo.org/LIGO-D1000313-v10
% LisaBar, August 14, 2013

q_in = 1.03+13.1i;  % Beam on HAM1 calculated from CalculatePRM.m 
                    % Lisa: we don't have a measurement yet which confirms
                    % this number! 

% 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];
% LLO current distances (as in drawing v9) 
%L = [0.805 0.988];
    
    
reflWFS = beamPath;
qin = beamq;
qin.q =  1.03+13.1i;
reflWFS.seedq = qin;
reflWFS.seedz = 0;


% Curved mirrors in REFL path
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'));


% Offset can be used to check that sled can be moved around     
offset =0e-2;

% LLO current tip-tilts distances (as in drawing v9)      
%z_L101 = reflWFS.component('M5').z + 22.5*25.4*2*1e-3 + 4.5*25.4*1e-3 + offset;

% Current version 10
% RM1 --> RM2: 832 mm
% RM1 --> RM5: 855 mm
% M5 --> M6: 733 mm
% M6 --> L1: 63 mm
% L1 --> LSC REFL diode: 198 mm
% M6 to edge of the sled: 326 mm 

dM5_M6 = 733*1e-3;
dM6_sled = 326*1e-3;

% LLO REFL sled as built at LLO - G1300837
% REFL WFS sled

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

z_L101 = reflWFS.component('M5').z + (dM5_M6 + dM6_sled) + 4.5*25.4*1e-3 + offset;
z_L202 = z_L101 + 203e-3;
z_WFSA = z_L202 + (140+74+59+130)*1e-3 ;
z_WFSB = z_WFSA + 355*1e-3 ;
    
reflLSC = reflWFS.duplicate;
         
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'));
      
     
% LSC path

% M6 position
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) 
   
%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');

% POSITIONS of OPTICS and DIODES

% outputLSC = 
% 
%     'label'          'z (m)'         'BeamWidth (mm)'    'Gouy Phase (dg)'
%     '-----'          '-----'         '----'              '----'           
%     'RM1'            [1.0000e-06]    '2.1129 '           '4.3469e-06 '    
%     'RM2'            [    0.8310]    '0.14517 '          '66.5778 '       
%     'M5'             [    1.6850]    '1.9985 '           '161.041 '       
%     'M6'             [    1.6857]    '1.9985 '           '161.0445 '      
%     'L1'             [    1.7487]    '1.9987 '           '161.3506 '      
%     'LSC-REFL\_A'    [    1.9467]    '0.22244 '          '170.026 '       
% 
% outputASC
% 
% outputASC = 
% 
%     'label'          'z (m)'         'BeamWidth (mm)'    'Gouy Phase (dg)'
%     '-----'          '-----'         '----'              '----'           
%     'RM1'            [1.0000e-06]    '2.1129 '           '4.3469e-06 '    
%     'RM2'            [    0.8310]    '0.14517 '          '66.5778 '       
%     'M5'             [    1.6850]    '1.9985 '           '161.041 '       
%     'L101'           [    2.8583]    '2.0109 '           '166.7156 '      
%     'L102'           [    3.0613]    '0.78964 '          '169.1971 '      
%     'ASC-REFL\_A'    [    3.4643]    '0.32428 '          '201.4068 '      
%     'ASC-REFL\_B'    [    3.8193]    '0.37138 '          '288.1028 '      

