%**************************************************************************
%This function reads insertion device analysis results.  It gets the phase
%matching information and performs fits.
%
%Zachary Wolf
%6/22/16

function [] = pid_fit_phase_match()

%Clear the workspace
clear

%Set parameter values by executing the parameter file
pid_fit_phase_match_param

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

%Initialize the data file
util_dat_init(dat_file);
util_dat_msg(dat_file, cellstr(' '));
util_dat_msg(dat_file, cellstr('Fit Phase Match Results:'));
util_dat_msg(dat_file, cellstr(FIT_NAME));

%Initialize the plots
pid_fit_plot_init(plt_file, FIT_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', 'gap', 'temp', 'field', 'traj', 'phas');
    
    %Get the analysis results
    gapm(ii) = gap;
    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;
    pme_enter(ii) = phas.pme_enter;
    phase_shifter_enter(ii) = phas.phase_shifter_enter;
    pme_exit(ii) = phas.pme_exit;
    phase_shifter_exit(ii) = phas.phase_shifter_exit;
    
    %Get the run number
    [run_num] = util_get_run_header(header);
    run(ii) = run_num;
    
%End loop over analysis results
end
gap = gapm;

%Write values to the data file
%pid_fit_dat_phase_match_error_all(dat_file, gap, k_eff, ideal_phase_match, ideal_phase_cell, pe_enter, pe_center, pe_exit, pe_cell);
pid_fit_dat_phase_match_error(dat_file, gap, k_eff, ideal_phase_match, ideal_phase_cell, pme_enter, pme_exit, pe_cell);

%Fit and plot the entrance phase matching error vs K
k = k_eff;
[pme_enter_resid, k_fit, pme_enter_fit, msg_fit] = pid_fit_phase_match_err(k, pme_enter);
%pid_fit_plot_phase_match_error_center_vs_k(plt_file, k_eff, pe_center);
pid_fit_plot_phase_match_error_enter_vs_k_fit(plt_file, k, pme_enter, k_fit, pme_enter_fit, msg_fit);
pid_fit_plot_phase_match_error_enter_vs_k_resid(plt_file, k_eff, pme_enter_resid);
pid_fit_plot_phase_shifter_enter(plt_file, k_eff, phase_shifter_enter);

%Fit and plot the exit phase matching error vs K
k = k_eff;
[pme_exit_resid, k_fit, pme_exit_fit, msg_fit] = pid_fit_phase_match_err(k, pme_exit);
pid_fit_plot_phase_match_error_exit_vs_k_fit(plt_file, k, pme_exit, k_fit, pme_exit_fit, msg_fit);
pid_fit_plot_phase_match_error_exit_vs_k_resid(plt_file, k_eff, pme_exit_resid);
pid_fit_plot_phase_shifter_exit(plt_file, k_eff, phase_shifter_exit);

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

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

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

%Write the data to an Excel file
col_names = {'Run #', 'Gap (mm)', 'Keff', 'PME Enter (deg)', 'PME Exit (deg)', 'PE Cell (deg)'};
xlswrite(xls_file, col_names, 1, 'a1:f1');
xlswrite(xls_file, run', 1, 'a2');
xlswrite(xls_file, (gap * 1000)', 1, 'b2');
xlswrite(xls_file, k_eff', 1, 'c2');
xlswrite(xls_file, (pme_enter * 180 / pi)', 1, 'd2');
xlswrite(xls_file, (pme_exit * 180 / pi)', 1, 'e2');
xlswrite(xls_file, (pe_cell * 180 / pi)', 1, 'f2');

%Done
