%This code is based on Dan's propagate_AS from an OMC alog in c. June 2015.
%Many of the parameters in it come from Lisa and Paul.

%% Initialization
addpath C:\Users\Eleanor\OneDrive\SRCguoy
addpath C:\Users\Eleanor\OneDrive\SRCguoy\alm-master
H1parametersNEW_ITMs;

lambda = 1.064e-6;

dL1_ASAIR=0.9954; %measured by me on table 11/10 %1.128; %+/-0.04 [m], measured by me on table.  See table drawing D120120 for reference as well.
fL1=0.6785;%from D1201210, table layout. %%146; %this and distance for L1 on IST6 before AS_Air camera, defined by LIsa in T1200410
fL2=inf;%just an ND filter. %2.5; %[m]  estimated, need to talk to Kiwamu about this or measure.
dOM1_L1=6.221-3.571;
dWaist_SM1 = (122.6 + 428.2)*1e-3;

%% double shot through SRC, to ASAIR
beam2shotASAIR=beamPath;
beam2shotASAIR.seedq = beamq(-0.2325 + 13.3377i); % q at MC3 HR - path for single bounce X
beam2shotASAIR.seedz = 0;

beam2shotASAIR.addComponent(component.flatMirror(dWaist_SM1, 'SM1f'));
beam2shotASAIR.addComponent(component.curvedMirror(ROC_PMMT1*cos(7*pi/180), beam2shotASAIR.component('SM1f').z + dSM1_PMMT1_eff, 'PMMT1f'));
beam2shotASAIR.addComponent(component.curvedMirror(ROC_PMMT2*cos(7*pi/180), beam2shotASAIR.component('PMMT1f').z + dPMMT2_PMMT1_eff, 'PMMT2f'));
beam2shotASAIR.addComponent(component.flatMirror(beam2shotASAIR.component('PMMT2f').z + dSM2_PMMT2_eff, 'SM2f'));
beam2shotASAIR.addComponent(component.dielectric(inf, R_PRM, tPRM, nCPX, beam2shotASAIR.component('SM2f').z + dPRM_SM2_eff + tPRM/idxPRM, 'PRMHR'));
beam2shotASAIR.addComponent(component.curvedMirror(R_PR2,  beam2shotASAIR.component('PRMHR').z + dPRM_PR2, 'PR2'));
beam2shotASAIR.addComponent(component.curvedMirror(R_PR3, beam2shotASAIR.component('PR2').z + dPR2_PR3, 'PR3'));
beam2shotASAIR.addComponent(component.dielectric(R_BS, inf, dBSHR_BSAR, nSRM, beam2shotASAIR.component('PR3').z + dPR3_BSHR, 'BSHR'));
beam2shotASAIR.addComponent(component.dielectric(R_CPY, inf, tCPY, nCPY, beam2shotASAIR.component('BSHR').z + dBSHR_CPY1, 'CPYf'));
beam2shotASAIR.addComponent(component.curvedMirror(-R_ITMY/nITMY, beam2shotASAIR.component('CPYf').z + dCPY2_ITYAR + tITMY/nITMY, 'ITYHR'));
beam2shotASAIR.addComponent(component.dielectric(inf, -R_CPY, tCPY, nCPY, beam2shotASAIR.component('ITYHR').z + dCPY2_ITYAR, 'CPYb'));
beam2shotASAIR.addComponent(component.dielectric(inf, -R_BS, dBSHR_BSAR, nSRM, beam2shotASAIR.component('CPYb').z + dBSHR_CPY1, 'BSAR')); 
beam2shotASAIR.addComponent(component.curvedMirror(R_SR3, beam2shotASAIR.component('BSAR').z + dSR3_BSAR, 'SR3'));
beam2shotASAIR.addComponent(component.curvedMirror(R_SR2, beam2shotASAIR.component('SR3').z + dSR2_SR3 , 'SR2'));

