%**************************************************************************
%This function writes to a text file Bx, By, and Bz vs z along a line
%whose x and y positions are specified.  The closest line with calculated
%data points is used.
%
%Input:
%dat_file, data file for results
%field, structure containing the magnetic field information
%x_val_req, requested x position of line to plot B along (m)
%y_val_req, requested y position of line to plot B along (m)
%
%Zachary Wolf, 4/2/08

function ppm_und_dat_bxyz_vs_z(dat_file, field, x_val_req, y_val_req)

%Write the results to the data file
fid = fopen(dat_file, 'a');
[x_close, ix_close] = min(abs(field.pos(:, 1) - x_val_req));
x_val = field.pos(ix_close(1), 1);
[y_close, iy_close] = min(abs(field.pos(:, 2) - y_val_req));
y_val = field.pos(iy_close(1), 2);
index_array = find(((field.pos(:, 1) == x_val) & (field.pos(:, 2) == y_val)) == 1);
z = field.pos(index_array, 3);
bx = field.b(index_array, 1);
by = field.b(index_array, 2);
bz = field.b(index_array, 3);
fprintf(fid, '\r\n');
fprintf(fid, 'Simulated Bx, By, and Bz vs z:\r\n');
fprintf(fid, 'x = %f m\r\n', x_val);
fprintf(fid, 'y = %f m\r\n', y_val);
fprintf(fid, '     Z           Bx           By           Bz     \r\n');
fprintf(fid, '    (m)          (T)          (T)          (T)    \r\n');
fprintf(fid, '-----------  -----------  -----------  -----------\r\n');
fprintf(fid, '%11.6f  %11.6f  %11.6f  %11.6f\r\n', [z, bx, by, bz]');
fclose(fid);

%Done
