%**************************************************************************
%This function writes detailed results to a data file.  It creates the
%file.
%
%Input:
%dat_file, name of the final results data file
%param, structure containing the analysis parameters
%field, structure containing magnetic field information
%traj, structure containing trajectory information
%rad, structure containing radiation information
%
%Zachary Wolf, 8/3/07

function [] = pid_anal_dat_vals(dat_file, param, field, traj, rad)

%Create the data file
fid = fopen(dat_file, 'w');

%Calculations
pole_num = 1 : param.num_poles;
pole_num_no_ends = (param.num_end_poles + 1) : (param.num_poles - param.num_end_poles);

%Write summary values to the data file
fprintf(fid, '\r\n');
fprintf(fid, 'Summary values\r\n');
fprintf(fid, '--------------\r\n');
fprintf(fid, 'average Bx peak field, no ends (T)\r\n');
fprintf(fid, '%15.7e  ', mean(abs(field.bx_peak_no_ends)));
fprintf(fid, '\r\n');
fprintf(fid, 'average By peak field, no ends (T)\r\n');
fprintf(fid, '%15.7e  ', mean(abs(field.by_peak_no_ends)));
fprintf(fid, '\r\n');
fprintf(fid, 'int_bx_dz_end (Tm)\r\n');
fprintf(fid, '%15.7e  ', field.int_bx_dz(end));
fprintf(fid, '\r\n');
fprintf(fid, 'int_by_dz_end (Tm)\r\n');
fprintf(fid, '%15.7e  ', field.int_by_dz(end));
fprintf(fid, '\r\n');
fprintf(fid, 'int_int_bx_dz_dz_end (Tm^2)\r\n');
fprintf(fid, '%15.7e  ', field.int_int_bx_dz_dz(end));
fprintf(fid, '\r\n');
fprintf(fid, 'int_int_by_dz_dz_end (Tm^2)\r\n');
fprintf(fid, '%15.7e  ', field.int_int_by_dz_dz(end));
fprintf(fid, '\r\n');
fprintf(fid, 'x_end (m)\r\n');
fprintf(fid, '%15.7e  ', traj.x(end));
fprintf(fid, '\r\n');
fprintf(fid, 'y_end (m)\r\n');
fprintf(fid, '%15.7e  ', traj.y(end));
fprintf(fid, '\r\n');
fprintf(fid, 'rms phase error (rad)\r\n');
fprintf(fid, '%15.7e  ', std(rad.phase_error_peak_no_ends));
fprintf(fid, '\r\n');
fprintf(fid, 'ave phase error relative to the nominal radiation wave in the undulator center (rad)\r\n');
fprintf(fid, '%15.7e  ', mean(rad.phase_nom_error_peak_no_ends));
fprintf(fid, '\r\n');
fprintf(fid, 'rms phase error relative to the nominal radiation wave in the undulator center (rad)\r\n');
fprintf(fid, '%15.7e  ', std(rad.phase_nom_error_peak_no_ends));
fprintf(fid, '\r\n');
fprintf(fid, 'phase error of cell relative to nominal radiation wave (rad)\r\n');
fprintf(fid, '%15.7e  ', rad.phase_nom_cell - param.nom_phase_cell);
fprintf(fid, '\r\n');

%Write the results at the field peaks to the data file
fprintf(fid, '\r\n');
fprintf(fid, 'Values vs z_peak\r\n');
fprintf(fid, '----------------\r\n');
fprintf(fid, 'pole_num\r\n');
fprintf(fid, '%15i  ', pole_num);
fprintf(fid, '\r\n');
fprintf(fid, 'z_peak (m)\r\n');
fprintf(fid, '%15.7e  ', field.z_peak);
fprintf(fid, '\r\n');
fprintf(fid, 'bx_peak (T)\r\n');
fprintf(fid, '%15.7e  ', field.bx_peak);
fprintf(fid, '\r\n');
fprintf(fid, 'by_peak (T)\r\n');
fprintf(fid, '%15.7e  ', field.by_peak);
fprintf(fid, '\r\n');
fprintf(fid, 'slip_peak (m)\r\n');
fprintf(fid, '%15.7e  ', rad.slip_peak);
fprintf(fid, '\r\n');
fprintf(fid, 'phase_peak (rad)\r\n');
fprintf(fid, '%15.7e  ', rad.phase_peak);
fprintf(fid, '\r\n');
fprintf(fid, 'phase_nom_peak (rad)\r\n');
fprintf(fid, '%15.7e  ', rad.phase_nom_peak);
fprintf(fid, '\r\n');

