%**************************************************************************
%This function reads insertion device analysis results.  It compares the
%results and plots the comparisons.
%
%Zachary Wolf, 12/24/07

function [] = pid_comp()

%Clear the workspace
clear

%Set parameter values by executing the parameter file
pid_comp_param

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

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

%Initialize the plots
pid_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 the nominal undulator gap
    [nom_gap] = util_get_gap_header(header);
    gap(ii) = nom_gap;
    
    %Get array data
    z_peak{ii, :} = field.z_peak;
    by_peak{ii, :} = field.by_peak;
    %temp{ii, :} = field.temp;
    z{ii, :} = traj.z;
    x{ii, :} = traj.x;
    y{ii, :} = traj.y;
    phase_peak{ii, :} = phas.phase_peak;
    phase_nom_peak{ii, :} = phas.phase_nom_peak;
    i2x{ii, :} = field.int_int_bx_dz_dz;
    i2y{ii, :} = field.int_int_by_dz_dz;
    
    %Get scalar data
    k_eff(ii) = phas.k_eff;
    phase_cell(ii) = phas.phase_cell;
    ideal_phase_match(ii) = phas.ideal_phase_match;
    ideal_phase_cell(ii) = phas.nom_phase_cell;
    pe_enter(ii) = phas.phase_match_error_enter_ave;
    pe_center(ii) = phas.phase_match_error_center_ave;
    pe_center_ave(ii) = phas.phase_match_error_center_ave;
    pe_center_rms(ii) = phas.phase_match_error_center_rms;
    pe_exit(ii) = phas.phase_match_error_exit_ave;
    pe_cell(ii) = phas.phase_error_cell;
    
    %Get the temperature
	[temp(ii)] = temp_get_und_temp_meas(header);
    t_ave(ii) = temp(ii).t_ave;
    
    %Get the date
	[dd, mm, yyyy] = util_get_date_header(header);
    date_vec(ii, :) = [yyyy, mm, dd];
    date_num(ii) = datenum([yyyy, mm, dd]);
    
    %Calculations
    peak_num{ii, :} = [1 : length(z_peak{ii, :})];
    
%End loop over analysis results
end

%Write values to the data file
%pid_comp_dat_results(dat_file, date_num, INPUT_NAME, t_ave, k_eff);
pid_comp_dat_phase_match_error(dat_file, gap, k_eff, ideal_phase_match, ideal_phase_cell, pe_enter, pe_center, pe_exit, pe_cell);

%Plot the peak fields
%pid_comp_plot_by_peak(plt_file, peak_num, by_peak, INPUT_NAME);

%Plot the difference in the peak fields
%pid_comp_plot_by_peak_diff(plt_file, peak_num, by_peak, INPUT_NAME);

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

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

%Plot I2x
pid_comp_plot_i2x(plt_file, z, i2x, INPUT_NAME);

%Plot I2y
pid_comp_plot_i2y(plt_file, z, i2y, INPUT_NAME);

%Plot the difference in the phase
%pid_comp_plot_phase_peak_diff(plt_file, peak_num, phase_peak, INPUT_NAME);

%Plot the difference in the phase relative to the nominal radiation wave
%pid_comp_plot_phase_nom_peak_diff(plt_file, peak_num, phase_nom_peak, INPUT_NAME);

%Plot the difference in k_eff relative to the first data set
%pid_comp_plot_k_eff_diff(plt_file, date_num, k_eff, INPUT_NAME);

%Plot the entrance phase matching error vs K
k = k_eff;
pme_enter = pe_center;
[pme_enter_resid, k_fit, pme_enter_fit, msg_fit] = pid_fit_phase_match_err(k, pme_enter);
%pid_comp_plot_phase_match_error_center_vs_k(plt_file, k_eff, pe_center);
pid_comp_plot_phase_match_error_enter_vs_k_fit(plt_file, k, pme_enter, k_fit, pme_enter_fit, msg_fit);
pid_comp_plot_phase_match_error_enter_vs_k_resid(plt_file, k_eff, pme_enter_resid);

%Find the entrance phase matching correction
pme_enter_res = mod(pe_center_ave, 2 * pi);
pme_enter_corr = 2 * pi - pme_enter_res;
%xxxpid_comp_dat_phase_match_enter(dat_file...

%Plot the entrance phase matching error vs K
k = k_eff;
pme_exit = pe_cell - pme_enter;
[pme_exit_resid, k_fit, pme_exit_fit, msg_fit] = pid_fit_phase_match_err(k, pme_exit);
pid_comp_plot_phase_match_error_exit_vs_k_fit(plt_file, k, pme_exit, k_fit, pme_exit_fit, msg_fit);
pid_comp_plot_phase_match_error_exit_vs_k_resid(plt_file, k_eff, pme_exit_resid);

%Plot the difference of the phase matching errors
pme_diff = pme_enter - pme_exit;
pid_comp_plot_phase_match_error_diff(plt_file, k_eff, pme_diff);

%Plot the phase error of the cell vs K
pid_comp_plot_phase_error_cell_vs_k(plt_file, k_eff, pe_cell);

%Plot the difference in phase_nom_cell relative to the first data set
%pid_comp_plot_phase_nom_cell_diff(plt_file, date_num, phase_nom_cell, INPUT_NAME);

%Plot the temperature of each data set
%pid_comp_plot_temp(plt_file, date_num, t_ave, INPUT_NAME);

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

%Done
