% In Situ Radiation Detection System Calibration setup tests
clear all;
[runs] = find_gap_int_plt;


set_runs_start = [12];
set_runs_end = [12];

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
 if length(runs) > 1
 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 #       
       plot_bars_with_mean_std(runs,GI_Integral_mean_corr,GI_Integral_std,'Run','#','Temp Corr Calib Integral','Vs')
        xlabel('Run (#)');
        ylabel('Temperature Corrected Calibration Integral (Vs)');
        title('PC97084 Calibration Integral mean vs Run #'); 
       saveas(gcf,'PC97084 Calibration Integral vs Run.png');
 else   
       GI_Integral_mean_corr = GI_Integral_mean;
       errorbar(runs,GI_Integral_mean,GI_Integral_std)
        xlabel('Run (#)');
        ylabel('Temperature Corrected Calibration Integral (C)');
        title('PC97084 Calibration Integral mean vs Run #'); 
       saveas(gcf,'PC97084 Calibration Integral vs Run.png');        
 end  
     
 
% Plot temperature data vs run #     
       figure('units','normalized','outerposition',[0 0 1 1])
        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('PC97084 Junction Temperatures vs Run #'); 
        legend('Walker Junction', 'HP3458 Junction');
        
% Write summary file  
  data_file = ['PC97084 Calib Summary.dat'];   
    f_title =  ['PC97084 Calib 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(runs)
fprintf(fidC,'%2i      %9.6f    %9.6f     %8.4e       %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));    
end
fprintf(fidC,'\n---------------------------------------------------\n');

fprintf(fidC,'Summary %9.7f    %9.7f     %8.4e        %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));
 
fclose(fidC);   
       

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

fprintf(fidC,'Run#    Walker Junc Temp  3458_Meas_Junction\n');
for i = 1:length(runs)
fprintf(fidC,'%2i      %8.3f              %8.3f\n',  runs(i), Temp_Meas_Junc_mean(i),  Temp_HP3458_mean(i));    
end

 
fclose(fidC);   
       