% ---------- Script for mode matching on ISCTEY ------------
% refer to DCC T1200200-v1 (only using nominal values)
% close all
% clear classes
addpath('/Users/alexastaley/Desktop/LIGO/ALS/svn/MatlabTools/');


% a convenient conversion parameter from inch to meter
inch = 0.0254;

savefigure = 0;
filename = ['ISCTEY.pdf'];  % from ISCTEY auxiliary laster (seed) to TMS seconday mirror (target)

% create a new beam path object
names = {'Horizontal', 'Vertical'};
for i = 1:length(names)
    GreenPath(i) = beamPath; 
    RedPath(i) = beamPath;
end

% define constants
green = 532e-9;
red   = 1064e-9;
Choose_Lambda = [green ; red];


%% Green path
lambda = Choose_Lambda(1);

% beam waist at z0 with ALS-FI3 as reference (measured with profilier)
% use BeamProfile532nmALSFI3.m data (linear regression for Horizontal(1) and Vertical(2) profile)
run('/Users/alexastaley/Documents/MATLAB/ISCTEY Data/BeamProfile532nmALSFI3.m')
w0_green(2) =  (lambda/pi) * 1/(p2(1) * 1e-4);
z0_green(2) =  -pi* w0_green(2) * (p2(2) * 1e-6) / (lambda);
w0_green(1) =  (lambda/pi) * 1/(p1(1) * 1e-4);
z0_green(1) =  -pi* w0_green(1) * (p1(2) * 1e-6) / (lambda);
% to account for laser move foward by 1 inch on 09/12/13
for i = 1:length(names);
    z0_green(i) = z0_green(i) + 1 * inch;
end

% Laser at Z = 0

% ALS-FI3 (refernce for beam profiler in BeamProfile532nmALSFI3.m)
z_ALS_FI3 = 20 * inch ; % 18in between FI3 and laser 
for i = 1:length(names);
    GreenPath(i).addComponent(component.flatMirror(z_ALS_FI3,'ALS-FI3'));
end

% define all mirrors, EOM, FI (all flat mirrors)
% ALS-M6
z_ALS_M6 = z_ALS_FI3 + 19 *inch; % 19in between ALSFI3 and M6 
% ALS-EOM NewFocus 4001NF aparture of 2 mm diameter
z_ALS_EOM = z_ALS_M6 + 4 *inch; % 4in between M6 and EOM 
% ALS-FI2
z_ALS_FI2 = z_ALS_M6 + 10 *inch; % 11in between ALSFI2 and M6 
% ALS-HWP4
z_ALS_HWP4 = z_ALS_FI2 - 2 * inch; % HWP4 is 2in before ALSFI2
% ALS-HWP5 
z_ALS_HWP5 = z_ALS_FI2 + 4 * inch; % HWP4 is 4in after ALSFI2
% ALS-M7
z_ALS_M7 = z_ALS_M6 + 26 * inch; % M7 is 26in from M6
% ALS-M8
z_ALS_M8 = z_ALS_M7 + 5 * inch; % M8 is 5in from M7
% ALS-M9
z_ALS_M9 = z_ALS_M8 + 10 * inch; % 9in between M8 and M9
% ALS-M10
z_ALS_M10 = z_ALS_M7 + 61 * inch; % 61 in between M7 ** and M10
% ALS-M11
z_ALS_M11 = z_ALS_M10 + 14 * inch; % 14 in between M10 and M11

for i = 1:length(names);
    GreenPath(i).addComponent(component.flatMirror(z_ALS_M6,'ALS-M6'));
    GreenPath(i).addComponent(component.flatMirror(z_ALS_M7,'ALS-M7'));
    GreenPath(i).addComponent(component.flatMirror(z_ALS_M8,'ALS-M8'));
    GreenPath(i).addComponent(component.flatMirror(z_ALS_M9,'ALS-M9'));
    GreenPath(i).addComponent(component.flatMirror(z_ALS_M10,'ALS-M10'));
    GreenPath(i).addComponent(component.flatMirror(z_ALS_M11,'ALS-M11'));
    
    GreenPath(i).addComponent(component.flatMirror(z_ALS_EOM,'ALS-EOM'));
    GreenPath(i).addComponent(component.flatMirror(z_ALS_FI2,'ALS-FI2'));
