"""
This module use the xraylib library
and provide high level functions for
absorption factors for chemical compounds
August 2012 by OT
based on work by AT
"""
from string import *
import numpy
import os
from pySAXS import xraylib
ATOMS_NAME=["Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon, Graphite","Nitrogen","Oxygen","Fluorine","Neon","Sodium","Magnesium","Aluminum","Silicon","Phosphorus","Sulfur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium","Iodine","Xenon","Cesium","Barium","Lanthanum","Cerium","Praseodymium","Neodymium","Promethium","Samarium","Europium","Gadolinium","Terbium","Dysprosium","Holmium","Erbium","Thulium","Ytterbium","Lutetium","Hafnium","Tantalum","Tungsten","Rhenium","Osmium","Iridium","Platinum","Gold","Mercury","Thallium","Lead","Bismuth","Polonium","Astatine","Radon","Francium","Radium","Actinium","Thorium","Protactinium","Uranium"]
COMMON_XRAY_SOURCE_MATERIALS=['Cu','Cr','Mo','Co ']
[docs]def getZName(name):
'''
return the Atomic Number Z from compound name
>>> getZName("Copper")
29
'''
Z=ATOMS_NAME.index(name)+1
return Z
[docs]def getNameZ(Z):
'''
return the name from a Z
>>> getNameZ(29)
'Copper'
'''
return ATOMS_NAME[Z-1]
[docs]def getMuZ(Z,energy=8.028):
'''
return Mu from Atomic number Z
>>> getMuZ(4)
0.8719998598098755
'''
return xraylib.CS_Photo(Z,energy)
[docs]def getMuSymbol(S,energy=8.028):
"""
This function returns the xray absorption coefficient of the atom with
symbole S (ie 'C' fo carbon, 'Au' for gold) by default energy is 8.03 keV
and mass-energy absorption coefficient are return. To get the mass
absorption coefficient ISEN has to be 0
"""
Z=xraylib.AtomicNumberToSymbol(S)
return getMuZ(Z ,energy)
[docs]def getMuName(name,energy=8.028):
'''
This function returns the xray absorption coefficient of the atom with
name S (ie 'Carbon' , 'Gold') by default energy is 8.028 keV and mass-energy
absorption coefficient are return. To get the mass absorption coefficient
'''
Z=getZName(name)
return getMuZ(Z,energy)
[docs]def getMasseZ(Z):
'''
returns the molar mass (atomic weight) of the atomic number Z
'''
return xraylib.AtomicWeight(Z)
[docs]def getMasseSymbol(S):
'''
returns the molar mass (atomic weight) of the atom with atomic number Z
(ie 'C' fo carbon, 'Au' for gold)
'''
Z=xraylib.SymbolToAtomicNumber(S)
return getMasseZ(Z)
[docs]def getElectronNumber(S):
S,N=getAtomsFormula(S)
Z=0
for i in numpy.arange(len(S)):
Z=Z+(xraylib.AtomicWeight(xraylib.SymbolToAtomicNumber(S[i])))*N[i]
return Z
[docs]def getElectronDensity(S,rho):
'''
return the electron density and the scattering length density
'''
#Thomson scattering length
Belec=0.282e-12
Navo=xraylib.AVOGNUM*1e23
M=getMasseFormula(S)
Z=getElectronNumber(S)
ED=(rho*Z*Navo)/M
return ED,ED*Belec
[docs]def getEnergyFromSource(source='Cu'):
'''
return the KA LINE (most used) energy from the x-ray source
>>> getEnergyFromSource('Cu')
8.04105057076251
>>> getEnergyFromSource('Mo')
17.443217030477935
'''
Z=xraylib.SymbolToAtomicNumber(source)
return xraylib.LineEnergy(Z,xraylib.KA_LINE)
[docs]def getAngstFromSource(source='Cu'):
'''
return the KA LINE (most used) energy in ANGSTROM from the x-ray source
>>> getAngstFromSource('Cu')
1.5418904085842968
'''
Z=xraylib.SymbolToAtomicNumber(source)
return xraylib.KEV2ANGST/xraylib.LineEnergy(Z,xraylib.KA_LINE)
[docs]def getTransmission(formula,thickness=1.0,density=1.0,energy=8.03):
'''
return the transmission of a compound
thickness in cm
>>> getTransmission('H 2 O 1',0.1,density=1.0,energy=8.03)
0.376802369048
'''
#calculate mu for compound
mu=getMuFormula(formula,energy)
#calculate Transmission
return numpy.exp(-density*mu*thickness)
if __name__== '__main__':
formula="H 2 O 1"
density=1.0
thickness=0.1 #1 mm
source='Cu'
energy=getEnergyFromSource(source)
print "energy for ",source," : ",energy," keV ->",getAngstFromSource(source),' A'
print "Calculate Mu from formula for water : ",formula
mu=getMuFormula(formula,energy)
print mu
print "X-ray transmission = ",numpy.exp(-density*mu*thickness)
print getTransmission(formula,thickness,density,energy)
print "--------------------"
source='Mo'
energy=getEnergyFromSource(source)
print "energy for ",source," : ",energy," keV ->",getAngstFromSource(source),' A'
print "Calculate Mu from formula for water : ",formula
mu=getMuFormula(formula,energy)
print mu
print "X-ray transmission = ",numpy.exp(-density*mu*thickness)
print getTransmission(formula,thickness,density,energy)
print "--------------------"