%**************************************************************************
%This function plots the y component of the magnetic field as a function
%of z along a line whose x and y positions are specified.
%The closest x and y 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)
%y_val_req, requested y position of line (m)
%
%Zachary Wolf, 4/22/10

function pm_block_rect_plot_by_vs_z(plt_file, block, charge, field, x_val_req, y_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;
    z_max = max([p1g(3), p2g(3), p3g(3), p4g(3), p5g(3), p6g(3), p7g(3), p8g(3)]);
    z_min = min([p1g(3), p2g(3), p3g(3), p4g(3), p5g(3), p6g(3), p7g(3), p8g(3)]);
    plot(z_min, 0, 'r+');
    plot(z_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);
[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);
by = field.b(index_array, 2);
plot(z, by);

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

%Finalize
grid on;
zoom on;
hold off;

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

%Done