%     GreenPath(i).addComponent(component.flatMirror(z_ALS_HWP4,'ALS-HWP4'));
%     GreenPath(i).addComponent(component.flatMirror(z_ALS_HWP5,'ALS-HWP5'));
end

% define all lenses
n_silica = 1.46071; % refractive index of fused silicon lens for 532nm 
n_BK7 = 1.51947;  % refractive index of fused BK7 lens for 532nm
% ALS-L4
ROC_ALS_L4 = 154.5e-3; % Radius of L4 is 154.3mm (PLCX-25.4-154.3-C-532)
f_ALS_L4   =  ROC_ALS_L4/(n_BK7-1); % Focal length of L4
z_ALS_L4   = z_ALS_FI3 + 0.007 +4*inch; % from ALS-FI3 
% ALS-L5
ROC_ALS_L5 = 150e-3; % Radius of L5 is (check on table!!) 
f_ALS_L5   =  ROC_ALS_L5/(n_silica-1); % Focal length of L5
z_ALS_L5   = z_ALS_M6 + 22 *inch; % About 22in from ALS-M6

% ALS-L6
ROC_ALS_L6 = -100e-3; % Radius of L6 is plcc-25.4-103-C-532
f_ALS_L6   =  ROC_ALS_L6/(n_BK7-1); % Focal length of L6
z_ALS_L6   = z_ALS_M9 + 13.5 *inch; % About 14in from ALS-M9

% ALS-L7
ROC_ALS_L7 = 500e-3; % Radius of L6 is (check on table!!)
f_ALS_L7   =  ROC_ALS_L7/(n_silica-1); % Focal length of L7
z_ALS_L7   = z_ALS_M10 - 6 *inch; % About 40in from ALS-M9, 6 inch from M10

for i = 1:length(names);
    GreenPath(i).addComponent(component.lens(f_ALS_L4, z_ALS_L4, 'ALS-L4'));
    %GreenPath(i).addComponent(component.lens(f_ALS_L5, z_ALS_L5, 'ALS-L5'));
    %GreenPath(i).addComponent(component.lens(f_ALS_L6, z_ALS_L6, 'ALS-L6'));
    %GreenPath(i).addComponent(component.lens(f_ALS_L7, z_ALS_L7, 'ALS-L7'));
    % lens syntax:     (focal length,z position,string label)

end




% define seed waist
for i = 1:length(names);
    GreenPath(i).seedWaist(w0_green(i),z_ALS_FI3 + z0_green(i), lambda);
end
% seedWaist syntax:(waist width,z position, wavelength)

% define target waist
w_target = 2.2e-3;  
z_target = z_ALS_M11 + 3.5;
for i = 1:length(names);
    GreenPath(i).targetWaist(w_target,z_target, lambda);
    % targetWaist syntax:(waist width,z position)
end

% slide components to optimize mode overlap.
lb = z_ALS_M9 + 1*inch;
ub = z_ALS_M10;
for i = 1:length(names);
    %GreenPath(i) = GreenPath(i).optimizePath('ALS-L6',[lb 2.6],'ALS-L7',[2.6 ub]);
end
% optimizePath syntax           (component name,[(lower bound) (upper bound)],another component name,...)
% you can choose to optimize as many components as you would wish. Result is sensitive to initial conditions
% which are defined by the z position of the components before running optimize path.
% You can make it unbounded on either or both sides by using inf.
% if a component is not named, it will stay put.





%% data with ALS-L4 4inch from base of FI3
run('/Users/alexastaley/Documents/MATLAB/ISCTEY Data/BeamProfile532nmALSL4.m');
Distance_temp = Distance_temp .* 1e-2 + z_ALS_L4;
A1_temp = A1_temp .* 1e-6;
STDEV1_temp = STDEV1_temp .* 1e-6;
A2_temp = A2_temp .* 1e-6;
STDEV2_temp = STDEV2_temp .* 1e-6;
fit1_temp = fit1_temp .* 1e-6;
fit2_temp = fit2_temp .* 1e-6;


