% In Situ Radiation Detection System Calibration setup tests

[runs] = find_gap_int_plt;

tunnel_run_num =    [1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16];
tunnel_runs_start = [9,  11, 13, 15, 17, 24, 29, 31, 34, 38, 43, 47, 50, 53, 56, 58];
tunnel_runs_end =   [10, 12, 14, 16, 18, 27, 30, 32, 35, 39, 44, 48, 51, 54, 57, 59];
upstairs_run = [33, 36, 37, 41, 45, 49, 52, 55, 60, 61];

for i = 1:length(runs)
   GI(i) =  Load_gap_int_plt(runs(i));
    Temp(i) = Load_tstpar_calib(runs(i));
    
    GI_Integral_mean(i) = mean(GI(i).Integral);
    GI_Integral_std(i) = std(GI(i).Integral);
       Temp_Meas_Junc_mean(i) = mean(Temp(i).Meas_Junc);
        Temp_Meas_Junc_std(i) = std(Temp(i).Meas_Junc);
          Temp_HP3458_mean(i) = mean(Temp(i).HP3458);
          Temp_HP3458_std(i) = std(Temp(i).HP3458);
end
 % Correct temperature correlation
 Temp_diff = Temp_Meas_Junc_mean- Temp_Meas_Junc_mean(1);

    [poly_plus, dp_plus] =  plot_polyfit_res(Temp_diff, GI_Integral_mean ,1,1, 'Meas Junc Delta Temp', 'Calib Integral', 'C', 'Vs');

       Calib_correction_factor = -poly_plus(1);
       
       GI_Integral_mean_corr = GI_Integral_mean + Temp_diff*Calib_correction_factor;
       
% Plot Integral voltage vs run # for all runs     
       plot_bars_with_mean_std(runs,GI_Integral_mean_corr,GI_Integral_std,'Run','#','Temp Corr Calib Integral','Vs')
       saveas(gcf,'All Corrected Calibration Integral vs Run.png'); 
       
% Plot temperature data vs run # for all runs    
       figure('units','normalized','outerposition',[0 0 0.9 0.9])
        errorbar(runs, Temp_Meas_Junc_mean, Temp_Meas_Junc_std, 'bo'); 
        hold
        errorbar(runs, Temp_HP3458_mean, Temp_HP3458_std, 'r*'); 
        xlabel('Run (#)');
        ylabel('Junction Temperatures (C)');
        title('Junction Temperature vs Run #'); 
        legend('Walker Junction', 'HP3458 Junction');
        saveas(gcf,'All Temperatures vs Run.png'); 

        
% Write summary file for tunnel runs 
  data_file = ['PC33408 Calib Tunnel Runs Summary.dat'];   
    f_title =  ['PC33408 Calib Tunnel Runs Summary Integral Data:\n\n'] ;
    
 fidC = fopen(data_file,'w');
fprintf(fidC,f_title);

fprintf(fidC,'Run#    Uncorr_Int   Corr_Int   Corr_Rel_Error   Walker_Junc_Temp\n');
for i = 1:length(tunnel_run_num)
fprintf(fidC,'%2i      %9.6f    %9.6f     %8.4e       %8.3f\n',  tunnel_run_num(i), GI_Integral_mean(tunnel_runs_start(i)),  GI_Integral_mean_corr(tunnel_runs_start(i)), GI_Integral_std(tunnel_runs_start(i))/GI_Integral_mean_corr(tunnel_runs_start(i)), Temp_Meas_Junc_mean(tunnel_runs_start(i)));    
fprintf(fidC,'%2i      %9.6f    %9.6f     %8.4e       %8.3f\n',  tunnel_run_num(i), GI_Integral_mean(tunnel_runs_end(i)),  GI_Integral_mean_corr(tunnel_runs_end(i)), GI_Integral_std(tunnel_runs_end(i))/GI_Integral_mean_corr(tunnel_runs_end(i)), Temp_Meas_Junc_mean(tunnel_runs_end(i)));    
end
fprintf(fidC,'\n--------------------------------------------------------------------------------\n');
fprintf(fidC,'Run#    Uncorr_Mean   Corr_Mean  Corr_Rel_Error   Walker_Junc_Temp_Mean\n');
fprintf(fidC,'         %9.7f    %9.7f     %8.4e       %8.3f\n',  mean([GI_Integral_mean(tunnel_runs_start), GI_Integral_mean(tunnel_runs_end)]), mean([GI_Integral_mean_corr(tunnel_runs_start), GI_Integral_mean_corr(tunnel_runs_end)]), std([GI_Integral_mean_corr(tunnel_runs_start), GI_Integral_mean_corr(tunnel_runs_end)])/mean([GI_Integral_mean_corr(tunnel_runs_start), GI_Integral_mean_corr(tunnel_runs_end)]), mean(Temp_Meas_Junc_mean([tunnel_runs_start,tunnel_runs_end])));
 
