def calibration_correction(measurement, channel, energy):
"""
calibration_correction implements a corrected energy calibration based
on the array of channels and energies given as input. It performs a
least squares regression fit of the channels and energies and implements
the new energy calibration in a newly generated .Spe file. The new
spectra file will contain the same information with only the old
calibration changed.
"""
cal_file = sh.copyfile(measurement, os.path.splitext(measurement)[0] +
'_recal.Spe')
fix_measurement = SPEFile.SPEFile(cal_file)
fix_measurement.read()
old_cal = (str(float(fix_measurement.energy_cal[0])) + ' ' +
str(float(fix_measurement.energy_cal[1])))
a_matrix = np.vstack([channel, np.ones(len(channel))]).T
calibration_line = np.linalg.lstsq(a_matrix, energy)
e0 = float(calibration_line[0][1])
eslope = float(calibration_line[0][0])
new_cal = str(float(e0)) + ' ' + str(float(eslope))
with fileinput.FileInput(cal_file, inplace=1) as file:
for line in file:
print(line.replace(old_cal, new_cal).rstrip())
return(cal_file)
评论列表
文章目录