%% data after ALS-M9 without ALS-L5
run('/Users/alexastaley/Documents/MATLAB/ISCTEY Data/BeamProfile532nmALSM9.m');
for i = 1:length(names)
    Nano(i)=GreenPath(i).branchPath(0);
    % syntax: path2 = path1.branchPath(zlink), zlink = location of seed
    % beam in new path, doesn't need to be super accurate
end
Distance_M9_3 = Distance_M9_3 .* 1e-2 + z_ALS_M9;
Distance_HWP5 = [4 5].*inch + z_ALS_HWP5;
Distance_M9_3 = horzcat(Distance_HWP5, Distance_M9_3);
A1_M9_3 = A1_M9_3 .* 1e-6;
A2_M9_3 = A2_M9_3 .* 1e-6;
A1_HWP5 = [1030 1096].* 1e-6/2;% data in micro meters
A2_HWP5 = [892 937] .* 1e-6/2;
A1_M9_3 = horzcat(A1_HWP5, A1_M9_3);
A2_M9_3 = horzcat(A2_HWP5, A2_M9_3);
Nanox=Nano(1).fitBeamWidth(Distance_M9_3, A1_M9_3);
Nanoy=Nano(2).fitBeamWidth(Distance_M9_3, A2_M9_3);

Distance_HWP5_2 = [4 5].*inch + z_ALS_HWP5;
Distance_M9_5 = [-5 -4 2.5 6.5 10.5].*inch + z_ALS_M9;
Distance_comb = horzcat(Distance_HWP5_2, Distance_M9_5);
A1_comb = [1016 1096 2021 2107 2612 2926 3289].*1e-6/2;
A2_comb = [882 931 1544 1573 1913 2125 2364].*1e-6/2;
    
for i = 1:length(names)
    GreenPath(i).addComponent(component.lens(f_ALS_L5, z_ALS_L5, 'ALS-L5'));
end

%% data with ALS-M9 as ref with ALS-L5 22inch from M6
for i = 1:length(names)
    Nano_2(i)=GreenPath(i).branchPath(0);
    % syntax: path2 = path1.branchPath(zlink), zlink = location of seed
    % beam in new path, doesn't need to be super accurate
end

Distance_M9_4 = [-5 -4 2.5 6.5 10.5 14.5 18.5 22.5 28.5 31.5 36].*inch + z_ALS_M9;
A1_M9_4 = [1043 1018 939 887 856 834 823 816 801 807 855].*1e-6/2;
A2_M9_4 = [711 689 555 521 537 591 641 714 869 927 1042].*1e-6/2;
Nanox_2=Nano_2(1).fitBeamWidth(Distance_M9_4, A1_M9_4);
Nanoy_2=Nano_2(2).fitBeamWidth(Distance_M9_4, A2_M9_4);

Nanox.addComponent(component.lens(f_ALS_L5, z_ALS_L5, 'ALS-L5'));
Nanoy.addComponent(component.lens(f_ALS_L5, z_ALS_L5, 'ALS-L5'));

%% data after ALS-M9 with ALS-L5 22 inches from M6
Distance_M9 = Distance_M9 .* 1e-2 + z_ALS_M9;
A1_M9 = A1_M9 .* 1e-6;
A2_M9 = A2_M9 .* 1e-6;
for i = 1:length(names)
    Nanoscan(i)=GreenPath(i).branchPath(0);
    % syntax: path2 = path1.branchPath(zlink), zlink = location of seed
    % beam in new path, doesn't need to be super accurate
end
Nanoscanx=Nanoscan(1).fitBeamWidth(Distance_M9, A1_M9);
Nanoscany=Nanoscan(2).fitBeamWidth(Distance_M9, A2_M9);

%% Data collected via mode master with ALS-L5 in place with ALS-M9 as reference
for i = 1:length(names)
    MM(i)=GreenPath(i).branchPath(0);
    % syntax: path2 = path1.branchPath(zlink), zlink = location of seed
    % beam in new path, doesn't need to be super accurate
