%**************************************************************************
%This program reads voltage measurements from a two Hall probe array.  It
%applies a calibration to get field values.  It uses the measurements to
%calculate the fields on the beam axis.  It outputs the fields on the beam
%axis to a file which can be read by an analysis program.
%
%param, structure containing program parameters
% param.input_file_dir = INPUT_FILE_DIR;
% param.input_file_name = INPUT_FILE_NAME;
% param.input_file_type = INPUT_FILE_TYPE;
% param.align_probe_pos_file_name = ALIGN_PROBE_POS_FILE_NAME;
% param.align_z_axis_enter = ALIGN_Z_AXIS_ENTER;
% param.align_z_axis_exit = ALIGN_Z_AXIS_EXIT;
% param.und_pol_mode = UND_POL_MODE;
% param.und_period_nom = UND_PERIOD_NOM;
% param.num_end_poles = NUM_END_POLES;
% param.probe1_bx_calib_file = PROBE1_BX_CALIB_FILE;
% param.probe1_by_calib_file = PROBE1_BY_CALIB_FILE;
% param.probe2_bx_calib_file = PROBE2_BX_CALIB_FILE;
% param.probe2_by_calib_file = PROBE2_BY_CALIB_FILE;
% param.num_zero_gauss_meas = NUM_ZERO_GAUSS_MEAS;
% param.x1_minus_xc_nom = X1_MINUS_XC_NOM;
% param.y1_minus_yc_nom = Y1_MINUS_YC_NOM;
% param.kx_nom = KX_NOM;
% param.ky_nom = KY_NOM;
% param.ku_nom = KU_NOM;
% param.probe2_offset_dir = PROBE2_OFFSET_DIR;
% param.probe1_param = probe1_param;
% param.probe2_param = probe2_param;
%
%b_meas, structure containing measurement information and calculated fields
% b_meas.zm = zm;
% b_meas.vx1m = vx1m;
% b_meas.vy1m = vy1m;
% b_meas.vz1m = vz1m;
% b_meas.vx2m = vx2m;
% b_meas.vy2m = vy2m;
% b_meas.vz2m = vz2m;
% b_meas.zv = zv;
% b_meas.vx1 = vx1;
% b_meas.vy1 = vy1;
% b_meas.vz1 = vz1;
% b_meas.vx2 = vx2;
% b_meas.vy2 = vy2;
% b_meas.vz2 = vz2;
% b_meas.z = z;
% b_meas.bx1 = bx1;
% b_meas.by1 = by1;
% b_meas.bz1 = bz1;
% b_meas.bx2 = bx2;
% b_meas.by2 = by2;
% b_meas.bz2 = bz2;
%
%field, structure containing the field analysis results
% field.z = z;
% field.bx1 = bx1;
% field.by1 = by1;
% field.bz1 = bz1;
% field.bx2 = bx2;
% field.by2 = by2;
% field.bz2 = bz2;
% field.field_peaks_zeros = field_peaks_zeros;
% field.lambda_u = lambda_u;
% field.z_peak_ctr = z_peak_ctr;
% %Linear polarization, vertical field
% field.by1_peak_ne = by1_peak_ne;
% field.dbx_at_by1_peak_ne = dbx_at_by1_peak_ne;
% field.dby_at_by1_peak_ne = dby_at_by1_peak_ne;
% field.bz1_at_by1_zero_ne = bz1_at_by1_zero_ne;
% field.bz2_at_by2_zero_ne = bz2_at_by2_zero_ne;
% %Linear polarization, horizontal field
% field.bx1_peak_ne = bx1_peak_ne;
% field.dby_at_bx1_peak_ne = dby_at_bx1_peak_ne;
% field.dbx_at_bx1_peak_ne = dbx_at_bx1_peak_ne;
% field.bz1_at_bx1_zero_ne = bz1_at_bx1_zero_ne;
% field.bz2_at_bx1_zero_ne = bz2_at_bx1_zero_ne;
% %Circular polarization
% field.bx1_peak_ne = bx1_peak_ne;
% field.by1_peak_ne = by1_peak_ne;
% field.bx2_peak_ne = bx2_peak_ne;
% field.by2_peak_ne = by2_peak_ne;
% field.bz1_at_bx1_peak_ne = bz1_at_bx1_peak_ne;
% field.bz1_at_by1_peak_ne = bz1_at_by1_peak_ne;
% field.bz2_at_bx2_peak_ne = bz2_at_bx2_peak_ne;
% field.bz2_at_by2_peak_ne = bz2_at_by2_peak_ne;
%
%field_peaks_zeros, structure containing the field peak and zero analysis results
% field_peaks_zeros.bx1_peak = bx1_peak;
% field_peaks_zeros.z_peak_bx1 = z_peak_bx1;
% field_peaks_zeros.z_zero_bx1 = z_zero_bx1;
% field_peaks_zeros.by1_peak = by1_peak;
% field_peaks_zeros.z_peak_by1 = z_peak_by1;
% field_peaks_zeros.z_zero_by1 = z_zero_by1;
% field_peaks_zeros.bz1_peak = bz1_peak;
% field_peaks_zeros.z_peak_bz1 = z_peak_bz1;
% field_peaks_zeros.z_zero_bz1 = z_zero_bz1;
% field_peaks_zeros.bx2_peak = bx2_peak;
% field_peaks_zeros.z_peak_bx2 = z_peak_bx2;
% field_peaks_zeros.z_zero_bx2 = z_zero_bx2;
% field_peaks_zeros.by2_peak = by2_peak;
% field_peaks_zeros.z_peak_by2 = z_peak_by2;
% field_peaks_zeros.z_zero_by2 = z_zero_by2;
% field_peaks_zeros.bz2_peak = bz2_peak;
% field_peaks_zeros.z_peak_bz2 = z_peak_bz2;
% field_peaks_zeros.z_zero_bz2 = z_zero_bz2;
% field_peaks_zeros.bx1_peak_ne = bx1_peak_ne;
% field_peaks_zeros.z_peak_bx1_ne = z_peak_bx1_ne;
% field_peaks_zeros.z_zero_bx1_ne = z_zero_bx1_ne;
% field_peaks_zeros.by1_peak_ne = by1_peak_ne;
% field_peaks_zeros.z_peak_by1_ne = z_peak_by1_ne;
% field_peaks_zeros.z_zero_by1_ne = z_zero_by1_ne;
% field_peaks_zeros.bz1_peak_ne = bz1_peak_ne;
% field_peaks_zeros.z_peak_bz1_ne = z_peak_bz1_ne;
% field_peaks_zeros.z_zero_bz1_ne = z_zero_bz1_ne;
% field_peaks_zeros.bx2_peak_ne = bx2_peak_ne;
% field_peaks_zeros.z_peak_bx2_ne = z_peak_bx2_ne;
% field_peaks_zeros.z_zero_bx2_ne = z_zero_bx2_ne;
% field_peaks_zeros.by2_peak_ne = by2_peak_ne;
% field_peaks_zeros.z_peak_by2_ne = z_peak_by2_ne;
% field_peaks_zeros.z_zero_by2_ne = z_zero_by2_ne;
% field_peaks_zeros.bz2_peak_ne = bz2_peak_ne;
% field_peaks_zeros.z_peak_bz2_ne = z_peak_bz2_ne;
% field_peaks_zeros.z_zero_bz2_ne = z_zero_bz2_ne;
%
%mag_ctr, structure containing results from the magnetic center analysis
% mag_ctr.ku = ku;
% mag_ctr.z_peak_ctr = z_peak_ctr;
% mag_ctr.x1_minus_xc = x1_minus_xc;
% mag_ctr.y1_minus_yc = y1_minus_yc;
% mag_ctr.kx = kx;
% mag_ctr.ky = ky;
% mag_ctr.delta_x_calc = delta_x_calc;
% mag_ctr.delta_y_calc = delta_y_calc;
%
%beam_axis, structure containing results from the beam axis analysis
% beam_axis.z_probe = z_probe;
% beam_axis.x_probe = x_probe;
% beam_axis.y_probe = y_probe;
% beam_axis.z_peak_ctr = z_peak_ctr;
% beam_axis.xc = xc;
% beam_axis.yc = yc;
% beam_axis.xb = xb;
% beam_axis.yb = yb;
% beam_axis.z = z;
% beam_axis.xb_fit = xb_fit;
% beam_axis.yb_fit = yb_fit;
% beam_axis.xc_fit = xc_fit;
% beam_axis.yc_fit = yc_fit;
% beam_axis.x1_minus_xc_fit = x1_minus_xc_fit;
% beam_axis.y1_minus_yc_fit = y1_minus_yc_fit;
% beam_axis.bx1_corr = bx1_corr;
% beam_axis.by1_corr = by1_corr;
% beam_axis.bz1_corr = bz1_corr;
% beam_axis.bx_b = bx_b;
% beam_axis.by_b = by_b;
% beam_axis.bz_b = bz_b;
%
%Zachary Wolf
%5/19/13

