addpath('/Users/alexastaley/Documents/MATLAB/alm');
% green path on ISCTEX
lambda = 532e-9;
% unit conversion
inch_to_meter = 0.0254;


% create a new beam path object
names = {'X', 'Y'};
for i = 1:length(names)
    OAT(i) = beamPath; 
    ISCTEX(i) = beamPath;
end

%% OAT configuration
% data collected with OAT configuration w/ ROC=-100mm removed
% used Silicon MM detector at low divergence
ModeMaster_2W0(1) = 1.439e-3;
ModeMaster_2W0(2) = 1.527e-3;
M11_to_ModeMaster = 28.5 * inch_to_meter; % zero position is M11
ModeMasterZ0(1) = -1.348;
ModeMasterZ0(2) = -0.827;
for i = 1:length(names)
    OAT(i).seedWaist(ModeMaster_2W0(i)/2, M11_to_ModeMaster + ModeMasterZ0(i), lambda);
end

% Mirror locations
z_M11 = 0;
z_M10 = -.36;
z_M9 = z_M10 - 1.47;
for i = 1:length(names)
    OAT(i).addComponent(component.flatMirror(z_M9, 'ALS M9'));
    OAT(i).addComponent(component.flatMirror(z_M11, 'ALS M11'));
    OAT(i).addComponent(component.flatMirror(z_M10, 'ALS M10'));
    OAT(i).addComponent(component.flatMirror(M11_to_ModeMaster, 'MM'));
end

% index of refraction for Fused Silica
n = 1.46071;
% ALS L7
ROC_L7 = 500e-3;
f_L7 = ROC_L7/(n-1);
z_L7 = -32 * inch_to_meter; % from M11
for i = 1:length(names)
    OAT(i).addComponent(component.lens(f_L7, z_L7, 'ALS L7'));
end

%% Nominal Configuration
for i = 1:length(names)
    ISCTEX(i)=OAT(i).branchPath(-5);
end

% Move ALS L7
z_L7 = z_M10 - 7*inch_to_meter;
for i = 1:length(names)
    ISCTEX(i).moveComponent('ALS L7', z_L7, 'absolute');
end

% Move MM
M11_to_ModeMaster = 660e-3; 
for i = 1:length(names)
    ISCTEX(i).moveComponent('MM', M11_to_ModeMaster, 'absolute');
end

% ALS L6
ROC_L6 = -100e-3;
f_L6 = ROC_L6/(n-1);
z_L6 = z_L7 - 37 * inch_to_meter; 
for i = 1:length(names)
    ISCTEX(i).addComponent(component.lens(f_L6, z_L6, 'ALS L6'));
end

% target waist
z_target = 184.6 * inch_to_meter;
waist_target = 2.2e-3;
for i = 1:length(names)
    ISCTEX(i).targetWaist(waist_target, z_target, lambda);
end

for i = 1:length(names)
    ISCTEX(i).targetOverlap
end

zz=-3:0.01:1;
figure(1)
clf;
hold on;
x_OAT = OAT(1).plotBeamWidth(zz, 'r');
y_OAT = OAT(2).plotBeamWidth(zz, 'b');
OAT(1).plotComponents(zz, 'r*');
legend([x_OAT y_OAT], 'x OAT', 'y OAT');
ylabel('Beam width (m)')
grid on
xlabel('distance along the path (m)')

figure(2)
clf;
hold on;
x_ISCTEX = ISCTEX(1).plotBeamWidth(zz, 'r');
y_ISCTEX = ISCTEX(2).plotBeamWidth(zz, 'b');
ISCTEX(1).plotComponents(zz, 'r*');
legend([x_ISCTEX y_ISCTEX], 'x', 'y');
ylabel('Beam width (m)')
grid on
xlabel('distance along the path (m)')


%% Mode Master
for i = 1:length(names)
    MM(i) = beamPath;
end

z_MM = 0;
% waist size at MM, approx avegere of four runs
MM_waist(1) = 4.5e-3/2;
MM_waist(2) = 5.4e-3/2;
for i = 1:length(names)
    MM(i).seedWaist(MM_waist(i), z_MM, lambda);
    MM(i).addComponent(component.flatMirror(z_MM, 'MM'));
end


% target waist
z_target = 184.6 * inch_to_meter;
waist_target = 2.2e-3;
for i = 1:length(names)
    MM(i).targetWaist(waist_target, z_target, lambda);
end

for i = 1:length(names)
    MM(i).targetOverlap
end


figure(3)
clf;
hold on;
x_MM = MM(1).plotBeamWidth(zz, 'r');
y_MM = MM(2).plotBeamWidth(zz, 'b');
MM(1).plotComponents(zz, 'r*');
legend([x_MM y_MM], 'x MM', 'y MM');
ylabel('Beam width (m)')
grid on
xlabel('distance along the path (m)')