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

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

    end
end
    load('SXU_46_47_Offset.mat');

% Put data into structure
for j = 1:2
     i = j + 19;
     Offset(i).name = eval(['archive_00000' num2str(j),'_name']);
     Offset(i).time_hr = eval(['archive_00000' num2str(j),'_time'])/3600;
     Offset(i).offset = eval(['archive_00000' num2str(j),'_value']);
     Offset(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 Offset data on each day of measurements

 for i = 1:21
  for j = 1:length(runs)
      Offset(i).run_offset(j) = mean(filloutliers([Offset(i).offset(hours(j):hours(j)+8)], 'center', 'mean'));
      Offset(i).std_run_offset(j) = std(filloutliers([Offset(i).offset(hours(j):hours(j)+8)], 'center', 'mean'));
      Offset(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 Offset data into directory
  
  for i = 1:21
      C = strsplit(Offset(i).name, ':');
      cell = C{3}(1:2);
      errorbar(Offset(i).run, Offset(i).run_offset*1000, Offset(i).std_run_offset*1000, 'bo');
      xlabel('Run #')
      ylabel('Offset (micron)');
      title(['Offset vs Run #, for SXU Cell ', cell]);
      saveas(gcf,['Offset vs Run # for SXU Cell ', cell, '.png']);

  end
  
close all
cd ..
load Normal_ISRDD_Tunnel_Upstairs;

 % Plot Offset vs Normalized data into directory

  cd('C:\Users\sda\OneDrive - SLAC National Accelerator Laboratory\Magnet_Data\LCLS-II\Radiation_Detection_System\Offset Plots\Normal_Plots');
  for i = 2:21
      figure('units','normalized','outerposition',[0 0 0.95 0.95])
      C = strsplit(Offset(i).name, ':');
      cell = C{3}(1:2);
      poly_polyfit_res(ISRDD_Tunnel(i).Cell_GI.GI_Integral_plus_Gap_Temp_Corr], Offset(i).run_offset, Offset(i).std_run_offset, 'bo');
%       hold
%       tunnel_runs = [1:max([ISRDD_Tunnel(i).GI.Run_Num])];
%       errorbar(tunnel_runs, [ISRDD_Tunnel(i).Cell_GI.GI_Integral_plus_Gap_Temp_Corr]/ ISRDD_Tunnel(i).Cell_GI.Integral_plus_Gap_Temp_Corr_mean, [ISRDD_Tunnel(i).GI.Gap_Corr_Integral_plus_std]/ISRDD_Tunnel(i).Cell_GI.Integral_plus_Gap_Temp_Corr_mean, marker_make3(2)); 
%       xlabel('Run #')
%       ylabel('Offset (micron)');
%       title(['Offset vs Run #, for SXU Cell ', cell]);
%       saveas(gcf,['Normalized Offset and Vs vs Run # for SXU Cell ', cell, '.png']);

  end


