%**************************************************************************
%This function plots the magnetic field vector along a line
%whose x and y position is at the center of the permanent magnet blocks.
%
%Input:
%plt_file, plot file for results
%block, structure containing the block information
%charge, structure containing the magnetic charge information
%mag_field, structure containing the magnetic field information
%
%Zachary Wolf, 3/31/08

function ppm_plot_b_on_ctr_z_line(plt_file, block, charge, mag_field)

%Initialize
figure;
hold on;

%Draw the projection of the blocks
for ii = 1 : block.num_block
    line([block.z_center(ii) - block.z_width(ii) / 2, block.z_center(ii) + block.z_width(ii) / 2],...
        [block.y_center(ii) + block.y_width(ii) / 2, block.y_center(ii) + block.y_width(ii) / 2]);  %Top
    line([block.z_center(ii) - block.z_width(ii) / 2, block.z_center(ii) + block.z_width(ii) / 2],...
        [block.y_center(ii) - block.y_width(ii) / 2, block.y_center(ii) - block.y_width(ii) / 2]);  %Bottom
    line([block.z_center(ii) - block.z_width(ii) / 2, block.z_center(ii) - block.z_width(ii) / 2],...
        [block.y_center(ii) - block.y_width(ii) / 2, block.y_center(ii) + block.y_width(ii) / 2]);  %Left
    line([block.z_center(ii) + block.z_width(ii) / 2, block.z_center(ii) + block.z_width(ii) / 2],...
        [block.y_center(ii) - block.y_width(ii) / 2, block.y_center(ii) + block.y_width(ii) / 2]);  %Right
end

%Draw the projection of the charges
index_array = find(charge.q > 0);
plot(charge.z(index_array), charge.y(index_array), 'r+');
index_array = find(charge.q < 0);
plot(charge.z(index_array), charge.y(index_array), 'kx');

%Plot the field
[x_close, ix_close] = min(abs(mag_field.x - mean(block.x_center)));
x_val = mag_field.x(ix_close(1));
[y_close, iy_close] = min(abs(mag_field.y - mean(block.y_center)));
y_val = mag_field.y(iy_close(1));
index_array = find((mag_field.x == x_val) & (mag_field.y == y_val) == 1);
quiver(mag_field.z(index_array), mag_field.y(index_array), mag_field.bz(index_array), mag_field.by(index_array));

%Labels
title(['Magnetic Field On Center Line At X = ', num2str(x_val), ' m, Y = ', num2str(y_val), ' m']);
xlabel('Z Position (m)');
ylabel('Y Position (m)');

%Finalize
grid on;
zoom on;
hold off;

%Print
orient landscape;
print('-append', '-dpsc', plt_file);

%Done