fclose(fidC);   

 % Write summary file for upstairs runs 
  data_file = ['PC33408 Calib Upstairs Runs Summary.dat'];   
    f_title =  ['PC33408 Calib Upstairs Runs Summary Integral Data:\n\n'] ;
    
 fidC = fopen(data_file,'w');
fprintf(fidC,f_title);

fprintf(fidC,'Run#    Uncorr_Int   Corr_Int   Corr_Rel_Error   Walker_Junc_Temp\n');
for i = 1:length(upstairs_run)
fprintf(fidC,'%2i      %9.6f    %9.6f     %8.4e       %8.3f\n',  i, GI_Integral_mean(upstairs_run(i)),  GI_Integral_mean_corr(upstairs_run(i)), GI_Integral_std(upstairs_run(i))/GI_Integral_mean_corr(upstairs_run(i)), Temp_Meas_Junc_mean(upstairs_run(i)));    
end
fprintf(fidC,'\n---------------------------------------------------------------\n');
fprintf(fidC,'Run#    Uncorr_Mean   Corr_Mean  Corr_Rel_Error   Walker_Junc_Temp_Mean\n');
fprintf(fidC,'         %9.7f    %9.7f     %8.4e     %8.3f\n',  mean(GI_Integral_mean(upstairs_run)), mean(GI_Integral_mean_corr(upstairs_run)), std(GI_Integral_mean_corr(upstairs_run))/mean(GI_Integral_mean_corr(upstairs_run)), mean(Temp_Meas_Junc_mean(upstairs_run)));
 
fclose(fidC);   
       

% Write summary temp summary file for all runs
  data_file = ['PC33408 Calib Temp Corrected Full Run Summary.dat'];   
    f_title =  ['PC33408 Calib Temp Corrected Full Run Summary:\n\n'] ;
    
 fidC = fopen(data_file,'w');
fprintf(fidC,f_title);


fprintf(fidC,'Run#    Uncorr_Int   Corr_Int   Corr_Rel_Error    Walker_Junc_Temp  3458_Meas_Junction\n');
for i = 1:length(GI)
fprintf(fidC,'%2i      %9.6f    %9.6f     %8.4e       %8.3f           %8.3f\n',  runs(i), GI_Integral_mean(i),  GI_Integral_mean_corr(i), GI_Integral_std(i)/GI_Integral_mean_corr(i), Temp_Meas_Junc_mean(i),  Temp_HP3458_mean(i));    
end
fprintf(fidC,'\n-------------------------------------------------------------------------------------\n');
fprintf(fidC,'Run#    Uncorr_Mean  Corr_Mean   Corr_Rel_Error  Walker_Junc_Temp  3458_Meas_Junction\n');
fprintf(fidC,'         %9.7f    %9.7f     %8.4e      %8.3f            %8.3f \n',  mean(GI_Integral_mean), mean(GI_Integral_mean_corr), std(GI_Integral_mean_corr)/mean(GI_Integral_mean_corr), mean(Temp_Meas_Junc_mean), mean(Temp_HP3458_mean));
 
fclose(fidC);   