beam2shotASAIR.addComponent(component.curvedMirror(R_SRM,beam2shotASAIR.component('SR2').z + dSRM_SR2, 'SRM_2'));

beam2shotASAIR.addComponent(component.curvedMirror(R_SR2, beam2shotASAIR.component('SRM_2').z + dSRM_SR2 , 'SR2_2'));
beam2shotASAIR.addComponent(component.curvedMirror(R_SR3, beam2shotASAIR.component('SR2_2').z +  dSR2_SR3, 'SR3_2'));
beam2shotASAIR.addComponent(component.dielectric(inf, -R_BS, dBSHR_BSAR, nSRM, beam2shotASAIR.component('SR3_2').z + dSR3_BSAR, 'BSAR_2'));
beam2shotASAIR.addComponent(component.dielectric(R_CPY, inf, tCPY, nCPY, beam2shotASAIR.component('BSAR_2').z + dBSHR_CPY1, 'CPYf_2'));
beam2shotASAIR.addComponent(component.curvedMirror(-R_ITMY/nITMY, beam2shotASAIR.component('CPYf_2').z + dCPY2_ITYAR + tITMY/nITMY, 'ITYHR_2'));

beam2shotASAIR.addComponent(component.dielectric(inf, -R_CPY, tCPY, nCPY, beam2shotASAIR.component('ITYHR_2').z + dCPY2_ITYAR, 'CPYb_2'));
beam2shotASAIR.addComponent(component.dielectric(inf, -R_BS, dBSHR_BSAR, nSRM, beam2shotASAIR.component('CPYb_2').z + dBSHR_CPY1, 'BSAR_3')); 
beam2shotASAIR.addComponent(component.curvedMirror(R_SR3, beam2shotASAIR.component('BSAR_3').z + dSR3_BSAR, 'SR3_3'));
beam2shotASAIR.addComponent(component.curvedMirror(R_SR2, beam2shotASAIR.component('SR3_3').z + dSR2_SR3 , 'SR2_3'));
beam2shotASAIR.addComponent(component.dielectric(-R_SRM, inf, tSRM, nSRM, beam2shotASAIR.component('SR2_3').z + dSRM_SR2, 'SRM_3'));
beam2shotASAIR.addComponent(component.dielectric(-R_OM1,inf,tOM1, nSRM,beam2shotASAIR.component('SRM_3').z + dSRM_OM1, 'OM_1'));
beam2shotASAIR.addComponent(component.lens(fL1,beam2shotASAIR.component('OM_1').z + dOM1_L1, 'L1_2'));
beam2shotASAIR.addComponent(component.lens(fL2,beam2shotASAIR.component('L1_2').z + dL1_ASAIR -2.5, 'L2_2'));

z2camera=beam2shotASAIR.component('L1_2').z+dL1_ASAIR;


beam2shotASAIR.qPropagate(z2camera).waistSize;

figure(102)
beam2shotASAIR.plotSummary(0:z2camera);


%% single shot through SR, to AS_AIR

beam1shotASAIR=beamPath;
beam1shotASAIR.seedq = beamq(-0.2325 + 13.3377i); % q at MC3 HR - path for single bounce X
beam1shotASAIR.seedz = 0;

