%**************************************************************************
%This function plots the magnetic field in the x-y plane whose z position
%is specified.  The closest z position with data points is used.
%
%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
%z_val_req, requested z position of slice (m)
%
%Zachary Wolf, 3/31/08

function pm_plot_b_on_z_slice(plt_file, block, charge, mag_field, z_val_req)

%Initialize
figure;
hold on;

%Draw the projection of the blocks
for ii = 1 : block.num_block
    line([block.x_center(ii) - block.x_width(ii) / 2, block.x_center(ii) + block.x_width(ii) / 2],...
        [block.y_center(ii) + block.y_width(ii) / 2, block.y_center(ii) + block.y_width(ii) / 2]);  %Top
    line([block.x_center(ii) - block.x_width(ii) / 2, block.x_center(ii) + block.x_width(ii) / 2],...
        [block.y_center(ii) - block.y_width(ii) / 2, block.y_center(ii) - block.y_width(ii) / 2]);  %Bottom
    line([block.x_center(ii) - block.x_width(ii) / 2, block.x_center(ii) - block.x_width(ii) / 2],...
        [block.y_center(ii) - block.y_width(ii) / 2, block.y_center(ii) + block.y_width(ii) / 2]);  %Left
    line([block.x_center(ii) + block.x_width(ii) / 2, block.x_center(ii) + block.x_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.x(index_array), charge.y(index_array), 'r+');
index_array = find(charge.q < 0);
plot(charge.x(index_array), charge.y(index_array), 'kx');

%Plot the field
[z_close, iz_close] = min(abs(mag_field.z - z_val_req));
z_val = mag_field.x(iz_close(1));
index_array = find(mag_field.z == z_val);
quiver(mag_field.x(index_array), mag_field.y(index_array), mag_field.bx(index_array), mag_field.by(index_array));

%Labels
title(['Magnetic Field On Slice At Z = ', num2str(z_val), ' m']);
xlabel('X Position (m)');
ylabel('Y Position (m)');

%Finalize
grid on;
zoom on;
hold off;

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

%Done
