%**************************************************************************
%This function writes phase information to the data file.
%
%Input:
%dat_file, data file for results
%param, parameter structure
%phas, structure containing the phase information
%
%Zachary Wolf, 6/30/10

function [] = phase_dat_phase_summary(dat_file, param, phas)

%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', phas.slope);
fprintf(fid, 'Undulator Period = %f m\r\n', phas.lambda_u);
fprintf(fid, 'Radiation Wavelength = %14.5e m\r\n', phas.lambda_r);
fprintf(fid, 'Phase Error (From Slippage vs Z Fit Residuals):\r\n');
fprintf(fid, '  Ave = %f deg, RMS = %f deg\r\n', phas.phase_error_peak_no_ends_ave * 180 / pi, phas.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, (if 0, use radiation wavelength)\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', param.num_pole_stat);
fprintf(fid, '  Ave = %f deg, RMS = %f deg\r\n', phas.phase_nom_error_peak_no_ends_enter_ave * 180 / pi, phas.phase_nom_error_peak_no_ends_enter_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', phas.phase_nom_error_peak_no_ends_ave * 180 / pi, phas.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', param.num_pole_stat);
fprintf(fid, '  Ave = %f deg, RMS = %f deg\r\n', phas.phase_nom_error_peak_no_ends_exit_ave * 180 / pi, phas.phase_nom_error_peak_no_ends_exit_rms * 180 / pi);
fprintf(fid, 'Cell:\r\n');
fprintf(fid, '  Phase Change Of Cell = %11.3f deg\r\n', phas.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', (phas.phase_nom_cell * 180 / pi) - (param.nom_phase_cell * 180. / pi));
fclose(fid);

%Done
