addpath C:\Users\sheila.dwyer\Documents\MATLAB\alm-master
close all
clear classes

lambda = 532e-9;

%start by fitting measured beam headed towards fiber port


%I will use distances from the steering mirror as the coordinates.  
%Nano scan rail was placed so that with slider at 16cm mark, front of
%scanhead was 265mm from steering mirror, ticks on rail go down further from steering mirror.  
zScan = (16e-2-[16, 13, 10, 7, 4, 12]*1e-2)+265e-3; % hole spacing is 2 inches
xWidth = [361, 76, 471, 757, 1105, 180] * 1e-6/2;
yWidth = [452, 85, 698, 700, 1071, 158] * 1e-6/2;

goo = beamPath;  %
goo.seedWaist(50e-6, 300e-3, lambda)
%ROC+25mm is 2 inches before steering mirror
goo.addComponent(component.flatMirror(0,'steering mirror'))
gooh = goo.fitBeamWidth(zScan, xWidth);  %
goov = goo.fitBeamWidth(zScan, yWidth);  %

%now propagate back through lenses
back_h = gooh.branchPath(0.3);
back_v = goov.branchPath(0.3);
back_h.addComponent(component.lens(0.111, -2*25.4e-3,'ROC +50mm'))
back_v.addComponent(component.lens(0.111, -2*25.4e-3,'ROC +50mm'))
back_h.addComponent(component.lens(0.056, 0.203,'ROC +25mm'))
back_v.addComponent(component.lens(0.056, 0.203,'ROC +25mm'))

%fitting of beam after adjusting fiber port to collimate
fScan = (61-[61,51,41,31,21,11])*1e-2+2.25*25.4e-3; % 61cm on nano scan rail puts nanoscan head 2 1/4" from fiber port front plate
fxWidth = [424, 393, 417, 470, 594, 747] * 1e-6/2;
fyWidth = [416, 381, 393, 471, 608, 791] * 1e-6/2;
fiber = beamPath;
fiber.seedWaist(182e-6, 0.2, lambda);
fiber_h = fiber.fitBeamWidth(fScan, fxWidth);
fiber_v = fiber.fitBeamWidth(fScan, fyWidth);

%make a beam which is the beam coming towards fiber port with no lenses in
trial_h = back_h.branchPath(-0.2);
trial_v = back_v.branchPath(-0.2);
%remove lenses from these paths
trial_h.deleteComponent('ROC +50mm');
trial_h.deleteComponent('ROC +25mm');
trial_v.deleteComponent('ROC +50mm');
trial_v.deleteComponent('ROC +25mm');

%make the target waist for the trial path match the measured waist coming
%out of the fiber, which is for horizontal 182um at 0.194m from fiber port
%fiber port is 13" from steering mirror, so target waist should be
%13"-0.194m from steering mirror
trial_h.targetWaist(182e-6, 13*25.4e-3-fiber_h.getWaists, lambda);

trial_h.addComponent(component.lens(0.556,-2*25.4e-3,'ROC +250'))
%there aren't many places to put a lens, so try a larger lens in the
%location of current 
disp(trial_h.targetOverlap)
%make a plot to compare data and fit of beam going into fiber, show back
%propagated beam in red
zdomain = -.2:.01:0.5;
figure(1)
goo;
hold on
gooh.plotBeamWidth(zdomain,'b')
goov.plotWaists(zdomain)
goov.plotBeamWidth(zdomain, 'k')
plot(zScan,xWidth,'bo')
plot(zScan,yWidth,'kx')
back_h.plotBeamWidth(zdomain,'r')
trial_h.plotBeamWidth(zdomain, 'g')
xlabel('distance from steering mirror [m]')
ylabel('beam radius [m]')
title('blue and black - measured, red - back propagated, green - new lens')
saveas(gcf, 'FCGC_input_beam.png')

zdomain = 0:.01:0.7;
figure(2)
fiber;
hold on
fiber_h.plotBeamWidth(zdomain,'b')
fiber_v.plotWaists(zdomain)
fiber_v.plotBeamWidth(zdomain, 'r')
plot(fScan,fxWidth,'bo')
plot(fScan,fyWidth,'rx')
xlabel('distance from fiber port [m]')
ylabel('beam radius [m]')
%title(strcat('L1 only horiztonal overlap ',num2str(measL1h.targetOverlap*100,3),...
%    '%, vertical overlap ', num2str(measL1v.targetOverlap*100, 3), '%'))
saveas(gcf, 'FCGC_fiber_mode.png')

figure(3)
trial_h;
hold on
trial_h.plotBeamWidth(zdomain, 'b')