end
ModeMaster_2W0(1) = 0.811e-3;
ModeMaster_2W0(2) = 0.667e-3;
M9_to_ModeMaster = z_ALS_M9 + 30* inch; % zero position is M9
ModeMasterZ0(1) = -0.216;
ModeMasterZ0(2) = -0.442;
for i = 1:length(names)
    MM(i).seedWaist(ModeMaster_2W0(i)/2, M9_to_ModeMaster + ModeMasterZ0(i), lambda);
end

%% knife edge test
KnifeEdge_W0 = 3.7e-4; %in meters
z_KnifeEdge = z_ALS_M9 + 30*inch;



%% Telescope optimization
for i = 1:length(names)
    MM_2(i) = MM(i).branchPath(2.1);
end
for i = 1:length(names);
    GreenPath(i).addComponent(component.lens(f_ALS_L6, z_ALS_L6, 'ALS-L6'));
    GreenPath(i).addComponent(component.lens(f_ALS_L7, z_ALS_L7, 'ALS-L7'));
    MM_2(i).addComponent(component.lens(f_ALS_L6, z_ALS_L6, 'ALS-L6'));
    MM_2(i).addComponent(component.lens(f_ALS_L7, z_ALS_L7, 'ALS-L7'));
    % lens syntax:     (focal length,z position,string label)

end
Nanox.addComponent(component.lens(f_ALS_L6, z_ALS_L6, 'ALS-L6'));
Nanoy.addComponent(component.lens(f_ALS_L6, z_ALS_L6, 'ALS-L6'));
Nanox.addComponent(component.lens(f_ALS_L7, z_ALS_L7, 'ALS-L7'));
Nanoy.addComponent(component.lens(f_ALS_L7, z_ALS_L7, 'ALS-L7'));
Nanox_2.addComponent(component.lens(f_ALS_L6, z_ALS_L6, 'ALS-L6'));
Nanoy_2.addComponent(component.lens(f_ALS_L6, z_ALS_L6, 'ALS-L6'));
Nanox_2.addComponent(component.lens(f_ALS_L7, z_ALS_L7, 'ALS-L7'));
Nanoy_2.addComponent(component.lens(f_ALS_L7, z_ALS_L7, 'ALS-L7'));


% slide components to optimize mode overlap.
lb = z_ALS_M9 + 1*inch;
ub = z_ALS_M10 - 1*inch;
for i = 1:length(names);
    %MM_2(i) = MM_2(i).optimizePath('ALS-L6',[lb 2.6],'ALS-L7',[2.6 ub]);
end
% optimizePath syntax           (component name,[(lower bound) (upper bound)],another component name,...)
% you can choose to optimize as many components as you would wish. Result is sensitive to initial conditions
% which are defined by the z position of the components before running optimize path.
% You can make it unbounded on either or both sides by using inf.
% if a component is not named, it will stay put.

%Nanox_2 = Nanox_2.optimizePath('ALS-L6',[lb 2.6],'ALS-L7',[2.6 ub]);
%Nanoy_2 = Nanoy_2.optimizePath('ALS-L6',[lb 2.6],'ALS-L7',[2.6 ub]);

%% Data collected with MM after Telescope
% configuration: L6 is 14 inch from M9 and L7 is 40 inch from M9
%(see snap shots of MM in /Users/alexastaley/Desktop/LIGO/ISCTEY Results/TelescopeMM/)
MM_tele(1) = MM_2(1).branchPath(3);
MM_tele(2) = MM_2(2).branchPath(3);
 
z_MM = z_ALS_M10 + 1.346;
% waist size read off by MM (Wo), approx average of four runs
MM_tele_waist(1) = 3.237e-3/2;
MM_tele_waist(2) = 3.935e-3/2;
MM_tele_ZO(1) = 11.819;
MM_tele_ZO(2) = 0.517;
for i = 1:length(names) 
    MM_tele(i).seedWaist(MM_tele_waist(i), z_MM + MM_tele_ZO(i), lambda);
    MM_tele(i).addComponent(component.flatMirror(z_MM, 'MM'));
end

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

%% plot
figure(111)
clf;
% define plotting domain
zdomain = -.25:0.01:5;         % zdomain from ETM to small waist

