%**************************************************************************
%This function writes field harmonic information to the data file.
%
%Input:
%dat_file, data file for results
%a0, constant term (T)
%an, cosine terms (T)
%bn, sine terms (T)
%
%Zachary Wolf
%8/19/10

function [] = field_dat_har_by(dat_file, a0, an, bn)
  
%Check
if abs(a0) == 0 || max(abs(an)) == 0 || max(abs(bn)) == 0
    return;
end

%Write the results to the data file
fid = fopen(dat_file, 'a');
fprintf(fid, '\r\n');
fprintf(fid, 'By Field Harmonics:\r\n');
fprintf(fid, 'In the undulator center, over an integer number of periods, expand By as\r\n');
fprintf(fid, 'By(z) = a0 + Sum{ an cos(n 2 pi z / lambda_u) + bn sin(n 2 pi z / lambda_u) }\r\n');
fprintf(fid, '   n         an        bn   \r\n');
fprintf(fid, '            (T)       (T)   \r\n');
fprintf(fid, '--------  --------  --------\r\n');
fprintf(fid, '%4i      %8.5f\r\n', 0, a0);
for ii = 1 : length(an)
    fprintf(fid, '%4i      %8.5f  %8.5f\r\n', ii, an(ii), bn(ii));
end
fclose(fid);

%Done
end