%**************************************************************************
%This function writes the calculated combined first and second
%field integral data to a file.
%
%Input:
%dat_file, data file for results
%param, parameter structure
%pos, position of measurement (m)
%integ_1, first field integral at each position (Tm or Tm^2)
%integ_2, second field integral at each position (Tm or Tm^2)
%integ_combine, combination of first and second field integral at each position (Tm or Tm^2)
%
%Zachary Wolf, 9/6/08

function [] = long_coil_dat_combine(dat_file, param, pos, integ_1, integ_2, integ_combine)

%Initialize
if strcmp(param.meas_type, 'I1YVSX')  %Iy vs x
    pos_str = 'X';
    integ_str = 'By';
elseif strcmp(param.meas_type, 'I2YVSX')
    pos_str = 'X';
    integ_str = 'By';
elseif strcmp(param.meas_type, 'I1XVSX')  %Ix vs x
    pos_str = 'X';
    integ_str = 'Bx';
elseif strcmp(param.meas_type, 'I2XVSX')
    pos_str = 'X';
    integ_str = 'Bx';
elseif strcmp(param.meas_type, 'I1YVSY')  %Iy vs y
    pos_str = 'Y';
    integ_str = 'By';
elseif strcmp(param.meas_type, 'I2YVSY')
    pos_str = 'Y';
    integ_str = 'By';
elseif strcmp(param.meas_type, 'I1XVSY')  %Ix vs y
    pos_str = 'Y';
    integ_str = 'Bx';
elseif strcmp(param.meas_type, 'I2XVSY')
    pos_str = 'Y';
    integ_str = 'Bx';
else                                       %Other
    pos_str = '?';
    integ_str = 'B?';
end

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

%Write the field integrals to the data file
fprintf(fid, '\r\n\r\n\r\n');
fprintf(fid, 'Calculated Second Field Integral\r\n');
fprintf(fid, '\r\n');
fprintf(fid, 'Input Files:\r\n');
fprintf(fid, 'I1 File = %s\r\n', [param.i1_input_file_dir, param.i1_input_file_name]);
fprintf(fid, 'I2 File = %s\r\n', [param.i2_input_file_dir, param.i2_input_file_name]);
fprintf(fid, '\r\n');
fprintf(fid, 'Calculation:\r\n');
fprintf(fid, 'I_2_calc = I_2 - (z_coil_end - z_und_exit) * I_1\r\n');
fprintf(fid, 'z_coil_end = %f m\r\n', param.coil_length);
fprintf(fid, 'z_und_exit = %f m\r\n', param.und_end_pos);
fprintf(fid, '\r\n');
fprintf(fid, 'Meas    %s Pos      I_1_%s      I_2_%s     I_2_calc \r\n', pos_str, integ_str, integ_str);
fprintf(fid, ' #       (m)        (Tm)       (Tm^2)      (Tm^2)  \r\n');
fprintf(fid, '----  ---------  ----------  ----------  ----------\r\n');
meas_num = 1 : length(pos);
fprintf(fid, '%4i  %9.6f  %10.6f  %10.6f  %10.6f\r\n', [meas_num; pos; integ_1; integ_2; integ_combine]);

%Close the data file
fclose(fid);

%Done