%Write the results at the field peaks with no ends to the data file
fprintf(fid, '\r\n');
fprintf(fid, 'Values vs z_peak_no_ends\r\n');
fprintf(fid, '------------------------\r\n');
fprintf(fid, 'pole_num_no_ends\r\n');
fprintf(fid, '%15i  ', pole_num_no_ends);
fprintf(fid, '\r\n');
fprintf(fid, 'z_peak_no_ends (m)\r\n');
fprintf(fid, '%15.7e  ', rad.z_peak_no_ends);
fprintf(fid, '\r\n');
fprintf(fid, 'bx_peak_no_ends (T)\r\n');
fprintf(fid, '%15.7e  ', field.bx_peak_no_ends);
fprintf(fid, '\r\n');
fprintf(fid, 'by_peak_no_ends (T)\r\n');
fprintf(fid, '%15.7e  ', field.by_peak_no_ends);
fprintf(fid, '\r\n');
fprintf(fid, 'slip_peak_no_ends (m)\r\n');
fprintf(fid, '%15.7e  ', rad.slip_peak_no_ends);
fprintf(fid, '\r\n');
fprintf(fid, 'phase_peak_no_ends (rad)\r\n');
fprintf(fid, '%15.7e  ', rad.phase_peak_no_ends);
fprintf(fid, '\r\n');
fprintf(fid, 'phase_error_peak_no_ends (rad)\r\n');
fprintf(fid, '%15.7e  ', rad.phase_error_peak_no_ends);
fprintf(fid, '\r\n');
fprintf(fid, 'phase_nom_peak_no_ends (rad)\r\n');
fprintf(fid, '%15.7e  ', rad.phase_nom_peak_no_ends);
fprintf(fid, '\r\n');
fprintf(fid, 'phase_nom_error_peak_no_ends (rad)\r\n');
fprintf(fid, '%15.7e  ', rad.phase_nom_error_peak_no_ends);
fprintf(fid, '\r\n');

%Write the continuous results to the data file
fprintf(fid, '\r\n');
fprintf(fid, 'Values vs z\r\n');
fprintf(fid, '-----------\r\n');
fprintf(fid, 'z (m)\r\n');
fprintf(fid, '%15.7e  ', field.z);
fprintf(fid, '\r\n');
fprintf(fid, 'bx (T)\r\n');
fprintf(fid, '%15.7e  ', field.bx);
fprintf(fid, '\r\n');
fprintf(fid, 'by (T)\r\n');
fprintf(fid, '%15.7e  ', field.by);
fprintf(fid, '\r\n');
fprintf(fid, 'int_bx_dz (Tm)\r\n');
fprintf(fid, '%15.7e  ', field.int_bx_dz);
fprintf(fid, '\r\n');
fprintf(fid, 'int_by_dz (Tm)\r\n');
fprintf(fid, '%15.7e  ', field.int_by_dz);
fprintf(fid, '\r\n');
fprintf(fid, 'int_int_bx_dz_dz (Tm^2)\r\n');
fprintf(fid, '%15.7e  ', field.int_int_bx_dz_dz);
fprintf(fid, '\r\n');
fprintf(fid, 'int_int_by_dz_dz (Tm^2)\r\n');
fprintf(fid, '%15.7e  ', field.int_int_by_dz_dz);
fprintf(fid, '\r\n');
fprintf(fid, 'x (m)\r\n');
fprintf(fid, '%15.7e  ', traj.x);
fprintf(fid, '\r\n');
fprintf(fid, 'y (m)\r\n');
fprintf(fid, '%15.7e  ', traj.y);
fprintf(fid, '\r\n');
fprintf(fid, 'x_filt (m)\r\n');
fprintf(fid, '%15.7e  ', traj.x_filt);
fprintf(fid, '\r\n');
fprintf(fid, 'y_filt (m)\r\n');
fprintf(fid, '%15.7e  ', traj.y_filt);
fprintf(fid, '\r\n');
fprintf(fid, 'slip (m)\r\n');
fprintf(fid, '%15.7e  ', rad.slip);
fprintf(fid, '\r\n');
fprintf(fid, 'phase (rad)\r\n');
fprintf(fid, '%15.7e  ', rad.phase);
fprintf(fid, '\r\n');
fprintf(fid, 'phase_nom (rad)\r\n');
fprintf(fid, '%15.7e  ', rad.phase_nom);
fprintf(fid, '\r\n');

%Close the data file
fclose(fid);

%Done