% Plot Integral voltage vs run # for tunnel runs       
       figure('units','normalized','outerposition',[0 0 0.9 0.9])
       errorbar(tunnel_run_num,GI_Integral_mean_corr(tunnel_runs_start),GI_Integral_std(tunnel_runs_start), 'bo');
       hold
       errorbar(tunnel_run_num,GI_Integral_mean_corr(tunnel_runs_end),GI_Integral_std(tunnel_runs_end), 'r*');
        xlabel('Tunnel Full Run (#)');
        ylabel('Temperature Corrected Calibration Integral (Vs)');
        title('PC33408 Tunnel Calibration Integral vs Run #'); 
        legend('Tunnel Run Start', 'Tunnel Run End');
        Tunnel_Full_Run_Integral_mean_corr = mean(GI_Integral_mean_corr([tunnel_runs_start,tunnel_runs_end]));
        Tunnel_Full_Run_Integral_std_corr = std(GI_Integral_mean_corr([tunnel_runs_start,tunnel_runs_end]));
        Plot_mean_stdev(Tunnel_Full_Run_Integral_mean_corr, Tunnel_Full_Run_Integral_std_corr, 'Vs')
        saveas(gcf,'Tunnel Corrected Calibration Integral vs Run.png'); 
       
 % Plot temperature data vs run # for tunnel runs   
       figure('units','normalized','outerposition',[0 0 0.9 0.9])
        errorbar(tunnel_run_num, Temp_Meas_Junc_mean(tunnel_runs_start), Temp_Meas_Junc_std(tunnel_runs_start), 'bo');
        hold
        errorbar(tunnel_run_num, Temp_Meas_Junc_mean(tunnel_runs_end), Temp_Meas_Junc_std(tunnel_runs_end), 'kd'); 
        errorbar(tunnel_run_num, Temp_HP3458_mean(tunnel_runs_start), Temp_HP3458_std(tunnel_runs_start), 'r*'); 
        errorbar(tunnel_run_num, Temp_HP3458_mean(tunnel_runs_end), Temp_HP3458_std(tunnel_runs_end), 'msq'); 
        xlabel('Tunnel Full Run (#)');
        ylabel('Junction Temperatures (C)');
        title('PC33408 Tunnel Junction Temperature vs Run #'); 
        legend('Walker Junction Start', 'Walker Junction End', 'HP3458 Junction Start', 'HP3458 Junction End');
        saveas(gcf,'Tunnel Full Run Temperture vs Tunnel Run.png'); 
     
% Plot Integral voltage vs run # for upstairs runs       
       figure('units','normalized','outerposition',[0 0 0.9 0.9])
       errorbar(1:length(upstairs_run),GI_Integral_mean_corr(upstairs_run),GI_Integral_std(upstairs_run), 'bo');
        xlabel('Upstairs Full Run (#)');
        ylabel('Temperature Corrected Calibration Integral (Vs)');
        title('PC33408 Upstairs Calibration Integral vs Run #'); 
        legend('Upstairs Run');
        Plot_mean_stdev(mean(GI_Integral_mean_corr(upstairs_run)),std(GI_Integral_mean_corr(upstairs_run)), 'Vs')
        saveas(gcf,'Upstairs Corrected Calibration Integral vs Run.png'); 
       
 % Plot temperature data vs run # for upstairs runs   
       figure('units','normalized','outerposition',[0 0 0.9 0.9])
        errorbar(1:length(upstairs_run), Temp_Meas_Junc_mean(upstairs_run), Temp_Meas_Junc_std(upstairs_run), 'bo');
        hold
        errorbar(1:length(upstairs_run), Temp_HP3458_mean(upstairs_run), Temp_HP3458_std(upstairs_run), 'r*'); 
        xlabel('Upstairs Full Run (#)');
        ylabel('Junction Temperatures (C)');
        title('PC33408 Upstairs Junction Temperature vs Run #'); 
        legend('Walker Junction', 'HP3458 Junction');
        saveas(gcf,'Upstairs Full Run Temperture vs Run Number.png'); 