hold on % right now all the plot commands act like the matlab plot command and will overwrite the existing
        % figure unless you turn hold on

% The plot commands actually plots two traces, the top and bottom of the beam.
% The output of the plot commands returns the plot handle of the top so when we make the legend we don't 
% have to put a label on the top and bottom of the beam.
gplot_h = GreenPath(1).plotBeamWidth(zdomain,'m');
gplot_v = GreenPath(2).plotBeamWidth(zdomain,'r');
MMplot_h = MM_2(1).plotBeamWidth(zdomain, 'y');
MMplot_v = MM_2(2).plotBeamWidth(zdomain, 'k');
MMteleplot_h = MM_tele(1).plotBeamWidth(zdomain, '--y');
MMteleplot_v = MM_tele(2).plotBeamWidth(zdomain, '--k');
% Nanoplot_h = Nanoscanx.plotBeamWidth(zdomain,'--y');
% Nanoplot_v = Nanoscany.plotBeamWidth(zdomain,'');
GreenPath(1).plotComponents(zdomain,0,'m*');
Nanoy_2.plotComponents(zdomain, 0, 'r*');
Nanox_2.plotComponents(zdomain, 0, 'b*');
dataplot1 = errorbar(Distance_temp, A1_temp, STDEV1_temp, 'd', 'Color', 'b');
dataplot2 = errorbar(Distance_temp, A2_temp, STDEV2_temp, 'd', 'Color', 'g');
%fitplot1 = plot(Distance_temp, fit1_temp, 'Color', 'b');
%fitplot2 = plot(Distance_temp, fit2_temp, 'Color', 'g');
dataplot3 = plot(Distance_M9_3, A1_M9_3,'o', 'Color', 'b');
dataplot4 = plot(Distance_M9_3, A2_M9_3,'o', 'Color', 'g');
%fitplot3 = Nanox.plotBeamWidth(zdomain,'--b');
%fitplot4 = Nanoy.plotBeamWidth(zdomain, '--g');
%dataplot5 = plot(Distance_M9, A1_M9,'o', 'Color', 'b');
%dataplot6 = plot(Distance_M9, A2_M9,'o', 'Color', 'g');
dataplot7 = plot(Distance_M9_4, A1_M9_4,'*', 'Color', 'b');
dataplot8 = plot(Distance_M9_4, A2_M9_4,'*', 'Color', 'g');
%dataplot9 = plot(Distance_comb, A1_comb,'*', 'Color', 'b');
%dataplot10 = plot(Distance_comb, A2_comb,'*', 'Color', 'g');
fitplot7 = Nanox_2.plotBeamWidth(zdomain,'-*b');
fitplot8 = Nanoy_2.plotBeamWidth(zdomain, '-*g');
% errorbar(Distance_FI3, A1_FI3, STDEV1_FI3, 'o', 'Color', 'b');
% errorbar(Distance_FI3, A2_FI3, STDEV2_FI3, 'o', 'Color', 'g');
axis tight;%([zdomain(1) zdomain(end) -0.07 0.07])
%set(gca, 'yscale', 'log');
plot(z_KnifeEdge, KnifeEdge_W0, '*', 'Color', 'k');
legend([gplot_h gplot_v, dataplot1, dataplot2, dataplot3, dataplot4, dataplot7, dataplot8, fitplot7, fitplot8, MMplot_h, MMplot_v, MMteleplot_h, MMteleplot_v],'horiz', 'vert', 'horz data', 'vert data', 'horz data', 'vert data','horz data', 'vert data', 'horz fit','vert fit','MM horz', 'MM vert', 'MM tele horz','MM tele vert'); % if we didn't use handles we would need to do 
                              %    legend('Y top','Y bottom','X top','X bottom') or something.
ylabel('Beam width (m)')
grid on
xlabel('axial distance along the arm (m)')
title('Mode matching (L4 located 4inch from FI3)');
set(gcf, 'color', 'w');


