% Q4078 Analysis

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

 %  Get int str (wire) and load it
   Load_strplt_dev(1); % Integrated gradient -6 to 6 to -6 A. Harmonics at -4.5 and 4.5 amps.
   Load_ctrdat_dev(2); % Center and harmonics at currents -4.5 to -5.5 to -3.5 and 4.5 to 5.5 to 3.5. Magnet warm
    Load_ctrdat_dev(3); % Center and harmonics at currents -4.5 to -5.5 to -3.5 and 4.5 to 5.5 to 3.5. Mirror plates removed and replaced. Magnet warm
     Load_ctrdat_dev(4); % Center and harmonics at currents -4.5 to -5.5 to -3.5 and 4.5 to 5.5 to 3.5. Magnet warm Mirror plates off
     Load_ctrdat_dev(10); %  Center and harmonics at currents -4.5 to -5.5 to -3.5 and 4.5 to 5.5 to 3.5.  Mirror plates on.  5 hour warmup. 
       Load_ctrdat_dev(11); %  Center and harmonics at currents -4.5 to -5.5 to -3.5 and 4.5 to 5.5 to 3.5.  Mirror plates switched upstream and downstream  
     
       runs = [2,3,4,10,11];
       first_x_center = [ctrdat2.Xcenter(1)-ctrdat2.Xcenter(1), ctrdat3.Xcenter(1)-ctrdat2.Xcenter(1), ctrdat4.Xcenter(1)-ctrdat2.Xcenter(1),ctrdat10.Xcenter(1)-ctrdat2.Xcenter(1), ctrdat11.Xcenter(1)-ctrdat2.Xcenter(1)];
       last_x_center = [ctrdat2.Xcenter(end)-ctrdat2.Xcenter(end), ctrdat3.Xcenter(end)-ctrdat2.Xcenter(end), ctrdat4.Xcenter(end)-ctrdat2.Xcenter(end), ctrdat10.Xcenter(end)-ctrdat2.Xcenter(end), ctrdat11.Xcenter(end)-ctrdat2.Xcenter(end)];
       figure
       plot(runs, first_x_center, 'bo');
       hold
       plot(runs, last_x_center, 'r*');
       xlabel('Run Number');
       ylabel('X center difference to run 2 center (microns)');
        title('X center difference to run 2 center vs run number');
        legend('First -4.5A measurement', 'Last +4.5 A measurement')
       
       first_y_center = [ctrdat2.Ycenter(1)-ctrdat2.Ycenter(1), ctrdat3.Ycenter(1)-ctrdat2.Ycenter(1), ctrdat4.Ycenter(1)-ctrdat2.Ycenter(1), ctrdat10.Ycenter(1)-ctrdat2.Ycenter(1), ctrdat11.Ycenter(1)-ctrdat2.Ycenter(1)];
       last_y_center = [ctrdat2.Ycenter(end)-ctrdat2.Ycenter(end), ctrdat3.Ycenter(end)-ctrdat2.Ycenter(end),  ctrdat4.Ycenter(end)-ctrdat2.Ycenter(end), ctrdat10.Ycenter(end)-ctrdat2.Ycenter(end), ctrdat11.Ycenter(end)-ctrdat2.Ycenter(end)];
       figure
       plot(runs, first_y_center, 'bo');
       hold
       plot(runs, last_y_center, 'r*');
       xlabel('Run Number');
         ylabel('Y center difference to run 2 center (microns)');
        title('Y center difference to run 2 center vs run number');
            legend('First -4.5A measurement', 'Last +4.5 A measurement')
            
    % Find and plot integrated field for trims, runs 6 and 12 
    Load_strplt_dev(12);
    Load_strplt_dev(6);
    % Calculate the integral field by subtracting the field with the trims
    % at 0 A.  The Diff values are these numbers.
   [Mag_sum12, Ang_sum12, Mag_diff12, Ang_diff12] = sum_vector_components(Strplt12.SL,  Strplt12.THsp, Strplt12.SL(1), Strplt12.THsp(1));
   [Mag_sum6, Ang_sum6, Mag_diff6, Ang_diff6] = sum_vector_components(Strplt6.SL,  Strplt6.THsp, Strplt6.SL(1), Strplt6.THsp(1));
   % Change to X and Y components
   [X6, Y6] = r_theta_to_x_y(Mag_diff6, Ang_diff6); % Original
   [X12, Y12] = r_theta_to_x_y(Mag_diff12, Ang_diff12); % Remeasure 
   figure
   compass(X6,Y6, 'r');
   hold
   compass(X12,Y12, 'b');
  