beam1shotASAIR.addComponent(component.flatMirror(dWaist_SM1, 'SM1f'));
beam1shotASAIR.addComponent(component.curvedMirror(ROC_PMMT1*cos(7*pi/180), beam1shotASAIR.component('SM1f').z + dSM1_PMMT1_eff, 'PMMT1f'));
beam1shotASAIR.addComponent(component.curvedMirror(ROC_PMMT2*cos(7*pi/180), beam1shotASAIR.component('PMMT1f').z + dPMMT2_PMMT1_eff, 'PMMT2f'));
beam1shotASAIR.addComponent(component.flatMirror(beam1shotASAIR.component('PMMT2f').z + dSM2_PMMT2_eff, 'SM2f'));
beam1shotASAIR.addComponent(component.dielectric(inf, R_PRM, tPRM, nCPX, beam1shotASAIR.component('SM2f').z + dPRM_SM2_eff + tPRM/idxPRM, 'PRMHR'));
beam1shotASAIR.addComponent(component.curvedMirror(R_PR2,  beam1shotASAIR.component('PRMHR').z + dPRM_PR2, 'PR2'));
beam1shotASAIR.addComponent(component.curvedMirror(R_PR3, beam1shotASAIR.component('PR2').z + dPR2_PR3, 'PR3'));
beam1shotASAIR.addComponent(component.dielectric(R_BS, inf, dBSHR_BSAR, nSRM, beam1shotASAIR.component('PR3').z + dPR3_BSHR, 'BSHR'));
beam1shotASAIR.addComponent(component.dielectric(R_CPY, inf, tCPY, nCPY, beam1shotASAIR.component('BSHR').z + dBSHR_CPY1, 'CPYf'));
beam1shotASAIR.addComponent(component.curvedMirror(-R_ITMY/nITMY, beam1shotASAIR.component('CPYf').z + dCPY2_ITYAR + tITMY/nITMY, 'ITYHR'));
beam1shotASAIR.addComponent(component.dielectric(inf, -R_CPY, tCPY, nCPY, beam1shotASAIR.component('ITYHR').z + dCPY2_ITYAR, 'CPYb'));
beam1shotASAIR.addComponent(component.dielectric(inf, -R_BS, dBSHR_BSAR, nSRM, beam1shotASAIR.component('CPYb').z + dBSHR_CPY1, 'BSAR')); 
beam1shotASAIR.addComponent(component.curvedMirror(R_SR3, beam1shotASAIR.component('BSAR').z + dSR3_BSAR, 'SR3'));
beam1shotASAIR.addComponent(component.curvedMirror(R_SR2, beam1shotASAIR.component('SR3').z + dSR2_SR3 , 'SR2'));
beam1shotASAIR.addComponent(component.dielectric(-R_SRM, inf, tSRM, nSRM, beam1shotASAIR.component('SR2').z + dSRM_SR2, 'SRM'));
beam1shotASAIR.addComponent(component.dielectric(-R_OM1,inf,tOM1, nSRM,beam1shotASAIR.component('SRM').z + dSRM_OM1, 'OM1'));
beam1shotASAIR.addComponent(component.lens(fL1,beam1shotASAIR.component('OM1').z + dOM1_L1, 'L1'));
beam1shotASAIR.addComponent(component.lens(fL2,beam1shotASAIR.component('L1').z + dL1_ASAIR -2.5, 'L2'));

z1camera=beam1shotASAIR.component('L1').z+dL1_ASAIR;
beam1shotASAIR.qPropagate(z1camera)

figure(101)
beam1shotASAIR.plotSummary(0:z1camera);

z1srm=beam1shotASAIR.component('SRM').z;
beam1shotASAIR.qPropagate(z1srm)

figure(201)
beam1shotASAIR.plotSummary(beam1shotASAIR.component('BSHR').z:beam1shotASAIR.component('SRM').z);
[gPhase,qout]=beam1shotASAIR.gouyPhase(beam1shotASAIR.component('BSHR').z:beam1shotASAIR.component('SRM').z);
accumGouyPhase=gPhase(end)-gPhase(1)

figure(301)
beam2shotASAIR.plotSummary(beam2shotASAIR.component('BSHR').z:beam2shotASAIR.component('BSAR_2').z);
[gPhase,qout]=beam2shotASAIR.gouyPhase(beam1shotASAIR.component('BSHR').z:beam2shotASAIR.component('BSAR_2').z);
accumGouyPhase2=gPhase(end)-gPhase(1)
M=beam2shotASAIR.getTransferMatrix(beam2shotASAIR.component('BSHR').z,beam2shotASAIR.component('BSAR_2').z)
stabilityFactor=.5*(M(1,1)+M(2,2))

