%**************************************************************************
%This function plots the z component of the magnetic field as a function
%of y along a line whose x and z positions are specified.
%The closest x and z positions with data points are used.
%
%Input:
%plt_file, plot file for results
%block, structure containing the block information
%charge, structure containing the magnetic charge information
%field, structure containing the magnetic field information
%x_val_req, requested x position of line (m)
%z_val_req, requested z position of line (m)
%
%Zachary Wolf, 4/22/10

function pm_block_rect_plot_bz_vs_y(plt_file, block, charge, field, x_val_req, z_val_req)

%Initialize
figure;
hold on;

%Mark the block positions
for ii = 1 : length(block)
    p1 = [- block(ii).dim(1) / 2,   block(ii).dim(2) / 2,   block(ii).dim(3) / 2];
    p2 = [  block(ii).dim(1) / 2,   block(ii).dim(2) / 2,   block(ii).dim(3) / 2];
    p3 = [- block(ii).dim(1) / 2, - block(ii).dim(2) / 2,   block(ii).dim(3) / 2];
    p4 = [  block(ii).dim(1) / 2, - block(ii).dim(2) / 2,   block(ii).dim(3) / 2];
    p5 = [- block(ii).dim(1) / 2,   block(ii).dim(2) / 2, - block(ii).dim(3) / 2];
    p6 = [  block(ii).dim(1) / 2,   block(ii).dim(2) / 2, - block(ii).dim(3) / 2];
    p7 = [- block(ii).dim(1) / 2, - block(ii).dim(2) / 2, - block(ii).dim(3) / 2];
    p8 = [  block(ii).dim(1) / 2, - block(ii).dim(2) / 2, - block(ii).dim(3) / 2];
    p1g = p1 * block(ii).rot + block(ii).trans;
    p2g = p2 * block(ii).rot + block(ii).trans;
    p3g = p3 * block(ii).rot + block(ii).trans;
    p4g = p4 * block(ii).rot + block(ii).trans;
    p5g = p5 * block(ii).rot + block(ii).trans;
    p6g = p6 * block(ii).rot + block(ii).trans;
    p7g = p7 * block(ii).rot + block(ii).trans;
    p8g = p8 * block(ii).rot + block(ii).trans;
    y_max = max([p1g(2), p2g(2), p3g(2), p4g(2), p5g(2), p6g(2), p7g(2), p8g(2)]);
    y_min = min([p1g(2), p2g(2), p3g(2), p4g(2), p5g(2), p6g(2), p7g(2), p8g(2)]);
    plot(y_min, 0, 'r+');
    plot(y_max, 0, 'r+');
end

%Plot the field
[x_close, ix_close] = min(abs(field.pos(:, 1) - x_val_req));
x_val = field.pos(ix_close(1), 1);
[z_close, iz_close] = min(abs(field.pos(:, 3) - z_val_req));
z_val = field.pos(iz_close(1), 3);
index_array = find(((field.pos(:, 1) == x_val) & (field.pos(:, 3) == z_val)) == 1);
y = field.pos(index_array, 2);
bz = field.b(index_array, 3);
plot(y, bz);

%Labels
title(['Bz vs Y At X = ', num2str(x_val), ' m, Z = ', num2str(z_val), ' m']);
xlabel('Y (m)');
ylabel('Bz (T)');

%Finalize
grid on;
zoom on;
hold off;

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

%Done
