%**************************************************************************
%This function writes phase information to the data file.
%
%Input:
%dat_file, data file for results
%param, parameter structure
%rad, structure containing the phase information
%
%Zachary Wolf, 12/27/05, 08/04/07

function [] = phase_dat_phase_results(dat_file, param, rad)

%Parameter
NUM_POLES_AVE = 10;

%Calculate entrance
nom_phase_enter_peak_no_ends = phase_match_calc_phase_match_nom_phase_from_cell_enter(param);
phase_nom_error_enter_peak_no_ends = rad.phase_nom_peak_no_ends - nom_phase_enter_peak_no_ends;
phase_nom_error_enter_peak_no_ends_ave = mean(phase_nom_error_enter_peak_no_ends(1 : NUM_POLES_AVE));
phase_nom_error_enter_peak_no_ends_rms = std(phase_nom_error_enter_peak_no_ends(1 : NUM_POLES_AVE));

%Calculate middle
phase_nom_error_peak_no_ends_ave = mean(rad.phase_nom_error_peak_no_ends);
phase_nom_error_peak_no_ends_rms = std(rad.phase_nom_error_peak_no_ends);

%Calculate middle, use actual wave, not nominal wave
phase_error_peak_no_ends_ave = mean(rad.phase_error_peak_no_ends);
phase_error_peak_no_ends_rms = std(rad.phase_error_peak_no_ends);

%Calculate exit
nom_phase_exit_peak_no_ends = phase_match_calc_phase_match_nom_phase_to_cell_exit(param);
phase_nom_exit_peak_no_ends = rad.phase_nom_cell - rad.phase_nom_peak_no_ends;
phase_nom_error_exit_peak_no_ends = phase_nom_exit_peak_no_ends - nom_phase_exit_peak_no_ends;
phase_nom_error_exit_peak_no_ends_ave = mean(phase_nom_error_exit_peak_no_ends(end - NUM_POLES_AVE + 1 : end));
phase_nom_error_exit_peak_no_ends_rms = std(phase_nom_error_exit_peak_no_ends(end - NUM_POLES_AVE + 1 : end));

%Write the results to the data file
fid = fopen(dat_file, 'a');
fprintf(fid, '\r\n');
fprintf(fid, 'Phase Fluctuations:\r\n');
fprintf(fid, 'Slippage vs Z At Peaks Fitted Slope = %14.5e\r\n', rad.slope);
fprintf(fid, 'Undulator Period = %f m\r\n', rad.lambda_u);
fprintf(fid, 'Radiation Wavelength = %14.5e m\r\n', rad.lambda_r);
fprintf(fid, 'Phase Error (From Slippage vs Z Fit Residuals):\r\n');
fprintf(fid, '  Ave = %f deg, RMS = %f deg\r\n', phase_error_peak_no_ends_ave * 180 / pi, phase_error_peak_no_ends_rms * 180 / pi);
fprintf(fid, '\r\n');
fprintf(fid, 'Phase Relative To Nominal Radiation Wave:\r\n');
fprintf(fid, 'Nominal Radiation Wavelength = %14.5e m\r\n', param.nom_lambda_r);
fprintf(fid, 'Cell Length = %f m\r\n', param.cell_length);
fprintf(fid, 'Number Of End Poles = %i\r\n', param.num_end_poles);
fprintf(fid, 'Entrance:\r\n');
fprintf(fid, '  Phase Error Of First %i Poles After The End Poles\r\n', NUM_POLES_AVE);
fprintf(fid, '  Ave = %f deg, RMS = %f deg\r\n', phase_nom_error_enter_peak_no_ends_ave * 180 / pi, phase_nom_error_enter_peak_no_ends_rms * 180 / pi);
fprintf(fid, 'Center:\r\n');
fprintf(fid, '  Phase Error Of All Poles In Undulator Center:\r\n');
fprintf(fid, '  Ave = %f deg, RMS = %f deg\r\n', phase_nom_error_peak_no_ends_ave * 180 / pi, phase_nom_error_peak_no_ends_rms * 180 / pi);
fprintf(fid, 'Exit:\r\n');
fprintf(fid, '  Phase Error To Cell Boundary Of Last %i Poles Before The End Poles\r\n', NUM_POLES_AVE);
fprintf(fid, '  Ave = %f deg, RMS = %f deg\r\n', phase_nom_error_exit_peak_no_ends_ave * 180 / pi, phase_nom_error_exit_peak_no_ends_rms * 180 / pi);
fprintf(fid, 'Cell:\r\n');
fprintf(fid, '  Phase Change Of Cell = %11.3f deg\r\n', rad.phase_nom_cell * 180 / pi);
fprintf(fid, '  Nominal Phase Change Of Cell = %11.3f deg\r\n', param.nom_phase_cell * 180. / pi);
fprintf(fid, '  Phase Error Of Cell = %11.3f deg\r\n', (rad.phase_nom_cell * 180 / pi) - (param.nom_phase_cell * 180. / pi));
fclose(fid);

%Done
