%**************************************************************************
%This function writes radiation information to the data file.
%
%Input:
%dat_file, data file for results
%param, parameter structure
%rad, structure containing the radiation information
%
%Zachary Wolf
%8/2/10

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

%Write the results to the data file
fid = fopen(dat_file, 'a');
fprintf(fid, '\r\n');
fprintf(fid, 'Spontaneous Radiation:\r\n');
fprintf(fid, '(Forward direction)\r\n');
fprintf(fid, 'Single electron at fundamental:\r\n');
fprintf(fid, '  |Ex| = %e (V/m), |Ey| = %e (V/m)\r\n', rad.ex_mag_fund, rad.ey_mag_fund);
fprintf(fid, '  Angle_y - Angle_x = %f (deg)\r\n', rad.exy_ang_fund * 180. / pi);
fprintf(fid, 'Stokes Parameters:\r\n');
fprintf(fid, '  S0 = Ix + Iy = %e, S1 = Ix - Iy = %e (V/m)^2\r\n', rad.s0, rad.s1);
fprintf(fid, '  S2 = I45 - I135 = %e, S3 = Ir - Il = %e (V/m)^2\r\n', rad.s2, rad.s3);
fprintf(fid, 'Polarization Rates:\r\n');
fprintf(fid, '  P1 = S1 / S0 = %f, P2 = S2 / S0 = %f\r\n', rad.p1, rad.p2);
fprintf(fid, '  P3 = S3 / S0 = %f\r\n', rad.p3);
fprintf(fid, 'Spectra:\r\n');
%fprintf(fid, '(Ideal = average period repeated with no phase errors)\r\n');
fprintf(fid, '(Ideal = calc field period using FT coeff repeated with no phase errors)\r\n');
fprintf(fid, 'Harmonic   Energy   Ang Num Spect       Ideal        Efficiency \r\n');
fprintf(fid, '   #       (keV)     (ph/sec/mrad^2/0.1%%bw/0.1A)          %%   \r\n');
fprintf(fid, '--------  --------  --------------  --------------  ------------\r\n');
for ii = 1 : 2 : length(rad.omega_har) - 1
    fprintf(fid, '%4i      %8.3f  %14.3e  %14.3e  %12.2f\r\n', ii, rad.energy_kev_har(ii), rad.ang_num_spect_har(ii), rad.ang_num_spect_ideal_har(ii), rad.efficiency_har(ii) * 100.);
    fprintf(fid, '%4i      %8.3f  %14.3e        ---            ---\r\n', ii + 1, rad.energy_kev_har(ii + 1), rad.ang_num_spect_har(ii + 1));
end
if (rem(length(rad.omega_har), 2) > 0)
    ii = length(rad.omega_har);
    fprintf(fid, '%4i      %8.3f  %14.3e  %14.3e  %12.2f\r\n', ii, rad.energy_kev_har(ii), rad.ang_num_spect_har(ii), rad.ang_num_spect_ideal_har(ii), rad.efficiency_har(ii) * 100.);
end

fclose(fid);

%Done
end