%**************************************************************************
%This function makes the final results data file.
%
%Input:
%dat_file, name of the final results data file
%param, structure containing the analysis parameters
%header, input data file header
%field, structure containing magnetic field information
%traj, structure containing trajectory information
%rad, structure containing radiation information
%
%Zachary Wolf, 12/17/06

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

%Initialize the data file
util_dat_init(dat_file);

%Write a message to the data file
msg{1} = ' ';
msg{2} = 'Planar Insertion Device Analysis';
msg{3} = '--------------------------------';
util_dat_msg(dat_file, msg);

%Write a header to the data file
util_dat_header(dat_file, header);

%Write the parameter values to the data file
pid_anal_dat_param(dat_file, param);

%Write a message to the data file that the field was corrected
field_corr_dat_field_corr(dat_file, param);

%Write a summary of the peak field results to the data file
field_pid_dat_peaks_zeros_summary(param, dat_file, field.z_peak, field.by_peak, field.z_zero, field.lambda_u);

%Write the overall field integrals to the data file
int_dat_b_int(dat_file, field.int_bx_dz(end), field.int_int_bx_dz_dz(end), field.int_by_dz(end), field.int_int_by_dz_dz(end));

%Write the trajectory exit information to the data file
traj_dat_traj_exit(dat_file, traj.x(end), traj.x_p(end), traj.y(end), traj.y_p(end));

%Write the undulator parameters to the data file
k_dat_keff_beff_lambdar(dat_file, rad.k_eff, rad.b_eff, rad.lambda_r);

%Write a summary of the results to the data file
phase_pid_dat_phase_summary(dat_file, param, rad);

%Write detailed values vs z
pid_anal_dat_vals_vs_z(dat_file, param, field, traj, rad);

%Done
