def eleXRF_energy(ele, energy): Z = xl.SymbolToAtomicNumber(ele); F = xl.LineEnergy(Z, xl.KA1_LINE) if xl.EdgeEnergy(Z, xl.K_SHELL) > energy: F = xl.LineEnergy(Z, xl.LA1_LINE) elif xl.EdgeEnergy(Z, xl.L1_SHELL) > energy: F = xl.LineEnergy(Z, xl.LB1_LINE) elif xl.EdgeEnergy(Z, xl.L2_SHELL) > energy: F = xl.LineEnergy(Z, xl.LB1_LINE) elif xl.EdgeEnergy(Z, xl.L3_SHELL) > energy: F = xl.LineEnergy(Z, xl.LG1_LINE) elif xl.EdgeEnergy(Z, xl.M1_SHELL) > energy: F = xl.LineEnergy(Z, xl.MA1_LINE) return F
def absorptionEdge(element, edge=None): if type(element) is str: element = xl.SymbolToAtomicNumber(element) shells = ["K", "L1", "L2", "L3", "M1", "M2", "M3", "M4", "M5"] if edge is not None: shell_ind = shells.index(edge) return xl.EdgeEnergy(element, shell_ind) else: shell_inds = range(8) print("Absorption edges %s" % xl.AtomicNumberToSymbol(element)) for shell_ind in shell_inds: print(" " + shells[shell_ind].ljust(3) + " = %7.1f eV" % (xl.EdgeEnergy(element, shell_ind) * 1000))
def absorptionEdge(element,edge=None): if type(element) is str: element = xl.SymbolToAtomicNumber(element) shells = ['K','L1','L2','L3','M1','M2','M3','M4','M5'] if edge is not None: shell_ind = shells.index(edge) return xl.EdgeEnergy(element,shell_ind) else: shell_inds = range(8) print('Absorption edges %s'%xl.AtomicNumberToSymbol(element)) for shell_ind in shell_inds: print(' '\ +shells[shell_ind].ljust(3)\ +' = %7.1f eV'%(xl.EdgeEnergy(element,shell_ind)*1000))
def XRF_line(Element, Beam_Energy): Z = xl.SymbolToAtomicNumber(str(Element)) F = xl.LineEnergy(Z, xl.KA1_LINE) if xl.EdgeEnergy(Z, xl.K_SHELL) > Beam_Energy: F = xl.LineEnergy(Z, xl.LA1_LINE) if xl.EdgeEnergy(Z, xl.L1_SHELL) > Beam_Energy: F = xl.LineEnergy(Z, xl.LB1_LINE) if xl.EdgeEnergy(Z, xl.L2_SHELL) > Beam_Energy: F = xl.LineEnergy(Z, xl.LB1_LINE) if xl.EdgeEnergy(Z, xl.L3_SHELL) > Beam_Energy: F = xl.LineEnergy(Z, xl.LG1_LINE) if xl.EdgeEnergy(Z, xl.M1_SHELL) > Beam_Energy: F = xl.LineEnergy(Z, xl.MA1_LINE) return F
def GetFluorescenceEnergy(Element,Beam): # send in the element and the beam energy to get the Excited Fluorescence Energy #this will return the highest energy fluorescence photon capable of being excited by the beam Z = xraylib.SymbolToAtomicNumber(Element) F = xraylib.LineEnergy(Z,xraylib.KA1_LINE) if xraylib.EdgeEnergy(Z,xraylib.K_SHELL) > Beam: F = xraylib.LineEnergy(Z,xraylib.LA1_LINE) if xraylib.EdgeEnergy(Z,xraylib.L1_SHELL) > Beam: F = xraylib.LineEnergy(Z,xraylib.LB1_LINE) if xraylib.EdgeEnergy(Z,xraylib.L2_SHELL) > Beam: F = xraylib.LineEnergy(Z,xraylib.LB1_LINE) if xraylib.EdgeEnergy(Z,xraylib.L3_SHELL) > Beam: F = xraylib.LineEnergy(Z,xraylib.LG1_LINE) if xraylib.EdgeEnergy(Z,xraylib.M1_SHELL) > Beam: F = xraylib.LineEnergy(Z,xraylib.MA1_LINE) return F
def get_Ele_XRF_Energy(ele, energy): Z = xl.SymbolToAtomicNumber(ele) #will it abosrb? if so, it will fluoresce F = xl.LineEnergy(Z, xl.KA1_LINE) if xl.EdgeEnergy(Z, xl.K_SHELL) > energy: F = xl.LineEnergy(Z, xl.LA1_LINE) if xl.EdgeEnergy(Z, xl.L1_SHELL) > energy: F = xl.LineEnergy(Z, xl.LB1_LINE) if xl.EdgeEnergy(Z, xl.L2_SHELL) > energy: F = xl.LineEnergy(Z, xl.LB1_LINE) if xl.EdgeEnergy(Z, xl.L3_SHELL) > energy: F = xl.LineEnergy(Z, xl.LG1_LINE) if xl.EdgeEnergy(Z, xl.M1_SHELL) > energy: F = xl.LineEnergy(Z, xl.MA1_LINE) return F
def get_data(): sym, z, group, period, _type = ([], [], [], [], []) mass, density, absedge, alw, flyield, augyield = ([], [], [], [], [], []) for i in range(1, 104): s = xraylib.AtomicNumberToSymbol(i) sym.append(s) z.append(i) mass.append(xraylib.AtomicWeight(i)) density.append(str(xraylib.ElementDensity(i)) + ' g/cm^3') absedge.append(str(xraylib.EdgeEnergy(i, 0)) + ' keV') alw.append(str(xraylib.AtomicLevelWidth(i, 0)) + ' keV') flyield.append(str(xraylib.FluorYield(i, 0))) augyield.append(str(xraylib.AugerYield(i, 0))) pop_period(i, period) pop_group(i, group) pop_type(i, _type) data = { 'sym': sym, 'z': z, 'mass': mass, 'group': group, 'period': period, '_type': _type, 'density': density, 'flyield': flyield, 'augyield': augyield, 'absedge': absedge, 'alw': alw } return data
def edge(element=None, line='K', unit='eV'): ''' function return edge (K or L3) in eV or keV with input element sympbol ''' atomic_num = xraylib.SymbolToAtomicNumber(element) if line == 'K': edge_value = xraylib.EdgeEnergy(atomic_num, xraylib.K_SHELL) if line == 'L3': edge_value = xraylib.EdgeEnergy(atomic_num, xraylib.L3_SHELL) if unit == 'eV': return edge_value * 1000 else: return edge_value
def parse_raw_fluoro(fn, edge, trans, acqTime): rv = [] with open(fn) as f: total_count = 0 background_count = 0 for i, l in enumerate(f): if i > 2: e, cts = l.strip().split() total_count += float(cts) background_count += min(float(cts), 2) # crude but works I guess... if float(e) > (float(edge) - 1250.0): break if (total_count - background_count) > 100: FilePrefix = os.path.splitext(os.path.basename(fn))[0] pure_path = PurePath(fn).parts VisitDir = pure_path[:6] RestOfFilename = os.path.join(*pure_path[6:]) RestOfDirs = os.path.dirname(RestOfFilename) OutputDir = os.path.join(*VisitDir, 'processed/pymca', RestOfDirs) elf = os.path.join(OutputDir, FilePrefix) + '.results.dat' els = [] # contains lines like # Cu-K 7929.020000 90.714300 with open(elf) as f: rv.append("Element\tCounts\t%age\tExpected Emission Energies") for i, l in enumerate(f): el, pk, conf = l.strip().split() symbol, edge = el.split('-') Z = xrl.SymbolToAtomicNumber(symbol) edgesEnergies = "<b>{:g}</b>,{:g}".format( xrl.LineEnergy(Z, LINE_MAPPER[edge]) * 1000.0, xrl.EdgeEnergy(Z, EDGE_MAPPER[edge]) * 1000.0) if float(pk) >= 100000: counts = int(float(pk)) else: counts = round(float(pk), 1) rv.append("{}\t{:g}\t{:g}\t{}".format( el, counts, round(100 * float(pk) / total_count, 1), edgesEnergies)) if i == 5: break else: rv.append("No fluorescence peaks detected, try a higher transmission") rv.append("\nCounts (total): {:g} (background): {:g}".format( total_count, background_count)) return rv
def XRF_line(Element, Beam_Energy): Z = xl.SymbolToAtomicNumber( str(Element)) #converts element string to element atomic number F = xl.LineEnergy( Z, xl.KA1_LINE ) #initialize energy of fluorescence photon as highest energy K-line transition for the element if xl.EdgeEnergy( Z, xl.K_SHELL ) > Beam_Energy: #if beam energy is less than K ABSORPTION energy, F = xl.LineEnergy( Z, xl.LA1_LINE ) #energy of fluorescence photon equals highest energy L-line transition for the element if xl.EdgeEnergy( Z, xl.L1_SHELL ) > Beam_Energy: #if beam energy is less than L1 ABSORPTION energy, and so on... F = xl.LineEnergy(Z, xl.LB1_LINE) if xl.EdgeEnergy(Z, xl.L2_SHELL) > Beam_Energy: F = xl.LineEnergy(Z, xl.LB1_LINE) if xl.EdgeEnergy(Z, xl.L3_SHELL) > Beam_Energy: F = xl.LineEnergy(Z, xl.LG1_LINE) if xl.EdgeEnergy(Z, xl.M1_SHELL) > Beam_Energy: F = xl.LineEnergy(Z, xl.MA1_LINE) return F
def edge_energy(element,shell): """ Get the absorption edge energy for a given element and shell using the xraylib backend Parameters: Element : Name, Symbol or Atomic Number (Z) as input Line :'K','L1','L2','L3','La','Lb','Lg','Ma','Mb','Mg' Returns: edge energy: float """ z = elementDB[element]["Z"] xlshell = _lookupxlshell(shell) return xraylib.EdgeEnergy(z,xlshell.shell)
def generateEPoints(ePointsGen, elem, kmin, kmax, kstep, reversed=True): """ Generates a list of energy values from the given list input: Tuples in the format (start energy, end energy, energy resolution), example: [(9.645,9.665,0.005),(9.666,9.7,0.0006),(9.705,9.725,0.005)] if reversed is true the list will be transposed return : list of energy points """ E0 = xraylib.EdgeEnergy(xraylib.SymbolToAtomicNumber(elem), xraylib.L3_SHELL) + ktoe(kmin) * 0.001 # kstart=2 #print(E0) krangeE = E0 + 0.001 * ktoe(np.arange(0.4, kmax, kstep)) e_points = [] if isinstance(ePointsGen[0], tuple): for values in ePointsGen: #use np.arange to generate values and extend it to the e_points list e_points.extend(np.arange(values[0], values[1], values[2])) edgeStart = values[1] + 0.001 e_points.extend(np.arange(edgeStart, E0, 0.001)) e_points.extend(krangeE) elif isinstance(ePointsGen, list): e_points = ePointsGen else: print(" Unknown energy list format") if reversed: #retrun list in the reversted order return np.around(e_points[::-1], 5) else: return np.around(e_points, 5)
def xoppy_calc_xraylib_widget(FUNCTION=0,ELEMENT=26,ELEMENTORCOMPOUND="FeSO4",COMPOUND="Ca5(PO4)3",TRANSITION_IUPAC_OR_SIEGBAHN=1,\ TRANSITION_IUPAC_TO=0,TRANSITION_IUPAC_FROM=0,TRANSITION_SIEGBAHN=0,SHELL=0,ENERGY=10.0): functions = [ '0 Fluorescence line energy', '1 Absorption edge energy', '2 Atomic weight', '3 Elemental density', '4 Total absorption cross section', '5 Photoionization cross section', '6 Partial photoionization cross section', '7 Rayleigh scattering cross section', '8 Compton scattering cross section', '9 Klein-Nishina cross section', '10 Mass energy-absorption cross section', '11 Differential unpolarized Klein-Nishina cross section', '12 Differential unpolarized Thomson cross section', '13 Differential unpolarized Rayleigh cross section', '14 Differential unpolarized Compton cross section', '15 Differential polarized Klein-Nishina cross section', '16 Differential polarized Thomson cross section', '17 Differential polarized Rayleigh cross section', '18 Differential polarized Compton cross section', '19 Atomic form factor', '20 Incoherent scattering function', '21 Momentum transfer function', '22 Coster-Kronig transition probability', '23 Fluorescence yield', '24 Jump factor', '25 Radiative transition probability', '26 Energy after Compton scattering', '27 Anomalous scattering factor φ′', '28 Anomalous scattering factor φ″', '29 Electronic configuration', '30 X-ray fluorescence production cross section (with full cascade)', '31 X-ray fluorescence production cross section (with radiative cascade)', '32 X-ray fluorescence production cross section (with non-radiative cascade)', '33 X-ray fluorescence production cross section (without cascade)', '34 Atomic level width', '35 Auger yield', '36 Auger rate', '37 Refractive index', '38 Compton broadening profile', '39 Partial Compton broadening profile', '40 List of NIST catalog compounds', '41 Get composition of NIST catalog compound', '42 List of X-ray emitting radionuclides', '43 Get excitation profile of X-ray emitting radionuclide', '44 Compoundparser' ] print("\nInside xoppy_calc_xraylib with FUNCTION = %s. " % (functions[FUNCTION])) if FUNCTION == 0: if TRANSITION_IUPAC_OR_SIEGBAHN == 0: lines = [ 'K', 'L1', 'L2', 'L3', 'M1', 'M2', 'M3', 'M4', 'M5', 'N1', 'N2', 'N3', 'N4', 'N5', 'N6', 'N7', 'O1', 'O2', 'O3', 'O4', 'O5', 'O6', 'O7', 'P1', 'P2', 'P3', 'P4', 'P5', 'Q1', 'Q2', 'Q3' ] line = lines[TRANSITION_IUPAC_TO] + lines[ TRANSITION_IUPAC_FROM] + "_LINE" command = "result = xraylib.LineEnergy(%d,xraylib.%s)" % (ELEMENT, line) print("executing command: ", command) line = getattr(xraylib, line) result = xraylib.LineEnergy(ELEMENT, line) print("result: %f keV" % (result)) if TRANSITION_IUPAC_OR_SIEGBAHN == 1: lines = [ 'KA1_LINE', 'KA2_LINE', 'KB1_LINE', 'KB2_LINE', 'KB3_LINE', 'KB4_LINE', 'KB5_LINE', 'LA1_LINE', 'LA2_LINE', 'LB1_LINE', 'LB2_LINE', 'LB3_LINE', 'LB4_LINE', 'LB5_LINE', 'LB6_LINE', 'LB7_LINE', 'LB9_LINE', 'LB10_LINE', 'LB15_LINE', 'LB17_LINE', 'LG1_LINE', 'LG2_LINE', 'LG3_LINE', 'LG4_LINE', 'LG5_LINE', 'LG6_LINE', 'LG8_LINE', 'LE_LINE', 'LL_LINE', 'LS_LINE', 'LT_LINE', 'LU_LINE', 'LV_LINE' ] line = lines[TRANSITION_SIEGBAHN] command = "result = xraylib.LineEnergy(%d,xraylib.%s)" % (ELEMENT, line) print("executing command: ", command) line = getattr(xraylib, line) result = xraylib.LineEnergy(ELEMENT, line) print("result: %f keV" % (result)) if TRANSITION_IUPAC_OR_SIEGBAHN == 2: lines = [ 'K', 'L1', 'L2', 'L3', 'M1', 'M2', 'M3', 'M4', 'M5', 'N1', 'N2', 'N3', 'N4', 'N5', 'N6', 'N7', 'O1', 'O2', 'O3', 'O4', 'O5', 'O6', 'O7', 'P1', 'P2', 'P3', 'P4', 'P5', 'Q1', 'Q2', 'Q3' ] for i1, l1 in enumerate(lines): for i2, l2 in enumerate(lines): if i1 != i2: line = l1 + l2 + "_LINE" try: line = getattr(xraylib, line) result = xraylib.LineEnergy(ELEMENT, line) except: pass else: if result != 0.0: print("%s%s %f keV" % (l1, l2, result)) elif FUNCTION == 1: shells = [ 'All shells', 'K_SHELL', 'L1_SHELL', 'L2_SHELL', 'L3_SHELL', 'M1_SHELL', 'M2_SHELL', 'M3_SHELL', 'M4_SHELL', 'M5_SHELL', 'N1_SHELL', 'N2_SHELL', 'N3_SHELL', 'N4_SHELL', 'N5_SHELL', 'N6_SHELL', 'N7_SHELL', 'O1_SHELL', 'O2_SHELL', 'O3_SHELL', 'O4_SHELL', 'O5_SHELL', 'O6_SHELL', 'O7_SHELL', 'P1_SHELL', 'P2_SHELL', 'P3_SHELL', 'P4_SHELL', 'P5_SHELL', 'Q1_SHELL', 'Q2_SHELL', 'Q3_SHELL' ] if SHELL == 0: #"all" for i, myshell in enumerate(shells): if i >= 1: # command = "result = xraylib.EdgeEnergy(%d,xraylib.%s)"%(ELEMENT,myshell) # print("executing command: ",command) shell_index = getattr(xraylib, myshell) try: result = xraylib.EdgeEnergy(ELEMENT, shell_index) except: pass else: if result != 0.0: print("%s %f keV" % (myshell, result)) else: print("No result") else: shell_index = getattr(xraylib, shells[SHELL]) try: command = "result = xraylib.EdgeEnergy(%d,xraylib.%s)" % ( ELEMENT, shells[SHELL]) print("executing command: ", command) result = xraylib.EdgeEnergy(ELEMENT, shell_index) except: pass else: if result != 0.0: print("Z=%d %s : %f keV" % (ELEMENT, shells[SHELL], result)) else: print("No result") elif FUNCTION == 2: result = xraylib.AtomicWeight(ELEMENT) if result != 0.0: print("Atomic weight for Z=%d : %f g/mol" % (ELEMENT, result)) elif FUNCTION == 3: result = xraylib.ElementDensity(ELEMENT) if result != 0.0: print("Element density for Z=%d : %f g/cm3" % (ELEMENT, result)) else: print("No result") elif FUNCTION == 4: command = "result = xraylib.CS_Total_CP('%s',%f)" % (ELEMENTORCOMPOUND, ENERGY) print("executing command: ", command) result = xraylib.CS_Total_CP(ELEMENTORCOMPOUND, ENERGY) if result != 0.0: print("Total absorption cross section: %f g/cm3" % (result)) else: print("No result") elif FUNCTION == 5: command = "result = xraylib.CS_Photo_CP('%s',%f)" % (ELEMENTORCOMPOUND, ENERGY) print("executing command: ", command) result = xraylib.CS_Photo_CP(ELEMENTORCOMPOUND, ENERGY) if result != 0.0: print("Photoionization cross section: %f g/cm3" % (result)) else: print("No result") elif FUNCTION == 6: shells = [ 'All shells', 'K_SHELL', 'L1_SHELL', 'L2_SHELL', 'L3_SHELL', 'M1_SHELL', 'M2_SHELL', 'M3_SHELL', 'M4_SHELL', 'M5_SHELL', 'N1_SHELL', 'N2_SHELL', 'N3_SHELL', 'N4_SHELL', 'N5_SHELL', 'N6_SHELL', 'N7_SHELL', 'O1_SHELL', 'O2_SHELL', 'O3_SHELL', 'O4_SHELL', 'O5_SHELL', 'O6_SHELL', 'O7_SHELL', 'P1_SHELL', 'P2_SHELL', 'P3_SHELL', 'P4_SHELL', 'P5_SHELL', 'Q1_SHELL', 'Q2_SHELL', 'Q3_SHELL' ] if SHELL == 0: #"all" for index in range(1, len(shells)): shell_index = getattr(xraylib, shells[index]) try: command = "result = xraylib.xraylib.CS_Photo_Partial('%d',xraylib.%s,%f)" % ( ELEMENT, shells[index], ENERGY) print("executing command: ", command) result = xraylib.CS_Photo_Partial(ELEMENT, shell_index, ENERGY) except: pass else: if result != 0.0: print("Z=%d, %s at E=%f keV: %f cm2/g" % (ELEMENT, shells[index], ENERGY, result)) else: print("No result") else: shell_index = getattr(xraylib, shells[SHELL]) try: command = "result = xraylib.xraylib.CS_Photo_Partial('%d',xraylib.%s,%f)" % ( ELEMENT, shells[SHELL], ENERGY) print("executing command: ", command) result = xraylib.CS_Photo_Partial(ELEMENT, shell_index, ENERGY) except: pass else: if result != 0.0: print("Z=%d, %s at E=%f keV: %f cm2/g" % (ELEMENT, shells[SHELL], ENERGY, result)) else: print("No result") elif FUNCTION == 7: command = "result = xraylib.CS_Rayl_CP('%s',%f)" % (ELEMENTORCOMPOUND, ENERGY) print("executing command: ", command) result = xraylib.CS_Rayl_CP(ELEMENTORCOMPOUND, ENERGY) if result != 0.0: print("Rayleigh cross section: %f cm2/g" % (result)) else: print("No result") elif FUNCTION == 8: command = "result = xraylib.CS_Compt_CP('%s',%f)" % (ELEMENTORCOMPOUND, ENERGY) print("executing command: ", command) result = xraylib.CS_Compt_CP(ELEMENTORCOMPOUND, ENERGY) if result != 0.0: print("Compton cross section: %f cm2/g" % (result)) else: print("No result") elif FUNCTION == 9: command = "result = xraylib.CS_KN(%f)" % (ENERGY) print("executing command: ", command) result = xraylib.CS_KN(ENERGY) if result != 0.0: print("Klein Nishina cross section: %f cm2/g" % (result)) else: print("No result") elif FUNCTION == 10: command = "result = xraylib.CS_Energy_CP('%s',%f)" % ( ELEMENTORCOMPOUND, ENERGY) print("executing command: ", command) result = xraylib.CS_Energy_CP(ELEMENTORCOMPOUND, ENERGY) if result != 0.0: print("Mass-energy absorption cross section: %f cm2/g" % (result)) else: print("No result") else: print("\n#### NOT YET IMPLEMENTED ####")
def _check_edge(element): symbol, edge = element Z = xrl.SymbolToAtomicNumber(symbol) shell = EDGE_MAPPER[edge] return float(energy) > xrl.EdgeEnergy(Z, shell) * 1000.0
deltamux = mux_above - mux_below time_estimates = { '10%': estimate_measurement_time(0.10), '5%': estimate_measurement_time(0.05), '1%': estimate_measurement_time(0.01), '0.5%': estimate_measurement_time(0.005), '0.1%': estimate_measurement_time(0.001) } #estimate the amount of element of interest in the sample #convert the absorption edge string to energy edge_element, edge_shell = inputs['edge_string'].split('-') Z = xrl.SymbolToAtomicNumber(edge_element) edge_energy = xrl.EdgeEnergy(Z, _SHELL_MACROS[edge_shell]) murho = [ xrl.CS_Total(Z, edge_energy - 0.001), xrl.CS_Total(Z, edge_energy + 0.001) ] mass_surface_density = deltamux / (murho[1] - murho[0]) print('') print('OUTPUT:') print( u' Estimated sample µx (below edge, above edge, edge step): %.3f, %.3f, %.3f ' % (mux_below, mux_above, deltamux)) print( u' Estimated sample µx (below edge, above edge, edge step): %.3f, %.3f, %.3f '
"""Example of using various xraylib functionality in python.""" import xraylib import math import numpy as np xraylib.XRayInit() xraylib.SetErrorMessages(0) print("Example of python program using xraylib") print("xraylib version: {}".format(xraylib.__version__)) print("Density of pure Al : {} g/cm3".format(xraylib.ElementDensity(13))) print("Ca K-alpha Fluorescence Line Energy: {}".format( xraylib.LineEnergy(20, xraylib.KA_LINE))) print("Fe partial photoionization cs of L3 at 6.0 keV: {}".format( xraylib.CS_Photo_Partial(26, xraylib.L3_SHELL, 6.0))) print("Zr L1 edge energy: {}".format(xraylib.EdgeEnergy(40, xraylib.L1_SHELL))) print("Pb Lalpha XRF production cs at 20.0 keV (jump approx): {}".format( xraylib.CS_FluorLine(82, xraylib.LA_LINE, 20.0))) print("Pb Lalpha XRF production cs at 20.0 keV (Kissel): {}".format( xraylib.CS_FluorLine_Kissel(82, xraylib.LA_LINE, 20.0))) print("Bi M1N2 radiative rate: {}".format( xraylib.RadRate(83, xraylib.M1N2_LINE))) print("U M3O3 Fluorescence Line Energy: {}".format( xraylib.LineEnergy(92, xraylib.M3O3_LINE))) print("Ca(HCO3)2 Rayleigh cs at 10.0 keV: {}".format( xraylib.CS_Rayl_CP("Ca(HCO3)2", 10.0))) cdtest = xraylib.CompoundParser("Ca(HCO3)2") print( "Ca(HCO3)2 contains {} atoms, {} elements and has a molar mass of {} g/mol" .format(cdtest['nAtomsAll'], cdtest['nElements'], cdtest['molarMass']))
def onChanged(self, text): ''' :param text: (str) choice of edge :return: sets an element's edge energy based off the chosen edge (as long as it is in the range 4.5 - 25) ''' edge = str(text) if edge == 'K': if xraylib.EdgeEnergy( element, xraylib.K_SHELL) >= 4.5 and xraylib.EdgeEnergy( element, xraylib.K_SHELL) <= 25: self.edge_energy.setValue( xraylib.EdgeEnergy(element, xraylib.K_SHELL) * 1000) self.edge_energy.setDisabled(False) else: self.edge_energy.setDisabled(True) if edge == 'L1': if xraylib.EdgeEnergy( element, xraylib.L1_SHELL) >= 4.5 and xraylib.EdgeEnergy( element, xraylib.L1_SHELL) <= 25: self.edge_energy.setValue( xraylib.EdgeEnergy(element, xraylib.L1_SHELL) * 1000) self.edge_energy.setDisabled(False) else: self.edge_energy.setDisabled(True) if edge == 'L2': if xraylib.EdgeEnergy( element, xraylib.L2_SHELL) >= 4.5 and xraylib.EdgeEnergy( element, xraylib.L2_SHELL) <= 25: self.edge_energy.setValue( xraylib.EdgeEnergy(element, xraylib.L2_SHELL) * 1000) self.edge_energy.setDisabled(False) else: self.edge_energy.setDisabled(True) if edge == 'L3': if xraylib.EdgeEnergy( element, xraylib.L3_SHELL) >= 4.5 and xraylib.EdgeEnergy( element, xraylib.L3_SHELL) <= 25: self.edge_energy.setValue( xraylib.EdgeEnergy(element, xraylib.L3_SHELL) * 1000) self.edge_energy.setDisabled(False) else: self.edge_energy.setDisabled(True) mode = self.mode.currentText() edge_energy = float(self.edge_energy.text()) xafs1_units = self.xafs1_units.currentText() xafs2_units = self.xafs2_units.currentText() xafs3_units = self.xafs3_units.currentText() if mode == 'Relative': self.pe_start.setRange(4500 - edge_energy, 25000 - edge_energy) self.pe_stop.setRange(4500 - edge_energy, 25000 - edge_energy) self.xanes_start.setRange(4500 - edge_energy, 25000 - edge_energy) self.xanes_stop.setRange(4500 - edge_energy, 25000 - edge_energy) self.xafs1_start.setRange(4500 - edge_energy, 25000 - edge_energy) self.xafs1_stop.setRange(4500 - edge_energy, 25000 - edge_energy) self.xafs2_start.setRange(4500 - edge_energy, 25000 - edge_energy) self.xafs2_stop.setRange(4500 - edge_energy, 25000 - edge_energy) self.xafs3_start.setRange(4500 - edge_energy, 25000 - edge_energy) self.xafs3_stop.setRange(4500 - edge_energy, 25000 - edge_energy) self.pe_start.setValue(0) self.xanes_start.setValue(0) self.xafs1_start.setValue(0) self.xafs2_start.setValue(0) self.xafs3_start.setValue(0) self.pe_stop.setValue(0) self.xanes_stop.setValue(0) self.xafs1_stop.setValue(0) self.xafs2_stop.setValue(0) self.xafs3_stop.setValue(0) if mode == 'Absolute': self.pe_start.setRange(4500, 25000) self.pe_stop.setRange(4500, 25000) self.xanes_start.setRange(4500, 25000) self.xanes_stop.setRange(4500, 25000) self.pe_start.setValue(edge_energy) self.xanes_start.setValue(edge_energy) self.pe_stop.setValue(edge_energy) self.xanes_stop.setValue(edge_energy) if xafs1_units == 'eV': self.xafs1_start.setRange(4500, 25000) self.xafs1_stop.setRange(4500, 25000) self.xafs1_start.setValue(edge_energy) self.xafs1_stop.setValue(edge_energy) if xafs2_units == 'eV': self.xafs2_start.setRange(4500, 25000) self.xafs2_stop.setRange(4500, 25000) self.xafs2_start.setValue(edge_energy) self.xafs2_stop.setValue(edge_energy) if xafs3_units == 'eV': self.xafs3_start.setRange(4500, 25000) self.xafs3_stop.setRange(4500, 25000) self.xafs3_start.setValue(edge_energy) self.xafs3_stop.setValue(edge_energy)
import xraylib #Generating the Element Tab file = open("elements.txt", "w") file.write('Element\tEdge\tEdge Energy\n\n') for i in range(0, 103): edge = 'K' y = xraylib.AtomicNumberToSymbol(i) if (xraylib.EdgeEnergy(i, xraylib.K_SHELL) >= 4.5): if (xraylib.EdgeEnergy(i, xraylib.K_SHELL) <= 25): file.write( y + '\t' + edge + '\t' + str(round(xraylib.EdgeEnergy(i, xraylib.K_SHELL) * 1000, 1)) + '\n') if (xraylib.EdgeEnergy(i, xraylib.L1_SHELL) >= 4.5): if (xraylib.EdgeEnergy(i, xraylib.L1_SHELL) <= 25): file.write( y + '\t' + 'L1' + '\t' + str(round(xraylib.EdgeEnergy(i, xraylib.L1_SHELL) * 1000, 1)) + '\n') if (xraylib.EdgeEnergy(i, xraylib.L2_SHELL) >= 4.5): if (xraylib.EdgeEnergy(i, xraylib.L2_SHELL) <= 25): file.write( y + '\t' + 'L2' + '\t' + str(round(xraylib.EdgeEnergy(i, xraylib.L2_SHELL) * 1000, 1)) + '\n') if (xraylib.EdgeEnergy(i, xraylib.L3_SHELL) >= 4.5):
print('** XAS SAMPLE OPTIMIZER v.' + _VERSION + ' **') print('----------------------------------') #Ask for user input input_str = {} input_str['compound'] = input('Compound: ') input_str['absorption_edge'] = input('Absorption edge (e.g. Cu-K, U-L3): ') photons_per_second = float( input('Incident photon flux (background subtracted) (ph/s): ')) compound = xrl.CompoundParser(input_str['compound']) #convert the absorption edge string to energy edge_element, edge_shell = input_str['absorption_edge'].split('-') edge_energy = xrl.EdgeEnergy(xrl.SymbolToAtomicNumber(edge_element), _SHELL_MACROS[edge_shell]) #calculate the mass attenuation coefficient below and above edge murho = [0, 0] for i in range(compound['nElements']): murho[0] = murho[0] + compound['massFractions'][i] * xrl.CS_Total( compound['Elements'][i], edge_energy - 0.001) murho[1] = murho[1] + compound['massFractions'][i] * xrl.CS_Total( compound['Elements'][i], edge_energy + 0.001) #calculate filler attenuation using an "average" nitrogen filter filler_mux = xrl.CS_Total(7, edge_energy) * 1.5 * 0.1 #Measurement specific values for background count rates, estimates are used beta = 1.1
def onActivated(self, text): ''' :param text: (str) choice of element :param element: (int) element's atomic number (needed to obtain edge energy) :return: sets an element's edge energy and a suggested edge (as long as it is in the range 4.5 - 25) ''' global element element = xraylib.SymbolToAtomicNumber(text) if xraylib.EdgeEnergy(element, xraylib.K_SHELL) >= 4.5 and xraylib.EdgeEnergy(element, xraylib.K_SHELL) <= 25: self.edge.setCurrentIndex(0) self.edge_energy.setValue(xraylib.EdgeEnergy(element, xraylib.K_SHELL)) else: if xraylib.EdgeEnergy(element, xraylib.L1_SHELL) >= 4.5 and xraylib.EdgeEnergy(element,xraylib.L1_SHELL) <= 25: self.edge.setCurrentIndex(1) self.edge_energy.setValue(xraylib.EdgeEnergy(element, xraylib.L1_SHELL)) else: if xraylib.EdgeEnergy(element, xraylib.L2_SHELL) >= 4.5 and xraylib.EdgeEnergy(element,xraylib.L2_SHELL) <= 25: self.edge.setCurrentIndex(2) self.edge_energy.setValue(xraylib.EdgeEnergy(element, xraylib.L2_SHELL)) else: if xraylib.EdgeEnergy(element, xraylib.L3_SHELL) >= 4.5 and xraylib.EdgeEnergy(element,xraylib.L3_SHELL) <= 25: self.edge.setCurrentIndex(3) self.edge_energy.setValue(xraylib.EdgeEnergy(element, xraylib.L3_SHELL))
def onActivated(self, text): ''' :param text: (str) choice of element :param element: (int) element's atomic number (needed to obtain edge energy) :return: sets an element's edge energy and a suggested edge (as long as it is in the range 4.5 - 25) ''' global element element = xraylib.SymbolToAtomicNumber(text) self.edge_energy.setDisabled(False) if xraylib.EdgeEnergy(element, xraylib.K_SHELL) >= 4.5 and xraylib.EdgeEnergy(element, xraylib.K_SHELL) <= 25: self.edge.setCurrentIndex(0) self.edge_energy.setValue(xraylib.EdgeEnergy(element, xraylib.K_SHELL)*1000) else: if xraylib.EdgeEnergy(element, xraylib.L1_SHELL) >= 4.5 and xraylib.EdgeEnergy(element,xraylib.L1_SHELL) <= 25: self.edge.setCurrentIndex(1) self.edge_energy.setValue(xraylib.EdgeEnergy(element, xraylib.L1_SHELL)*1000) else: if xraylib.EdgeEnergy(element, xraylib.L2_SHELL) >= 4.5 and xraylib.EdgeEnergy(element,xraylib.L2_SHELL) <= 25: self.edge.setCurrentIndex(2) self.edge_energy.setValue(xraylib.EdgeEnergy(element, xraylib.L2_SHELL)*1000) else: if xraylib.EdgeEnergy(element, xraylib.L3_SHELL) >= 4.5 and xraylib.EdgeEnergy(element,xraylib.L3_SHELL) <= 25: self.edge.setCurrentIndex(3) self.edge_energy.setValue(xraylib.EdgeEnergy(element, xraylib.L3_SHELL)*1000) edge_energy = float(self.edge_energy.text()) mode = self.mode.currentText() if mode == 'Relative': self.pe_start.setRange(4500 - edge_energy, 25000 - edge_energy) self.pe_stop.setRange(4500 - edge_energy, 25000 - edge_energy) self.xanes_start.setRange(4500 - edge_energy, 25000 - edge_energy) self.xanes_stop.setRange(4500 - edge_energy, 25000 - edge_energy) self.xafs1_start.setRange(4500 - edge_energy, 25000 - edge_energy) self.xafs1_stop.setRange(4500 - edge_energy, 25000 - edge_energy) self.xafs2_start.setRange(4500 - edge_energy, 25000 - edge_energy) self.xafs2_stop.setRange(4500 - edge_energy, 25000 - edge_energy) self.xafs3_start.setRange(4500 - edge_energy, 25000 - edge_energy) self.xafs3_stop.setRange(4500 - edge_energy, 25000 - edge_energy) self.pe_start.setValue(0) self.xanes_start.setValue(0) self.xafs1_start.setValue(0) self.xafs2_start.setValue(0) self.xafs3_start.setValue(0) self.pe_stop.setValue(0) self.xanes_stop.setValue(0) self.xafs1_stop.setValue(0) self.xafs2_stop.setValue(0) self.xafs3_stop.setValue(0) if mode == 'Absolute': self.pe_start.setRange(4500, 25000) self.pe_stop.setRange(4500, 25000) self.xanes_start.setRange(4500, 25000) self.xanes_stop.setRange(4500, 25000) self.xafs1_start.setRange(4500, 25000) self.xafs1_stop.setRange(4500, 25000) self.xafs2_start.setRange(4500, 25000) self.xafs2_stop.setRange(4500, 25000) self.xafs3_start.setRange(4500, 25000) self.xafs3_stop.setRange(4500, 25000) self.pe_start.setValue(edge_energy) self.xanes_start.setValue(edge_energy) self.xafs1_start.setValue(edge_energy) self.xafs2_start.setValue(edge_energy) self.xafs3_start.setValue(edge_energy) self.pe_stop.setValue(edge_energy) self.xanes_stop.setValue(edge_energy) self.xafs1_stop.setValue(edge_energy) self.xafs2_stop.setValue(edge_energy) self.xafs3_stop.setValue(edge_energy)
def edge_energy(element, edge): element = Z_number(element) if element is None: return None edge = edge_number(edge) if edge is None: return None return round(xraylib.EdgeEnergy(int(element), int(edge)) * 1000, 1)
def onChanged(self, text): ''' :param text: (str) choice of edge :return: sets an element's edge energy based off the chosen edge (as long as it is in the range 4.5 - 25) ''' edge = str(text) if edge == 'K': if xraylib.EdgeEnergy(element, xraylib.K_SHELL) >= 4.5 and xraylib.EdgeEnergy(element, xraylib.K_SHELL) <= 25: self.edge_energy.setValue(xraylib.EdgeEnergy(element, xraylib.K_SHELL)*1000) self.edge_energy.setDisabled(False) else: self.edge_energy.setDisabled(True) if edge == 'L1': if xraylib.EdgeEnergy(element, xraylib.L1_SHELL) >= 4.5 and xraylib.EdgeEnergy(element, xraylib.L1_SHELL) <= 25: self.edge_energy.setValue(xraylib.EdgeEnergy(element, xraylib.L1_SHELL)*1000) self.edge_energy.setDisabled(False) else: self.edge_energy.setDisabled(True) if edge == 'L2': if xraylib.EdgeEnergy(element, xraylib.L2_SHELL) >= 4.5 and xraylib.EdgeEnergy(element, xraylib.L2_SHELL) <= 25: self.edge_energy.setValue(xraylib.EdgeEnergy(element, xraylib.L2_SHELL)*1000) self.edge_energy.setDisabled(False) else: self.edge_energy.setDisabled(True) if edge == 'L3': if xraylib.EdgeEnergy(element, xraylib.L3_SHELL) >= 4.5 and xraylib.EdgeEnergy(element, xraylib.L3_SHELL) <= 25: self.edge_energy.setValue(xraylib.EdgeEnergy(element, xraylib.L3_SHELL)*1000) self.edge_energy.setDisabled(False) else: self.edge_energy.setDisabled(True)
ElementRange = range(1, 100) Erange = [2, 12] edge = 0 edgenames = ['K1', 'L1', 'L2', 'L3', 'M1'] plt.figure('energy levels') plt.clf() tx = [] ty = [] tt = [] for n, edge in enumerate(range(5)): E = [] Elements = [] for el in ElementRange: tE = xl.EdgeEnergy(el, edge) if tE > np.min(Erange) and tE < np.max(Erange): E.append(tE) Elements.append(el) l = plt.plot(E, Elements, '+', label=edgenames[n]) for el, tE in zip(Elements, E): tx.append(tE) ty.append(el) tt.append(\ plt.text(tE,el,xl.AtomicNumberToSymbol(el),\ bbox={'pad':0, 'alpha':0}, color=l[0].get_color())) plt.xlim(2, 12) plt.legend() plt.xlabel('Photon Energy / keV')
def findLines(self, paramdict=XRFDataset().paramdict): """ Calculates the line energies to fit """ # Incident Energy used in the experiment # Energy range to use for fitting pileup_cut_off = paramdict["FitParams"]["pileup_cutoff_keV"] include_pileup = paramdict["FitParams"]["include_pileup"] include_escape = paramdict["FitParams"]["include_escape"] fitting_range = paramdict["FitParams"]["fitted_energy_range_keV"] # x = paramdict["FitParams"]["mca_energies_used"] energy = paramdict["Experiment"]["incident_energy_keV"] detectortype = 'Vortex_SDD_Xspress' fitelements = paramdict["Experiment"]["elements"] peakpos = [] escape_peaks = [] for _j, el in enumerate(fitelements): z = xl.SymbolToAtomicNumber(str(el)) for i, shell in enumerate(shells): if (xl.EdgeEnergy(z, shell) < energy - 0.5): linepos = 0.0 count = 0.0 for line in transitions[i]: en = xl.LineEnergy(z, line) if (en > 0.0): linepos += en count += 1.0 if (count == 0.0): break linepos = linepos / count if (linepos > fitting_range[0] and linepos < fitting_range[1]): peakpos.append(linepos) peakpos = np.array(peakpos) too_low = set(list(peakpos[peakpos > fitting_range[0]])) too_high = set(list(peakpos[peakpos < fitting_range[1]])) bar = list(too_low and too_high) bar = np.unique(bar) peakpos = list(bar) peaks = [] peaks.extend(peakpos) if (include_escape): for i in range(len(peakpos)): escape_energy = calc_escape_energy(peakpos[i], detectortype)[0] if (escape_energy > fitting_range[0]): if (escape_energy < fitting_range[1]): escape_peaks.extend([escape_energy]) # print escape_peaks peaks.extend(escape_peaks) if (include_pileup): # applies just to the fluorescence lines pileup_peaks = [] peakpos1 = np.array(peakpos) peakpos_high = peakpos1[peakpos1 > pileup_cut_off] peakpos_high = list(peakpos_high) for i in range(len(peakpos_high)): foo = [peakpos_high[i] + x for x in peakpos_high[i:]] foo = np.array(foo) pileup_peaks.extend(foo) pileup_peaks = np.unique(sorted(pileup_peaks)) peaks.extend(pileup_peaks) peakpos = peaks peakpos = np.array(peakpos) too_low = set(list(peakpos[peakpos > fitting_range[0]])) too_high = set(list(peakpos[peakpos < fitting_range[1] - 0.5])) bar = list(too_low and too_high) bar = np.unique(bar) peakpos = list(bar) peakpos = np.unique(peakpos) # print peakpos return peakpos
#from xraylib import * import sys, string import xraylib import math if __name__ == '__main__' : xraylib.XRayInit() xraylib.SetErrorMessages(0) print ("Example of python program using xraylib") print ("Density of pure Al : %f g/cm3" % xraylib.ElementDensity(13)) print ("Ca K-alpha Fluorescence Line Energy: %f" % xraylib.LineEnergy(20,xraylib.KA_LINE)) print ("Fe partial photoionization cs of L3 at 6.0 keV: %f" % xraylib.CS_Photo_Partial(26,xraylib.L3_SHELL,6.0)) print ("Zr L1 edge energy: %f" % xraylib.EdgeEnergy(40,xraylib.L1_SHELL)) print ("Pb Lalpha XRF production cs at 20.0 keV (jump approx): %f" % xraylib.CS_FluorLine(82,xraylib.LA_LINE,20.0)) print ("Pb Lalpha XRF production cs at 20.0 keV (Kissel): %f" % xraylib.CS_FluorLine_Kissel(82,xraylib.LA_LINE,20.0)) print ("Bi M1N2 radiative rate: %f" % xraylib.RadRate(83,xraylib.M1N2_LINE)) print ("U M3O3 Fluorescence Line Energy: %f" % xraylib.LineEnergy(92,xraylib.M3O3_LINE)) print ("Ca(HCO3)2 Rayleigh cs at 10.0 keV: %f" % xraylib.CS_Rayl_CP("Ca(HCO3)2",10.0)) cdtest = xraylib.CompoundParser("Ca(HCO3)2") print ("Ca(HCO3)2 contains %g atoms and %i elements"% (cdtest['nAtomsAll'], cdtest['nElements'])) for i in range(cdtest['nElements']): print ("Element %i: %lf %%" % (cdtest['Elements'][i],cdtest['massFractions'][i]*100.0)) cdtest = xraylib.CompoundParser("SiO2") print ("SiO2 contains %g atoms and %i elements"% (cdtest['nAtomsAll'], cdtest['nElements'])) for i in range(cdtest['nElements']): print ("Element %i: %lf %%" % (cdtest['Elements'][i],cdtest['massFractions'][i]*100.0))