ZGainsforth
5/5/2015 - 9:11 PM

Using two Bruker element quant output files, subtracts the counts of one from the other.

Using two Bruker element quant output files, subtracts the counts of one from the other.

# Created 2015, Zack Gainsforth
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import numpy as np
import wx
import pandas as pd
import sys, os

if 'app' not in locals():
    app = wx.App(None)

def FileDialog(DefaultDir='', Title='Select File:', Extension=''):
    dlg = wx.FileDialog (None, Title, DefaultDir, '', Extension, wx.FD_DEFAULT_STYLE | wx.FD_FILE_MUST_EXIST)
    if dlg.ShowModal() == wx.ID_OK:
        FileName = dlg.GetPath()
    else:
        FileName = None
    dlg.Destroy()

    return FileName

SFileName = FileDialog(Title='Select Bruker quant results file for the spectrum with a background:')
BFileName = FileDialog(Title='Select Bruker quant results file for the background spectrum:')
NormElement = raw_input('Choose normalization element (e.g. S):')

#SFileName = '/Volumes/Stardust/Desktop/Current samples/Dante Foils/Foil 2 ACI2,05, 500C/20150422 - TitanX - Dante foil ACI2,05/Sulfide Metal interface 200 keV/EDS/Bruker Spectra/Metal whisker Bruker Results.txt'
#BFileName = '/Volumes/Stardust/Desktop/Current samples/Dante Foils/Foil 2 ACI2,05, 500C/20150422 - TitanX - Dante foil ACI2,05/Sulfide Metal interface 200 keV/EDS/Bruker Spectra/Metal whisker background Bruker Results.txt'
#NormElement='S'

S = np.genfromtxt(SFileName, skip_header=5, skip_footer=2, dtype=None)
B = np.genfromtxt(BFileName, skip_header=5, skip_footer=2, dtype=None)

if S.shape != B.shape:
    print 'Both spectra must have the same element list.'
    
# Drop it into pandas, and do a sort on elemental Z.  Then 
pdS = pd.DataFrame(S).sort('f1')
pdB = pd.DataFrame(B).sort('f1')

if np.any(pdS['f0'].as_matrix() != pdB['f0'].as_matrix()):
    print 'Both spectra must have the same element list.'

# Get the list of elements for reporting.  Columns are 0: Element Name, 3: Counts.
CountsArray = np.vstack((pdS.as_matrix()[:,(0,3)].T, pdB.as_matrix()[:,3]))

# Now we have three rows: Element names, spectrum counts, background counts.  We'll make a new row which is the normalized counts for the background.
# First get the index of the normalization element.
try:
    NormIdx = np.where(CountsArray[0,:]==NormElement)[0][0]
except:
    print "Normalization element wasn't found!"
    sys.exit()
        
print 'Normalizing to ' + NormElement

# We multiply the background row by the appropriate amount so that the counts in the normalization element are the same
# as the counts in the main spectrum.
NormBkg = CountsArray[2, :] * (CountsArray[1,NormIdx]*1. / CountsArray[2,NormIdx])
CountsArray = np.vstack((CountsArray, NormBkg))

# And now we add one more row which is the corrected counts after removing the normalized background.
CountsArray = np.vstack((CountsArray, CountsArray[1,:]-CountsArray[3,:]))

# And back to pandas for the final formatting and output.
CountsTable = pd.DataFrame(CountsArray.T)
CountsTable.columns = ['Element', 'Counts', 'Bkg Counts', 'Norm Bkg Counts', 'Corr Counts']

print CountsTable

print 'Saving xls.'

CountsTable.to_excel(os.path.splitext(SFileName)[0] + ' minus background.xls')

print 'Done.'