
Bmap = read2Dfieldmap( 'xleap2-wiggler-data/xLEAP-II-1/' );

[nz,nd] = size(Bmap);
zs = Bmap(1:nz/11,1);

% plot vs z

[Bx,By]= getfield(zs, -0.999e-3, Bmap,0); 
figure;
plot(zs,Bx);hold on; plot(zs,By,'color','red');hold off;
xlabel('z position (m)'); ylabel('B field (T)');
legend('Bx','By'); title('x = -1 mm')

[Bx,By]= getfield(zs, 0.00, Bmap,0); 
figure;
plot(zs,Bx);hold on; plot(zs,By,'color','red');hold off;
xlabel('z position (m)'); ylabel('B field (T)');
legend('Bx','By'); title('x = 0 mm')

[Bx,By]= getfield(zs, 0.999e-3, Bmap,0); 
figure;
plot(zs,Bx);hold on; plot(zs,By,'color','red');hold off;
xlabel('z position (m)'); ylabel('B field (T)');
legend('Bx','By'); title('x = +1 mm')

% plot vs x

Bxs = []; Bys = [];

myz = -0.275;
xs = 0.999e-3 * linspace(-1,1,21);

for x = xs
    [Bx,By] = getfield(myz, x, Bmap,0);
    Bxs = [Bxs, Bx];
    Bys = [Bys, By];
end

figure;
plot(1e3*xs,Bxs);
xlabel('x position (mm)'); ylabel('Bx field (T)');
title(['z = ' num2str(myz) ' m'])

figure;
plot(1e3*xs,Bys,'color','red');
xlabel('x position (mm)'); ylabel('By field (T)');
title(['z = ' num2str(myz) ' m'])

figure;
plot(1e3*xs,Bys/max(Bys),'color','red'); hold on;
fieldrolloff = 1. - 0.003 * ((xs+0.000075) / 0.001).^2;
plot(1e3*xs,fieldrolloff,'-.','color','black'); %hold off;
fieldrolloff = 1. - 0.00275 * ((xs+0.000075) / 0.001).^2;
plot(1e3*xs,fieldrolloff,'color','black'); hold off;
xlabel('x position (mm)'); ylabel('relative By field');
legend('By/max(By)','alpha=0.003','alpha=0.00275'); title(['z = ' num2str(myz) ' m'])



% plot vs x

Bxs = []; Bys = [];

myz = -1.377;
xs = 0.999e-3 * linspace(-1,1,21);

for x = xs
    [Bx,By] = getfield(myz, x, Bmap,0);
    Bxs = [Bxs, Bx];
    Bys = [Bys, By];
end

figure;
plot(1e3*xs,Bxs);
xlabel('x position (mm)'); ylabel('Bx field (T)');
title(['z = ' num2str(myz) ' m'])

figure;
plot(1e3*xs,Bys,'color','red');
xlabel('x position (mm)'); ylabel('By field (T)');
title(['z = ' num2str(myz) ' m'])

figure; xcenter = 0.2e-3;
plot(1e3*xs,Bys/max(Bys),'color','red'); hold on;
fieldrolloff = 1. - 0.003 * ((xs-xcenter) / 0.001).^2;
plot(1e3*xs,fieldrolloff,'-.','color','black'); %hold off;
fieldrolloff = 1. - 0.00275 * ((xs-xcenter) / 0.001).^2;
plot(1e3*xs,fieldrolloff,'color','black'); hold off;
xlabel('x position (mm)'); ylabel('relative By field');
legend('By/max(By)','alpha=0.003','alpha=0.00275'); title(['z = ' num2str(myz) ' m'])

