%**************************************************************************
%This function reads undulator analysis results.  It also reads
%fiducialization results.  It compares corrected K value measurements.
%
%Zachary Wolf, 4/7/09

function [] = k_comp()

%Clear the workspace
clear

%Set parameter values by executing the parameter file
k_comp_param

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

%Initialize the data file
util_dat_init(dat_file);
util_dat_msg(dat_file, cellstr(' '));
util_dat_msg(dat_file, cellstr('K Calculation Comparison:'));
util_dat_msg(dat_file, cellstr(COMPARE_NAME));

%Initialize the plots
k_comp_plot_init(plt_file, COMPARE_NAME);

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

    %Input the analysis results
    load(in_mat_file, 'param', 'header', 'field', 'traj', 'rad');
    
    %Get the K value
    k_eff(ii) = rad.k_eff;
    
    %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]);
    
%End loop over analysis results
end

%Loop over fiducialization results, get data
[r, c] = size(FID_INPUT_FILE);
for ii = 1 : c
    
    %Get the input file
    file_name = FID_INPUT_FILE{ii};
    
    %Get the measurement date from the file name
    index = findstr(file_name, '.');
    date_str{ii} = file_name(index - 11 : index - 1);
    date_num(ii) = datenum(date_str{ii}, 'dd-mmm-yyyy');
    
    %Get the data
	[tbvals] = cmm_tbval_comp_read_data(file_name);
    tbval_data(ii) = tbvals;
    
    %Calculations
    x_ave(ii) = (tbvals.x(5) + tbvals.x(6) + tbvals.x(7) + tbvals.x(8)) / 4;
    y_ave(ii) = (tbvals.y(1) + tbvals.y(2) + tbvals.y(3) + tbvals.y(4)) / 4;
    
%End loop over analysis results
end

%Calculate the difference between Keff and the nominal value
dk_eff = k_eff - k_eff(1);

%Calculate the difference between T and the nominal value
dt_ave = t_ave - 20;

%Calculate the x change to move to the nominal k_eff
%If the K value the undulator is set to is too large, then the x value
%determined in the fiducialization is less than if K were set correctly,
%dx_k is negative, the correction to get to the line of nominal K is in the
%+x direction
dx_k = dk_eff / DKDX;  %K > Knom ==> Xfid < Xnom
dx_corr_k = - dx_k;  %Correction to Xnom which gives Knom

%Calculate the x change to move to the 20 C axis
%If the lab temperature is greater than 20 C, the field is weaker, the x
%position of the fiducialization is shifted negative to compensate for the
%weaker field, the correction is in the +x direction to give the axis at 20 C
dk_t = dt_ave * DKDT;  %T > 20 C ==> K < Knom at given X, dk_t < 0
dx_t = - dk_t / DKDX;  %Shift dx_t < 0 to compensate, fiducialize there
dx_corr_t = - dx_t;  %Correction to go from Xfid to Xnom at 20 C, dx_corr_t > 0

%Calculate the x change to correct for K and T
dx_corr_k_t = dx_corr_k + dx_corr_t;

%Apply the correction
x_ave_corr = x_ave + dx_corr_k_t;
dx_ave_corr = x_ave_corr - x_ave_corr(1);

%Write values to the data file
k_comp_dat_results(dat_file, date_num, INPUT_NAME, k_eff, dk_eff, t_ave, dt_ave, dx_corr_k, dx_corr_t, dx_corr_k_t);

%Plot the k_eff values
k_comp_plot_keff(plt_file, date_num, k_eff, INPUT_NAME);

%Plot the k_eff difference values dk_eff
k_comp_plot_dkeff(plt_file, date_num, dk_eff, INPUT_NAME);

%Plot the x corrections to move to the nominal k_eff
k_comp_plot_dxcorrk(plt_file, date_num, dx_corr_k, INPUT_NAME);

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

%Plot the temperature difference from the nominal temperature
k_comp_plot_dtemp(plt_file, date_num, dt_ave, INPUT_NAME);

%Plot the x corrections to move to the nominal temperature
k_comp_plot_dxcorrt(plt_file, date_num, dx_corr_t, INPUT_NAME);

%Plot the x corrections to move to the nominal K and nominal temperature
k_comp_plot_dxcorrkt(plt_file, date_num, dx_corr_k_t, INPUT_NAME);

%Plot the measured tooling ball x values
k_comp_plot_xave(plt_file, date_num, x_ave, INPUT_NAME);

%Plot the corrected tooling ball x values
k_comp_plot_xave_corr(plt_file, date_num, x_ave_corr, INPUT_NAME);

%Plot the change in corrected tooling ball x values
k_comp_plot_dxave_corr(plt_file, date_num, dx_ave_corr, INPUT_NAME);

%Save the results to the mat file
save(mat_file, 'COMPARE_NAME', 'OUTPUT_DIR', 'ANL_INPUT_FILE', 'FID_INPUT_FILE', 'INPUT_NAME', 'K_NOM', 'DKDX', 'DKDT',  'k_eff', 't_ave', 'date_num', 'x_ave');

%Done
