%**************************************************************************
%This function compares field integral measurements done with the long
%coil.
%
%Zachary Wolf, 4/17/09

function [] = long_coil_comp()

%Clear the workspace
clear

%Set parameter values by executing the parameter file
long_coil_comp_param

%Loop over measurement types
for ii = 1 : 4

%Select the measurement type
if ii == 1
    MEAS_TYPE = MEAS_TYPE_I1Y;  INPUT_FILE = INPUT_FILE_I1Y;
elseif ii == 2
    MEAS_TYPE = MEAS_TYPE_I2Y;  INPUT_FILE = INPUT_FILE_I2Y;
elseif ii == 3
    MEAS_TYPE = MEAS_TYPE_I1X;  INPUT_FILE = INPUT_FILE_I1X;
elseif ii == 4
    MEAS_TYPE = MEAS_TYPE_I2X;  INPUT_FILE = INPUT_FILE_I2X;
end

%Output files
dat_file = [OUTPUT_DIR, 'long_coil_comp_', MEAS_TYPE, '.dat'];
plt_file = [OUTPUT_DIR, 'long_coil_comp_', MEAS_TYPE, '.ps'];
mat_file = [OUTPUT_DIR, 'long_coil_comp_', MEAS_TYPE, '.mat'];

%Initialize the data file
util_dat_init(dat_file);
util_dat_msg(dat_file, cellstr(' '));
util_dat_msg(dat_file, cellstr('Field Integral Analysis Results:'));
util_dat_msg(dat_file, cellstr(COMPARE_NAME));

%Initialize the plots
long_coil_comp_plot_init(plt_file, COMPARE_NAME, INPUT_FILE);

%Loop over measurement results, get data
[r, c] = size(INPUT_FILE);
for ii = 1 : c

    %Get the long coil measurements
    input_file = INPUT_FILE{ii};
	[header, pos, integ] = long_coil_comp_read_meas(input_file);

    %Average measurements taken at the same position
    [pos_ave, integ_ave, integ_rms] = long_coil_calc_integ_ave(pos, integ);
    
    %Store array data
    pos_vals{:, ii} = pos;
    integ_vals{:, ii} = integ;
    pos_ave_vals{:, ii} = pos_ave;
    integ_ave_vals{:, ii} = integ_ave;
    integ_rms_vals{:, ii} = integ_rms;
    
    %Get the temperature
    [temp] = long_coil_comp_get_und_temp_meas(header);
    t_ave(ii) = temp.t_ave;
    
    %Get the date
	[dd, mm, yyyy] = util_get_date_header(header);
    date_vec = [yyyy, mm, dd];
    date_vec_vals(:, ii) = date_vec;
    date_num(ii) = datenum(date_vec);
    
    %Use the date as a dataset name
    INPUT_NAME{ii} = datestr(date_num(ii));
    
    %Get the dataset number from the file name
    index_val = strfind(input_file, 'DATASET00');
    dataset_num_str{ii} = input_file(index_val(1) + 9 : index_val(1) + 10);
    dataset_num(ii) = str2num(dataset_num_str{ii});
    
%End loop over measurements
end

%Write results
long_coil_comp_dat_integ_ave(dat_file, COMPARE_NAME, MEAS_TYPE, date_num, dataset_num, pos_ave_vals, integ_ave_vals, integ_rms_vals);

%Plot the data
long_coil_comp_plot_integ_ave(plt_file, MEAS_TYPE, INPUT_NAME, pos_ave_vals, integ_ave_vals, integ_rms_vals);

%Plot the data at x = 0 as a function of time
long_coil_comp_plot_integ_ave_at_pos(plt_file, MEAS_TYPE, INPUT_NAME, date_num, pos_ave_vals, integ_ave_vals, integ_rms_vals, 0.);

%Save the results to the mat file
save(mat_file, 'COMPARE_NAME', 'OUTPUT_DIR', 'INPUT_FILE', 'INPUT_NAME', 'date_num', 'pos_vals', 'integ_vals', 'pos_ave_vals', 'integ_ave_vals', 'integ_rms_vals', 't_ave');

%End loop over measurement types
end

%Done