%% data with ALS-L4 at base of FI3
% moveleft = 4*inch;
% z_ALS_L4 = z_ALS_L4 - moveleft;
% for i = 1:length(names)
%     GreenPath(i).moveComponent('ALS-L4', -moveleft);
% end
% convert units to m and place in correct location
% Distance_Full = Distance_Full .* 1e-2 + z_ALS_L4;
% A1_Full = A1_Full .* 1e-6;
% STDEV1_Full = STDEV1_Full .* 1e-6;
% A2_Full = A2_Full .* 1e-6;
% STDEV2_Full = STDEV2_Full .* 1e-6;
% fit1_Full = fit1_Full .* 1e-6;
% fit2_Full = fit2_Full .* 1e-6;

% 
% %% plot
% 
% 
% figure(112)
% clf;
% % define plotting domain
% zdomain = -.5:0.01:6;         % zdomain from ETM to small waist
% 
% hold on % right now all the plot commands act like the matlab plot command and will overwrite the existing
%         % figure unless you turn hold on
% 
% % The plot commands actually plots two traces, the top and bottom of the beam.
% % The output of the plot commands returns the plot handle of the top so when we make the legend we don't 
% % have to put a label on the top and bottom of the beam.
% gplot_h = GreenPath(1).plotBeamWidth(zdomain,'m');
% gplot_v = GreenPath(2).plotBeamWidth(zdomain,'r');
% GreenPath(1).plotComponents(zdomain,0,'m*');
% % GreenPath(2).plotComponents(zdomain, 0, 'r*');
% dataplot1 = errorbar(Distance_Full, A1_Full, STDEV1_Full, 'o', 'Color', 'b');
% dataplot2 = errorbar(Distance_Full, A2_Full, STDEV2_Full, 'o', 'Color', 'g');
% fitplot1 = plot(Distance_Full, fit1_Full, 'Color', 'b');
% fitplot2 = plot(Distance_Full, fit2_Full, 'Color', 'g');
% % errorbar(Distance_FI3, A1_FI3, STDEV1_FI3, 'o', 'Color', 'b');
% % errorbar(Distance_FI3, A2_FI3, STDEV2_FI3, 'o', 'Color', 'g');
% axis tight;%([zdomain(1) zdomain(end) -0.07 0.07])
% %set(gca, 'yscale', 'log');
% 
% legend([gplot_h gplot_v, dataplot1, dataplot2, fitplot1, fitplot2],'horiz', 'vert', 'horz data', 'vert data', 'horz fit', 'vert fit'); % if we didn't use handles we would need to do 
%                               %    legend('Y top','Y bottom','X top','X bottom') or something.
% ylabel('Beam width (m)')
% grid on
% xlabel('axial distance along the arm (m)')
% title('Mode matching (L4 located at base of FI3)');
% set(gcf, 'color', 'w');

%% Red path
lambda = Choose_Lambda(2);

% beam waist at z0 w/ ALS-M1 as reference
% use BeamProfile1064nm.m data (linear regression for Horizontal(1) and Vertical(2) profile)
run('/Users/alexastaley/Documents/MATLAB/ISCTEY Data/BeamProfile1064nmALSM1.m');
w0_red(1) =  (lambda/pi) * 1/(p1(1) * 1e-4);
z0_red(1) =  -pi* w0_red(1) * (p1(2) * 1e-6) / (lambda);
w0_red(2) =  (lambda/pi) * 1/(p2(1) * 1e-4);
z0_red(2) = -pi* w0_red(2) * (p2(2) * 1e-6) / (lambda);
% to account for laser move foward by 1 inch on 09/12/13
for i = 1:length(names);
    z0_red(i) = z0_red(i) + 1 * inch;
end

% ALS-FI1
z_ALS_FI1 = 8 *inch; % about 8 inch from laser
% ALS-M1 (after FI1)
z_ALS_M1 = 22*inch; % about 23in from laser
% ALS-PBS1
z_ALS_PBS1 = z_ALS_M1 + 17 * inch; % about 17inch from M1
% ALS-BS3
z_ALS_BS3 = z_ALS_PBS1 + 16*inch; % about 16inch from PBS1
% ALS-M3
z_ALS_M3 = z_ALS_PBS1 + 25*inch; % about 25inch form PBS1
% ALS-PD7
z_ALS_PD7 = z_ALS_PBS1 + 30*inch; % about 30inch from PBS1
for i = 1:length(names)
    RedPath(i).addComponent(component.flatMirror(z_ALS_FI1,'ALS-FI1'));
    RedPath(i).addComponent(component.flatMirror(z_ALS_M1,'ALS-M1'));
    RedPath(i).addComponent(component.flatMirror(z_ALS_PBS1,'ALS-PBS1'));
    RedPath(i).addComponent(component.flatMirror(z_ALS_BS3,'ALS-BS3'));
    RedPath(i).addComponent(component.flatMirror(z_ALS_M3,'ALS-M3'));
    RedPath(i).addComponent(component.flatMirror(z_ALS_PD7,'ALS-PD7'));

