%**************************************************************************
%This function reads data analysis results.  It compares the
%results and plots the comparisons.
%
%Zachary Wolf
%5/27/12

function [] = ps_comp()

%Clear the workspace
clear

%Set parameter values by executing the parameter file
ps_comp_param

%Output files
dat_file = [OUTPUT_DIR, 'ps_comp.dat'];
plt_file = [OUTPUT_DIR, 'ps_comp.ps'];
mat_file = [OUTPUT_DIR, 'ps_comp.mat'];

%Initialize the data file
util_dat_init(dat_file);
util_dat_msg(dat_file, cellstr(' '));
util_dat_msg(dat_file, cellstr('Analysis Results:'));
util_dat_msg(dat_file, cellstr(COMPARE_NAME));

%Initialize the plots
ps_comp_plot_init(plt_file, COMPARE_NAME, INPUT_FILE);

%Loop over analysis results, get data
[r, c] = size(INPUT_FILE);
for ii = 1 : c
    
    %Get the input file
    in_mat_file = INPUT_FILE{ii};

    %Input the analysis results
    load(in_mat_file, 'param', 'header', 'field', 'traj', 'phas');
    
    %Get array data
    bx{ii, :} = field.bx;
    by{ii, :} = field.by;
    int_bx_dz{ii, :} = field.int_bx_dz;
    int_by_dz{ii, :} = field.int_by_dz;
    int_int_bx_dz_dz{ii, :} = field.int_int_bx_dz_dz;
    int_int_by_dz_dz{ii, :} = field.int_int_by_dz_dz;
    z{ii, :} = traj.z;
    x{ii, :} = traj.x;
    y{ii, :} = traj.y;
    
    %Get scalar data
    int_bx_dz_end(ii) = field.int_bx_dz(end);
    int_by_dz_end(ii) = field.int_by_dz(end);
    int_int_bx_dz_dz_end(ii) = field.int_int_bx_dz_dz(end);
    int_int_by_dz_dz_end(ii) = field.int_int_by_dz_dz(end);
    mag_length_b(ii) = field.mag_length_by;
    mag_length_int_b(ii) = field.mag_length_i1y;
    phase_integral_end(ii) = phas.phase_integral_end;
    
    %Get the date
	[dd, mm, yyyy] = util_get_date_header(header);
    date_vec(ii, :) = [yyyy, mm, dd];
    date_num(ii) = datenum([yyyy, mm, dd]);
    
    %Get the gap
    %gap(ii) = ps_comp_get_gap_header(header);
    gap(ii) = sscanf(INPUT_NAME{ii}, 'g = %f mm');
    gap(ii) = gap(ii) / 1000;  %(m)
    
%End loop over analysis results
end

%Write values to the data file
ps_comp_dat_results(dat_file, INPUT_NAME, int_bx_dz_end, int_by_dz_end, int_int_bx_dz_dz_end, int_int_by_dz_dz_end, phase_integral_end);

%Plot the x trajectories
ps_comp_plot_x_traj(plt_file, z, x, INPUT_NAME);

%Plot the y trajectories
ps_comp_plot_y_traj(plt_file, z, y, INPUT_NAME);

%Plot the field integrals
ps_comp_plot_i1bx(plt_file, int_bx_dz_end, INPUT_NAME);
ps_comp_plot_i1by(plt_file, int_by_dz_end, INPUT_NAME);
ps_comp_plot_i2bx(plt_file, int_int_bx_dz_dz_end, INPUT_NAME);
ps_comp_plot_i2by(plt_file, int_int_by_dz_dz_end, INPUT_NAME);

%Plot the magnetic lengths
%ps_comp_plot_mag_length_b(plt_file, mag_length_b, INPUT_NAME);
ps_comp_plot_mag_length_int_b(plt_file, mag_length_int_b, INPUT_NAME);

%Plot the phase integral
ps_comp_plot_phase_int(plt_file, phase_integral_end, INPUT_NAME);

%Save the results to the mat file
save(mat_file, 'COMPARE_NAME', 'OUTPUT_DIR', 'INPUT_FILE', 'INPUT_NAME');

%Done
