%**************************************************************************
%This function writes the shim parameter information to the data file.
%
%Input:
%dat_file, data file for results
%shim_param, structure containing the shim parameter information
%
%Zachary Wolf, 1/13/06

function [] = pid_shim_dat_param(dat_file, shim_param)

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

%Write the parameter information to the data file
fprintf(fid, '\r\n');
fprintf(fid, 'Set Gap Shim Parameters:\r\n');
fprintf(fid, 'Shim range = %10.2e m\r\n', shim_param.setgap_shim_range);
fprintf(fid, 'Shim sort bin size = %10.2e m\r\n', shim_param.setgap_shim_bin_size);

fprintf(fid, '\r\n');
fprintf(fid, 'Trajectory Shim Parameters:\r\n');
fprintf(fid, 'Number of segments in the segmented trajectory = %i\r\n', shim_param.traj_num_seg);
fprintf(fid, 'Maximum trajectory shift when a shim is moved = %11.3e m\r\n', shim_param.traj_shift_max);
fprintf(fid, 'Maximum trajectory shift accumulation before add shim = %11.3e m\r\n', shim_param.traj_shift_accum_max);
fprintf(fid, 'X trajectory end shim pole number = %i\r\n', shim_param.x_traj_end_shim_pole_num);
fprintf(fid, 'Y trajectory end shim pole number = %i\r\n', shim_param.y_traj_end_shim_pole_num);
fprintf(fid, 'Experimental parameters\r\n');
fprintf(fid, 'Let s = desired trajectory slope change (dimensionless)\r\n');
fprintf(fid, 'Let d = distance between the shim and the pole (m)\r\n');
fprintf(fid, 'd = p1 * s^2 + p2 * s + p3\r\n');
fprintf(fid, 'Type < 0 shims are reserved for the end shims.\r\n');
fprintf(fid, 'Type > 0 shims are reserved for the undulator center shims.\r\n');
fprintf(fid, '1)X Trajectory Shim Parameters\r\n');
[r, c] = size(shim_param.x_traj_shim_par);
for ii = 1 : r
    fprintf(fid, 'Type = %i\r\n', shim_param.x_traj_shim_par(ii, 1));
    fprintf(fid, '    Slope_min = %11.3e, Slope_max = %11.3e\r\n', shim_param.x_traj_shim_par(ii, 2), shim_param.x_traj_shim_par(ii, 3));
    fprintf(fid, '    p1 = %11.3e, p2 = %11.3e, p3 = %11.3e\r\n', shim_param.x_traj_shim_par(ii, 4), shim_param.x_traj_shim_par(ii, 5), shim_param.x_traj_shim_par(ii, 6));
end
fprintf(fid, '2)Y Trajectory Shim Parameters\r\n');
[r, c] = size(shim_param.y_traj_shim_par);
for ii = 1 : r
    fprintf(fid, 'Type = %i\r\n', shim_param.y_traj_shim_par(ii, 1));
    fprintf(fid, '    Slope_min = %11.3e, Slope_max = %11.3e\r\n', shim_param.y_traj_shim_par(ii, 2), shim_param.y_traj_shim_par(ii, 3));
    fprintf(fid, '    p1 = %11.3e, p2 = %11.3e, p3 = %11.3e\r\n', shim_param.y_traj_shim_par(ii, 4), shim_param.y_traj_shim_par(ii, 5), shim_param.y_traj_shim_par(ii, 6));
end

fprintf(fid, '\r\n');
fprintf(fid, 'Phase Shim Parameters:\r\n');
fprintf(fid, 'Number of segments in the segmented phase error = %i\r\n', shim_param.phaserr_num_seg);
fprintf(fid, 'Maximum positive phase error change before a shim is added = %11.3e rad\r\n', shim_param.phaserr_pos_delta_max);
fprintf(fid, 'Maximum negative phase error change before a shim is added = %11.3e rad\r\n', shim_param.phaserr_neg_delta_max);
fprintf(fid, 'Gap shim effective width = %11.3e m\r\n', shim_param.phaserr_gap_shim_width);
fprintf(fid, 'Nominal undulator gap = %11.3e m\r\n', shim_param.phaserr_gap_shim_nom_gap);
fprintf(fid, 'Experimental parameters\r\n');
fprintf(fid, 'Gap shim correction factor = %11.3e\r\n', shim_param.phaserr_gap_shim_corr_fact);
fprintf(fid, 'Let c = desired phase error change (rad)\r\n');
fprintf(fid, 'Let t = shim thickness (m)\r\n');
fprintf(fid, 't = p1 * c^2 + p2 * c + p3\r\n');
[r, c] = size(shim_param.phaserr_phase_shim_par);
for ii = 1 : r
    fprintf(fid, 'Type = %i\r\n', shim_param.phaserr_phase_shim_par(ii, 1));
    fprintf(fid, '    phase_change_min = %11.3e, phase_change_max = %11.3e\r\n', shim_param.phaserr_phase_shim_par(ii, 2), shim_param.phaserr_phase_shim_par(ii, 3));
    fprintf(fid, '    p1 = %11.3e, p2 = %11.3e, p3 = %11.3e\r\n', shim_param.phaserr_phase_shim_par(ii, 4), shim_param.phaserr_phase_shim_par(ii, 5), shim_param.phaserr_phase_shim_par(ii, 6));
end

fprintf(fid, '\r\n');
fprintf(fid, 'K Shim Parameters:\r\n');
fprintf(fid, 'Cant angle = %11.3e rad\r\n', shim_param.k_shim_cant_angle);

%Close the data file
fclose(fid);

%Done