end

% define all lenses
n_silica = 1.44963; % refractive index of fused silicon lens for 1064nm 
% ALS-L1
ROC_ALS_L1 = 75e-3; % Radius of L1 is 50mm (09/18/13 -- swapped L1 and L3)
f_ALS_L1   =  ROC_ALS_L1/(n_silica-1); % Focal length of L1
z_ALS_L1   = z_ALS_PBS1 + 9*inch; %3 inch from ALS-PBS1 (see BeamProfile1064nmALSBS3 for details)
% ALS-L3
ROC_ALS_L3 = 50e-3; % Radius of L3 is 75mm (09/18/13 -- swapped L1 and L3)
f_ALS_L3   =  ROC_ALS_L3/(n_silica-1); % Focal length of L3
z_ALS_L3   = z_ALS_PBS1 + 23*inch; %21inch from ALS-PBS1


for i = 1:length(names);
    RedPath(i).addComponent(component.lens(f_ALS_L1, z_ALS_L1, 'ALS-L1'));
    RedPath(i).addComponent(component.lens(f_ALS_L3, z_ALS_L3, 'ALS-L3'));
    % lens syntax:     (focal length,z position,string label)

end

% define seed waist
for i = 1:length(names);
    RedPath(i).seedWaist(w0_red(i),z_ALS_M1 + z0_red(i), lambda);
end
% seedWaist syntax:(waist width,z position, wavelength)

% define target waist
run('/Users/alexastaley/Documents/MATLAB/ISCTEY Data/BeamProfile1064nmALSBS3.m');
w_target = fiber_1064.seedq.waistSize;  
z_target = fiber_1064.seedq.waistZ + z_ALS_BS3;
for i = 1:length(names);
    RedPath(i).targetWaist(w_target,z_target, lambda);
    % targetWaist syntax:(waist width,z position)
end

% slide components to optimize mode overlap.
lb_1 = z_ALS_PBS1;
ub_1 = z_ALS_BS3;
lb_2 = z_ALS_BS3;
ub_2 = z_ALS_M3;
for i = 1:length(names);
    RedPath(i) = RedPath(i).optimizePath('ALS-L1',[lb_1 ub_1]);
end

%% plot
figure(113)
clf;
% define plotting domain
zdomain = -.5:0.01:2.5;         % zdomain from ETM to small waist

hold on % right now all the plot commands act like the matlab plot command and will overwrite the existing
        % figure unless you turn hold on

% The plot commands actually plots two traces, the top and bottom of the beam.
% The output of the plot commands returns the plot handle of the top so when we make the legend we don't 
% have to put a label on the top and bottom of the beam.
gplot_h = RedPath(1).plotBeamWidth(zdomain,'m');
gplot_v = RedPath(2).plotBeamWidth(zdomain,'r');
RedPath(1).plotComponents(zdomain,0,'m*');
% GreenPath(2).plotComponents(zdomain, 0, 'r*');
axis tight;%([zdomain(1) zdomain(end) -0.07 0.07])
%set(gca, 'yscale', 'log');

legend([gplot_h gplot_v],'horiz', 'vert'); % if we didn't use handles we would need to do 
                              %    legend('Y top','Y bottom','X top','X bottom') or something.
ylabel('Beam width (m)')
grid on
xlabel('axial distance along the arm (m)')
title('Mode matching of 1064nm');
set(gcf, 'color', 'w');
%% Figure Saving
if savefigure
    saveas(gcf, filename, 'pdf');
end;
