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

function ppm_und_plot_bx_vs_x(plt_file, block, charge, field, y_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;
    x_max = max([p1g(1), p2g(1), p3g(1), p4g(1), p5g(1), p6g(1), p7g(1), p8g(1)]);
    x_min = min([p1g(1), p2g(1), p3g(1), p4g(1), p5g(1), p6g(1), p7g(1), p8g(1)]);
    plot(x_min, 0, 'r+');
    plot(x_max, 0, 'r+');
end

%Plot the field
[y_close, iy_close] = min(abs(field.pos(:, 2) - y_val_req));
y_val = field.pos(iy_close(1), 2);
[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(:, 2) == y_val) & (field.pos(:, 3) == z_val)) == 1);
x = field.pos(index_array, 1);
bx = field.b(index_array, 1);
plot(x, bx);

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

%Finalize
grid on;
zoom on;
hold off;

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

%Done
