function Experiment_constant = Coil_Constant_Calc(wire_field, coil_field, field_units, old_constant, radius, turns, imag, mag_name, mag_order, num_coil_coils, coil_name)

%This function calculates the New Coil constant as well as an expected
%coil constant using the old constant, radius of the coil number of turns
%and the order of the magnet. This program works for dipole through sextupole Order should be given in integer form

% Inputs:
% wire_field - integrated field strength or preferably integrated field strength over current (transfer function)
%              measured by the stretched wire.  You must use the same field units for the coil measurement. 
% coil_field - integrated field strength or preferably integrated field strength over current (transfer function) 
%              measured by the rotating coil 

% field_units - units of wire_field & coil_field as a string i.e. 'T-m' (integrated field strength) or 'T-m/A' for dipole.
%               'T' (integrated field gradient) or 'T/A' for quad, etc, for documentation
% old_constant - the coil constant used for the coil_field, found in file coilpar.ru#
% radius, - the radius used for the coil_field, found in file coilpar.ru#
% turns - the number of turns for the coil used for the coil_field, found in file coilpar.ru#
% imag - the average current used for the measurements.  Only used for documentation.
% mag_name -  name of magnet, for documentation
% mag_order - 1 = Dipole, 2 = Quad, 3 = Sext, 4 = Octupole.
% num_coil_coils - 'number of coils used.  1 if just the A (main) coil, 2 if
% using A and B coils. for 
% coil_name -  name of coil as a string ie 'DC55_add'  for documentation

% Output
% Experiment_constant  (New Calibrated Coil Constant in file) is the new coil constant to be used in the coil file. 

%Dipole = 1
%Quadrupole = 2
%Sextupole = 3
%Octupole = 4

Experiment_constant = (wire_field/coil_field)*old_constant;

if mag_order == 1
    Calculated_constant = 1/(radius*turns)/num_coil_coils;
        new_radius = (1/num_coil_coils)/(Experiment_constant*turns);
    mag_type = 'Dipole';
elseif mag_order == 2 
    Calculated_constant = (factorial(mag_order))/(radius^2*turns)/num_coil_coils;
           new_radius = sqrt((factorial(mag_order)/num_coil_coils)/(Experiment_constant*turns));
    mag_type = 'Quadrupole';
elseif mag_order == 3 
    Calculated_constant = (factorial(mag_order))/(radius^3*turns)/num_coil_coils;
     new_radius = power((factorial(mag_order)/num_coil_coils)/(Experiment_constant*turns),1/3)
    mag_type = 'Sextupole';
elseif mag_order == 4 
    Calculated_constant = (factorial(mag_order))/(radius^3*turns)/num_coil_coils;
           new_radius = power((factorial(mag_order)/num_coil_coils)/(Experiment_constant*turns),1/4);
    mag_type = 'Octupole';
else
    Calculated_constant = 0;
    
end

data_file = ['Coil Constant ',coil_name,'.txt'];   
    title = ['Coil Constant for ',coil_name,' using magnet ' mag_name, '\n\n' ];
    
fidC = fopen(data_file,'w');

fprintf(fidC,title);
fprintf(fidC,'New Calibrated Coil Constant = %8.5f, \n',Experiment_constant);
fprintf(fidC,'File coilpar.ru (Old) Coil Constant = %8.5f  \n',  old_constant);
fprintf(fidC,'New Constant/Old Constant = %6.4f, \n\n', (Experiment_constant/old_constant));
fprintf(fidC,'Old Coil Radius = %9.7f,   New Radius = %9.7f, Number of coils = %2i (Only change radius if Number of coils = 1!) \n\n',radius, new_radius, num_coil_coils);
fprintf(fidC,'Wire Strength = %6.4f %s,  Coil Strength = %6.4f %s, \n\n', wire_field, field_units, coil_field, field_units);

fprintf(fidC,'Magnet Type is %s at Imag = %5.3f for coil. Number of Coil Turns = %3.0f \n', mag_type ,imag, turns);

fprintf(fidC,'Coil Constant Calculated from radius, turns and magnet order = %6.4f  (Should be within 5%% of Calibrated Coil Constant) \n', Calculated_constant);

fprintf('Old Coil Constant = %6.4f \n', old_constant);
fprintf('Calibrated Coil Constant = %6.4f \n', Experiment_constant);
fprintf('Calculated Coil Constant =  %6.4f \n', Calculated_constant);


fclose(fidC);