function [] = hall2_anal()

%Clear the workspace
clear

%Loop
%for ii = 1 : 11
for ii = 1 : 1
%for ii = 1 : 88
%for ii = 1 : 260
%for ii = 1 : 44
%for ii = 1 : 80
%for ii = 11 + [1, 2, 3, 4, 6, 8, 10]
close all force;

%Get the program parameters
% [param] = hall2_anal_param(ii);
[param] = hall2_anal_param_2015_meas(ii);

%Make file names
dat_file = strcat(lower(param.input_file_dir), 'hall2_anal_ctr.dat');   %Output data file
plt_file = strcat(lower(param.input_file_dir), 'hall2_anal_ctr.ps');    %Output plot file
mat_file = strcat(lower(param.input_file_dir), 'hall2_anal_ctr.mat');   %Output file to save results
% probe1_field_dat_file = strcat(lower(param.input_file_dir), 'hall2_anal_probe1_field.dat');   %Output data file
% probe2_field_dat_file = strcat(lower(param.input_file_dir), 'hall2_anal_probe2_field.dat');   %Output data file
%meas_field_dat_file = strcat(lower(param.input_file_dir), 'hall2_anal_meas_field_ctr.dat');   %Output data file
%nom_beam_axis_field_dat_file = strcat(lower(param.input_file_dir), 'hall2_anal_nom_beam_axis_field_ctr.dat');   %Output data file
beam_axis_field_dat_file = strcat(lower(param.input_file_dir), 'hall2_anal_beam_axis_field_ctr.dat');   %Output data file

