% Load and Plot SXU Slope data.  
  cd('C:\Users\sda\OneDrive - SLAC National Accelerator Laboratory\Magnet_Data\LCLS-II\Radiation_Detection_System\Slope Plots');

load('SXU_26_45_Slope.mat');

% Put data into structure
for i = 1:19
   
    if i < 10
     Slope(i).name = eval(['archive_00000' num2str(i),'_name']);
     Slope(i).time_hr = eval(['archive_00000' num2str(i),'_time'])/3600;
     Slope(i).slope = eval(['archive_00000' num2str(i),'_value']);
     Slope(i).start_time = eval(['archive_00000' num2str(i),'_starttime']);
    else
     Slope(i).name = eval(['archive_0000' num2str(i),'_name']);
     Slope(i).time_hr = eval(['archive_0000' num2str(i),'_time'])/3600;
     Slope(i).slope = eval(['archive_0000' num2str(i),'_value']);
     Slope(i).start_time = eval(['archive_0000' num2str(i),'_starttime']);

    end
end
    load('SXU_46_47_Slope.mat');

% Put data into structure
for j = 1:2
     i = j + 19;
     Slope(i).name = eval(['archive_00000' num2str(j),'_name']);
     Slope(i).time_hr = eval(['archive_00000' num2str(j),'_time'])/3600;
     Slope(i).slope = eval(['archive_00000' num2str(j),'_value']);
     Slope(i).start_time = eval(['archive_00000' num2str(j),'_starttime']);
end

% Import file with # of days from the 1st SXU Tunnel run to the latest one

RD = importdata('SXU Tunnel Run Days.txt', '\t');

runs = RD.data(:,1);
days = RD.data(:,2);
hours = days*24 + 1;

% Find Slope data on each day of measurements

 for i = 1:21
  for j = 1:length(runs)
      Slope(i).run_slope(j) = mean(filloutliers([Slope(i).slope(hours(j):hours(j)+8)], 'center', 'mean'));
      Slope(i).std_run_slope(j) = std(filloutliers([Slope(i).slope(hours(j):hours(j)+8)], 'center', 'mean'));
      Slope(i).run(j) = runs(j);
  end
 end

 % clear the archive data, because there is so much of it and it is not
 % needed anymore.
  clear archive*

  % Plot Slope data into directory
  
  for i = 1:21
      C = strsplit(Slope(i).name, ':');
      cell = C{3}(1:2);
      errorbar(Slope(i).run, Slope(i).run_slope*1000, Slope(i).std_run_slope*1000, 'bo');
      xlabel('Run #')
      ylabel('Slope (micron)');
      title(['Slope vs Run #, for SXU Cell ', cell]);
      saveas(gcf,['Slope vs Run # for SXU Cell ', cell, '.png']);

  end
  close all

