%**************************************************************************
%This program reads magnetic field measurements.  For instance, it could
%read fluxgate measurements of the Earth's magnetic field.
%The program calculates first and second field integrals.
%The results can be used for corrections to long coil measurements,
%or to compare to long coil measurements.
%
%Zachary Wolf
%9/11/08

%Clear the workspace
clear

%Parameters
coil_length = 1.2;  %Coil length (m)

%Get the field measurements
load fieldmeas.dat;
point_num = fieldmeas(:, 1)';
x = fieldmeas(:, 2)';
y = fieldmeas(:, 3)';
z = fieldmeas(:, 4)';
bx = fieldmeas(:, 5)';
by = fieldmeas(:, 6)';
bz = fieldmeas(:, 7)';

%Correct for the direction of the fluxgate
bx = -bx;
by = -by;

%Plot the fluxgate measurements
figure;
hold on;
plot(z, bx, 'b*');
plot(z, by, 'r*');
plot(z, bz, 'k*');
title('MMF Dover Bench Earth Field Measurements');
xlabel('Z Position (m)');
ylabel('Bx, By, Bz (T)');
legend('Bx', 'By', 'Bz');
grid on;
zoom on;
hold off;

%Calculate the first and second field integrals
weight = (coil_length - z);
bx_weight = bx .* weight;
by_weight = by .* weight;
I1x = trapz(z, bx);
I2x = trapz(z, bx_weight);
I1y = trapz(z, by);
I2y = trapz(z, by_weight);

%Display the field integrals
I1x
I2x
I1y
I2y

%Save all results
save 'fieldmeas.mat';

%Done