%Initialize the data file
util_dat_init(dat_file);
% util_dat_init(probe1_field_dat_file);
% util_dat_init(probe2_field_dat_file);
%util_dat_init(meas_field_dat_file);
%util_dat_init(nom_beam_axis_field_dat_file);
util_dat_init(beam_axis_field_dat_file);

%Write a message to the data file
msg{1} = ' ';
msg{2} = 'Hall Probe Array Analysis';
msg{3} = '*************************';
util_dat_msg(dat_file, msg);
% util_dat_msg(probe1_field_dat_file, msg);
% util_dat_msg(probe2_field_dat_file, msg);
%util_dat_msg(meas_field_dat_file, msg);
%util_dat_msg(nom_beam_axis_field_dat_file, msg);
util_dat_msg(beam_axis_field_dat_file, msg);

%Write the parameter values to the data file
hall2_anal_dat_param(dat_file, param);

%Initialize the plot file
hall2_anal_plt_init(plt_file, msg, param);

%Get the probe position
[probe_pos] = hall2_anal_get_probe_pos(param);

%Plot the probe position
hall2_anal_plot_x_probe_pos(plt_file, 1 : length(probe_pos.x), probe_pos.x);
hall2_anal_plot_y_probe_pos(plt_file, 1 : length(probe_pos.y), probe_pos.y);

%Get the measured data
[header, temp, zm, vx1m, vy1m, vz1m, vx2m, vy2m, vz2m] = hall2_anal_get_meas(param);
%hall2_meas_plot_zshift_vxyz(zm, vx1m, vy1m, vz1m, vx2m, vy2m, vz2m);

