
N1 = 400;
dx = 0.34/N1;
x = (-0.17+dx/2):dx:0.17;
y2 = x(:)*ones(1,N1);
x2 = y2';

% get IFO intensity
w0 = 0.054;
intIFO = exp(-2*((x2/w0).^2 + (y2/w0).^2));

% normalize CO2 intensity
wC = 9.5E-3*20.5;
pCO2 = 1E-1;
rmax = 5E-3*20.5;
rCO2 = sqrt(x2.^2 + y2.^2);
intCO2 = exp(-2*((x2/wC).^2 + (y2/wC).^2)).*(rCO2<rmax);
normCO2 = pCO2/sum(sum(intCO2*dx^2));


% get the scale right
n = 1.45;
eta = 0.17;
alpha = 0.55E-6;
dndT = 8.6E-6;
f = 85;
C1 = 772;
rho = 2202;
RIN = 0.05;
dzSc = pi/(2*450)*((n-1)*(1+eta)*alpha+dndT);
dzSc = dzSc/(2*pi*f*C1*rho)*RIN;

close all

OLList = [];

N2 = 34*2;
out1 = zeros(N2,N2);

xc = -0.17 + (1:N2)*0.34/N2;
yc = -0.17 + (1:N2)*0.34/N2;

for ii = 1:N2
    for jj = 1:N2

        
        x2a = x2 - xc(ii);
        y2a = y2 - yc(jj);

        rCO2 = sqrt(x2a.^2 + y2a.^2);
        intCO2 = normCO2*exp(-2*((x2a/wC).^2 + (y2a/wC).^2)).*(rCO2<rmax);

        OL = dzSc*sum(sum(intIFO.*intCO2*dx^2))/sum(sum(intIFO*dx^2));

        %OLList = [OLList; xc OL];
        out1(ii,jj) = OL;
    end 
    disp(ii)
    pause(0.01)
end

figure
colormap('hot')
h = subplot(1,1,1);
set(h, 'FontSize', 16)
contourf(xc*1E3, yc*1E3, out1, 25);
hold all
%contour(xc, yc, out1);
h = colorbar
set(h, 'FontSize', 16)
grid on
xlabel('Beam offset (mm)')
ylabel('Beam offset (mm)')
title('Displacement noise (m) for misaligned CO2 central')
axis square
orient landscape
print('-dpdf', [mfilename, '.pdf'])
