%**************************************************************************
%This function writes the shim information to set the gap to the data file.
%
%Input:
%dat_file, data file for results
%param, structure containing analysis parameters
%z_peak_no_ends, position of field peaks, no ends (m)
%phaserr_shim_dgap_no_ends, gap change required to correct the phase error, no ends (m)
%phaserr_shim_dx_no_ends, shim x motion required to correct the phase error, no ends (m)
%
%Zachary Wolf, 11/21/06

function [] = setgap_dat_shim(dat_file, param, z_peak_no_ends, phaserr_shim_dgap_no_ends, phaserr_shim_dx_no_ends)

%Open the data file
fid = fopen(dat_file, 'a');

%Write the x trajectory shim strengths to the data file
fprintf(fid, '\r\n\r\n\r\n');
fprintf(fid, '                          Set The Gap, Shim Data           \r\n');
fprintf(fid, '\r\n');
fprintf(fid, 'Same adjustments on both the upper and lower poles.   \r\n');
fprintf(fid, '\r\n');
fprintf(fid, 'Pole    Z Pole   Del Gap    Del X    |  Pole    Z Pole   Del Gap    Del X  \r\n');
fprintf(fid, ' #       (m)      (mu m)     (mm)    |   #       (m)      (mu m)     (mm)  \r\n');
fprintf(fid, '----  ---------  --------  --------  |  ----  ---------  --------  --------\r\n');
pole_num = param.num_end_poles + (1 : length(z_peak_no_ends));
index_show = 1 : 2 : length(z_peak_no_ends);
%p2 = floor(length(index_show) / 2);
%for kk = 1 : p2
%    ii = index_show(kk);
%    jj = index_show(kk + p2);
%    fprintf(fid, '%4i  %9.3f  %8.3f  %8.3f  |  %4i  %9.3f  %8.3f  %8.3f\r\n', ...
%            [pole_num(ii); z_peak_no_ends(ii); phaserr_shim_dgap_no_ends(ii) * 1.e6; ...
%            phaserr_shim_dx_no_ends(ii) * 1.e3; ...
%            pole_num(jj); z_peak_no_ends(jj); phaserr_shim_dgap_no_ends(jj) * 1.e6; ...
%            phaserr_shim_dx_no_ends(jj) * 1.e3]);
%end
p2 = ceil(length(index_show) / 2);
for jj = 1 : p2
    ii = index_show(jj);
    str{jj} = sprintf('%4i  %9.3f  %8.3f  %8.3f  |  ', ...
            [pole_num(ii); z_peak_no_ends(ii); phaserr_shim_dgap_no_ends(ii) * 1.e6; ...
            phaserr_shim_dx_no_ends(ii) * 1.e3]);
end
for jj = 1 : length(index_show) - p2
    ii = index_show(jj + p2);
    stradd{jj} = sprintf('%4i  %9.3f  %8.3f  %8.3f', ...
            [pole_num(ii); z_peak_no_ends(ii); phaserr_shim_dgap_no_ends(ii) * 1.e6; ...
            phaserr_shim_dx_no_ends(ii) * 1.e3]);
    str{jj} = [str{jj}, stradd{jj}];
end
for jj = 1 : p2
    s = str{jj};
    fprintf(fid, '%s\r\n', s);
end

%Close the data file
fclose(fid);

%Done