%Find the correction to the linear scale
samp_num = 1 : length(probe_pos.z);
z_corr = probe_pos.z - 100.e-6 * (samp_num - 1);  %probe_pos.z starts at 0
meas_range = param.num_zero_gauss_meas + 1 : length(zm) - param.num_zero_gauss_meas;
zm(meas_range) = zm(meas_range) + z_corr;

%Plot the correction to the linear scale
hall2_anal_plot_dz_probe_pos(plt_file, samp_num, z_corr);

%Apply calibrations to the data
[b_meas] = hall2_anal_calib_meas(param, dat_file, plt_file, zm, vx1m, vy1m, vz1m, vx2m, vy2m, vz2m);

% %Debug, use fake data
% [probe1_param, probe2_param] = hall2_anal_gen_fake_probe_param();
% param.probe1_param = probe1_param;
% param.probe2_param = probe2_param;
% [header, temp, b_meas, probe_pos] = hall2_anal_gen_fake_data(param);

%***********************

% %Extract the fields from the two probes
% z = b_meas.z;
% bx1 = b_meas.bx1;
% by1 = b_meas.by1;
% bz1 = b_meas.bz1;
% bx2 = b_meas.bx2;
% by2 = b_meas.by2;
% bz2 = b_meas.bz2;
% 
% %Write the fields from the two probes to the measured data files
% hall2_anal_dat_zbxyz(probe1_field_dat_file, z, bx1, by1, bz1);
% hall2_anal_dat_zbxyz(probe2_field_dat_file, z, bx2, by2, bz2);
% %hall2_anal_dat_zbxyz(meas_field_dat_file, z1, bx1, by1, bz1);
% 
% %Analyze the measured field, find the field peaks
% [field] = hall2_anal_probe_field(param, dat_file, plt_file, b_meas);
% 
% %Output the results to the mat file
% mag_ctr = 0;
% beam_axis = 0;
% save(mat_file, 'param', 'header', 'temp', 'b_meas', 'field', 'mag_ctr', 'beam_axis');
% 
% % %Display
% % bx1_ave = mean(abs(field.bx1_peak_ne))
% % by1_ave = mean(abs(field.by1_peak_ne))
% % bx2_ave = mean(abs(field.bx2_peak_ne))
% % by2_ave = mean(abs(field.by2_peak_ne))
% 
% %Exit here if only want the measured fields
% %continue;
% %return;

%***********************

% %Find the fields on the nominal beam axis
% [nom_beam_axis] = hall2_anal_beam_axis_given(param, dat_file, plt_file, b_meas, probe_pos);
% 
% %Extract the fields on the nominal beam axis
% zb_nom = nom_beam_axis.z;
% bxb_nom = nom_beam_axis.bx_b;
% byb_nom = nom_beam_axis.by_b;
% bzb_nom = nom_beam_axis.bz_b;
% 
% %Write the fields on the nominal beam axis to the data file
% hall2_anal_dat_zbxyz(nom_beam_axis_field_dat_file, zb_nom, bxb_nom, byb_nom, bzb_nom);
% 
% % %Exit here if only want the fields on the nominal beam axis
% % return;

%***********************

%Analyze the measured field, find the field peaks
[field] = hall2_anal_field(param, dat_file, plt_file, b_meas);

%Find the magnetic center
[mag_ctr] = hall2_anal_mag_ctr(param, dat_file, plt_file, field);

%Find the beam axis
[beam_axis] = hall2_anal_beam_axis(param, dat_file, plt_file, b_meas, field, mag_ctr, probe_pos);

%Extract the fields on the beam axis
zb = beam_axis.z;
bxb = beam_axis.bx_b;
byb = beam_axis.by_b;
bzb = beam_axis.bz_b;

%Write the fields to the data file
hall2_anal_dat_zbxyz(beam_axis_field_dat_file, zb, bxb, byb, bzb);

% %Debug
% num2str(max(by1), 8)
% num2str(max(byb), 8)

%Output the results to the mat file
save(mat_file, 'param', 'header', 'temp', 'b_meas', 'field', 'mag_ctr', 'beam_axis');

%Loop
end

%Done
