% Load and Plot Taper data for the Upstairs Runs.  
  cd('C:\Users\sda\OneDrive - SLAC National Accelerator Laboratory\Magnet_Data\LCLS-II\Radiation_Detection_System\Taper Plots Upstairs');

load('SXU_26_45_Taper_Des.mat');

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

    end
end
    load('SXU_46_47_Taper_Des.mat');

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

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

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

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

start_time_hours = [18., 19., 14., 19., 17., 15., 15., 15. ]; 
run_num =          [1,   2,   3,   4,   5,   6,   7,   8];   

% Find taper data on in the hours before each measurement

 for i = 1:21
  for j = 1:length(run_num)
      if j > 7 % The data after run the taper will be set to 0.
      Taper(i).run_taper(j) = 0.0;
      Taper(i).std_run_taper(j) = 0.0;
      Taper(i).run(j) = run_num(j);
      else
      Taper(i).run_taper(j) = mean(filloutliers([Taper(i).taper(hours(j) + (start_time_hours(j)-2):hours(j) + start_time_hours(j))], 'center', 'mean'));
      Taper(i).std_run_taper(j) = std(filloutliers([Taper(i).taper(hours(j) + (start_time_hours(j)-2):hours(j) + start_time_hours(j))], 'center', 'mean'));
      Taper(i).run(j) = run_num(j);
      end     
  end
 end

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

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

  end
  close all
  
   % Put taper data into a file
   
  for i = 1:21
      C = strsplit(Taper(i).name, ':');
      cell = C{3}(1:2);
       fidC = fopen(['Taper for SXU Cell ', cell, '.dat'], 'w');

     fprintf(fidC,'Run#    Taper    Taper_std \n');
     fprintf(fidC,' #        mm         mm \n');
    for j =1:length([Taper(i).run_taper])
      fprintf(fidC,' %2i   %8.4f   %8.4f \n',  Taper(i).run(j), Taper(i).run_taper(j), Taper(i).std_run_taper(j));
    end
      fclose(fidC); 
  end


