function [overlap, optimized_overlap, SR2_move] = calcMirrorPosTolerance(pos_variance, ROC_variance)
% D. Hoak, based on a script by Lisa Barsotti, see LLO alog 8565
%
% varyMirrorPositions - calculates the dependence of mode matching into the OMC on the position
%                       of optics in the SRC and HAM6 telescope
%
% Inputs:
% 
% pos_variance is a set of six random translations to optical element positions (SR3, SR2, SRM, OM1, OM2, OMC)
%
% ROC_variance is a set of five random variations to optical radii of curvature (SR3, SR2, SRM, OM1, OM2)
%
%
% Note that this function loads the H1 as-built optical parameters, make sure these are correct!
%
% Also we use the latest version of a la mode (May 2014).  Some component declarations have changed:
%
% beamq(beamQZ()) has become beamq.beamWaistAndR()
% beamq.thickLens has become beamq.dielectric
%
% Earlier versions of a la mode may throw errors.
%

% Start: load optical parameters (mirror position, radius of curvature, etc)

H1_AsbuiltParameters;

% Use the red beam
lambda = 1.064e-6;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% OMC output Telescope parameters

% as-built L1 HAM6 parameters
%dSRM_OM1 = 3.646;    
%dOM1_OM2 = 1.395;                  
%dOM2_OM3 = 0.708; 

% preliminary H1 HAM6 parameters
dSRM_OM1 = 3.5710;  % does this match as-built?    
dOM1_OM2 = 1.395;                  
dOM2_OM3 = 0.708; 
dOM3_OMCw = 0.262;

R_OM1 = 4.6;
R_OM2 = 1.7;
nOM1 = 1.45;
tOM1 = 3/8 * 25.4 * 1e-3; % OM1 thickness


% Optical length PR3 --> ITMX AR
dPR3_ITXAR = dPR3_BSHR + dBSHR_BSAR * 1.45 + dBSAR_CPX1 + dCPX1_CPX2 *1.45 + dCPX2_ITXAR;
dPRM_ITXAR = dPRM_PR2 + dPR2_PR3 + dPR3_ITXAR;
dPRM_BSHR = dPRM_PR2 + dPR2_PR3 + dPR3_BSHR;

% Optical length BS --> ITMY HR / ITX HR
dBSHR_ITYHR = dBSHR_CPY1 + dCPY1_CPY2 * 1.45 + dCPY2_ITYAR + dTYAR_ITYHR * 1.45;
dBSHR_ITXHR =  dBSHR_BSAR * 1.45 + dBSAR_CPX1 + dCPX1_CPX2 * 1.45 + dCPX2_ITXAR + dITXAR_ITXHR * 1.45;

% Optical length SR3 --> ITMY AR
dSR3_BSHR = dSR3_BSAR + dBSAR_BSHR * 1.45;
dSR3_ITYAR = dSR3_BSHR + dBSHR_CPY1 + dCPY1_CPY2 *1.45 + dCPY2_ITYAR;

% Cross check optical length of signal recycling cavities
PRC = dPRM_PR2 + dPR2_PR3 + dPR3_BSHR + (dBSHR_ITXHR + dBSHR_ITYHR) / 2;
SRC = dSRM_SR2 + dSR2_SR3 + dSR3_BSHR + (dBSHR_ITXHR + dBSHR_ITYHR) / 2;

Schnupp = dBSHR_ITXHR - dBSHR_ITYHR;

% Build beam path to AS port
% Starting positions are initialized using distance to previous optical component
% ROCs are varied as they are initialized
% After this is finished the positions are varied according to our estimated errors
% This is an awkward way to do things but it allows us to use the same parameter file for everything

asX = beamPath;

% Seed beam at PRM
%asX.seedq = beamq(beamQZ(-R_PRM, w_PRM, lambda));
asX.seedq = beamq.beamWaistAndR(w_PRM, -R_PRM, lambda);
asX.seedz = 0;

% X - Path from PRM to ITX HR
asX.addComponent(component.curvedMirror(R_PR2, dPRM_PR2, 'PR2'));
asX.addComponent(component.curvedMirror(R_PR3, asX.component('PR2').z + dPR2_PR3, 'PR3'));
asX.addComponent(component.dielectric(R_BS, inf, dBSHR_BSAR, nSRM, asX.component('PR3').z + dPR3_BSHR, 'BSHR'));

% X - Path from BSHR to ITXHR   
asX.addComponent(component.dielectric(R_BS, inf, tCPX, nCPX, asX.component('BSHR').z + dBSAR_CPX1, 'CPXf'));
asX.addComponent(component.curvedMirror(-R_ITMX/nITMX, asX.component('CPXf').z + dCPX2_ITXAR + tITMX/nITMX, 'ITXHR'));

% X - Path back from ITXHR to BS HR
asX.addComponent(component.dielectric(inf, inf, 100e-3, nCPX, asX.component('ITXHR').z + dCPX2_ITXAR, 'CPXb'));
asX.addComponent(component.dielectric(inf, -R_BS, 2*dBSHR_BSAR, nSRM, asX.component('CPXb').z + dBSAR_CPX1, 'BSAR'));

% X - Path back from BS AR to SRM
asX.addComponent(component.curvedMirror(R_SR3 + ROC_variance(1), asX.component('BSAR').z + dSR3_BSAR, 'SR3'));
asX.addComponent(component.curvedMirror(R_SR2 + ROC_variance(2), asX.component('SR3').z + dSR2_SR3, 'SR2'));
asX.addComponent(component.dielectric(-R_SRM + ROC_variance(3), inf, tSRM, nSRM, asX.component('SR2').z + dSRM_SR2, 'SRM'));

asAIR = asX.duplicate;  % make a copy to calculate beam profile on ISCT6

% OutputTelescope - X path
asX.addComponent(component.curvedMirror(R_OM1 + ROC_variance(4), asX.component('SRM').z + dSRM_OM1-tSRM, 'OM1'));
asX.addComponent(component.curvedMirror(R_OM2 + ROC_variance(5), asX.component('OM1').z + dOM1_OM2, 'OM2'));
asX.addComponent(component.flatMirror(asX.component('OM2').z + dOM2_OM3, 'OM3'));
asX.addComponent(component.flatMirror(asX.component('OM3').z + dOM3_OMCw, 'OMC waist'));

% adjust the position of the each component

asX.component('SR3').z = asX.component('SR3').z + pos_variance(1);
asX.component('SR2').z = asX.component('SR2').z + pos_variance(2);
asX.component('SRM').z = asX.component('SRM').z + pos_variance(3);
asX.component('OM1').z = asX.component('OM1').z + pos_variance(4);
asX.component('OM2').z = asX.component('OM2').z + pos_variance(5);

% Set the target.  Vary the position at which the waist measurement occurs.  This simulates error in the location of the OMC.
asX.targetz = asX.component('OMC waist').z + pos_variance(6); % this is the position of the waist
asX.targetq = beamq.beamWaistAndZ(0.490e-3, 0.0);

overlap = asX.targetOverlap;

SR2_start = asX.component('SR2').z;

asX = asX.optimizePath('SR2', asX.component('SR2').z + [-0.07 0.07]);

SR2_end = asX.component('SR2').z;

SR2_move = SR2_start - SR2_end;

optimized_overlap = asX.targetOverlap;


return
