%**************************************************************************
%This function plots Int By dz as a function
%of x along a line whose y position is specified.
%The closest y 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
%field_int, structure containing the field integral information
%y_val_req, requested y position of line (m)
%
%Zachary Wolf, 9/30/10

function magic_fingers_plot_intbydz_vs_x(plt_file, block, charge, field_int, 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;
    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 integral
[y_close, iy_close] = min(abs(field_int.pos(:, 2) - y_val_req));
y_val = field_int.pos(iy_close(1), 2);
index_array = find(field_int.pos(:, 2) == y_val);
x = field_int.pos(index_array, 1);
int_by_dz = field_int.int_by_dz(index_array);
plot(x, int_by_dz);

%Labels
title(['(Int By dz) vs X At Y = ', num2str(y_val), ' m']);
xlabel('X (m)');
ylabel('Int By dz (Tm)');

%Finalize
grid on;
zoom on;
hold off;

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

%Done
