%  Quad 4078 main coil calibration for dipole signal 

cd('c:\Magnet_Data\LCLS-II\Quad\4078\Coil Calibration');

% Wire Measurements
Wire_10 = Load_wireplt_dev(10); % H-Trim
Wire_11 = Load_wireplt_dev(11); % V-Trim

% Coil Measurements
Str_12 = Load_strplt_dev(12); % H-Trim
Str_13 = Load_strplt_dev(13); % V-Trim

% Remove zero field dipole
H_trim_wire_plus = mean(Wire_10.SL(3:4)) - mean(Wire_10.SL(1:2));
H_trim_wire_minus = abs(mean(Wire_10.SL(5:6)) - mean(Wire_10.SL(1:2)));

[X, H_trim_coil_zero1] = r_theta_to_x_y(Str_12.SL(1), Str_12.THsp(1));
[X, H_trim_coil_zero2] = r_theta_to_x_y(Str_12.SL(6), Str_12.THsp(6));
[X, H_trim_coil_plus_1] = r_theta_to_x_y(Str_12.SL(2:3), Str_12.THsp(2:3));
[X, H_trim_coil_minus_1] = r_theta_to_x_y(Str_12.SL(4:5), Str_12.THsp(4:5));

H_trim_coil_plus = mean(H_trim_coil_plus_1 - mean([H_trim_coil_zero1]));
H_trim_coil_minus = mean(H_trim_coil_minus_1 - mean([H_trim_coil_zero1]));

old_constant = 937.74436 % cc from self seeding dipoles 

h_trim_cc_plus = abs(H_trim_wire_plus/H_trim_coil_plus)*old_constant 
h_trim_cc_minus = abs(H_trim_wire_minus/H_trim_coil_minus)*old_constant

V_trim_wire_plus = mean(Wire_11.SL(3:4)) - mean(Wire_11.SL(1:2));
V_trim_wire_minus = abs(mean(Wire_11.SL(5:6)) - mean(Wire_11.SL(1:2)));

[V_trim_coil_zero1, Y] = r_theta_to_x_y(Str_13.SL(1), Str_13.THsp(1));
[V_trim_coil_zero2, Y] = r_theta_to_x_y(Str_13.SL(6), Str_13.THsp(6));
[V_trim_coil_plus_1, Y] = r_theta_to_x_y(Str_13.SL(2:3), Str_13.THsp(2:3));
[V_trim_coil_minus_1, Y] = r_theta_to_x_y(Str_13.SL(4:5), Str_13.THsp(4:5));

V_trim_coil_plus = mean(V_trim_coil_plus_1 - mean([V_trim_coil_zero1]));
V_trim_coil_minus = mean(V_trim_coil_minus_1 - mean([V_trim_coil_zero1]));

V_trim_cc_plus = abs(V_trim_wire_plus/V_trim_coil_plus)*old_constant 
V_trim_cc_minus = abs(V_trim_wire_minus/V_trim_coil_minus)*old_constant

new_cc = mean([h_trim_cc_plus,h_trim_cc_minus,V_trim_cc_plus,V_trim_cc_minus])

ratio_new_old_cc = mean([h_trim_cc_plus,h_trim_cc_minus,V_trim_cc_plus,V_trim_cc_minus])/old_constant