File: GenerateXCOMCrossSections.py

package info (click to toggle)
pymca 4.7.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 52,352 kB
  • ctags: 9,570
  • sloc: python: 116,490; ansic: 18,322; cpp: 826; sh: 57; xml: 24; makefile: 19
file content (120 lines) | stat: -rw-r--r-- 3,784 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#/*##########################################################################
# Copyright (C) 2012 European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This file is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
#############################################################################*/
__doc__= "Generate specfile from XCOM generated files"
__author__ = "V.A. Sole - ESRF Software Group"
import sys
import os
import numpy
from PyMca import Elements

if len(sys.argv) < 3:
    print("Usage:")
    print("python GenerateXCOMTotalCrossSections SPEC_output_filename Barns_flag")
    sys.exit(0)

def getHeader(filename):
    text  = '#F %s\n' % filename
    text += '#U00 This file is a direct conversion to specfile format of \n'
    text += '#U01 the XCOM selected-arrays output.\n'
    text += '#U02 \n'
    text += '#U03 XCOM itself can be found at:\n'
    text += '#U04   http://www.nist.gov/pml/data/xcom/index.cfm\n'
    text += '\n'
    return text

fname = sys.argv[1]
if os.path.exists(fname):
    os.remove(fname)

if int(sys.argv[2]):
    BARNS = True
else:
    BARNS = False
print("BARNS = %s" % BARNS)
outfile = open(fname, 'wb')
outfile.write(getHeader(fname))

for i in range(1, 101):
    ele = Elements.getsymbol(i)
    print("i = %d element = %s" % (i, ele))
    # force data readout
    dataDict = Elements.getelementmassattcoef(ele)

    # pure XCOM data
    dataDict = Elements.Element[ele]['xcom']

    # energy (keV)
    energy = dataDict['energy']

    # coherent (cm2/g)
    cohe = dataDict['coherent']
    
    # incoherent
    incohe = dataDict['compton']
    
    # photoelectric
    photo = dataDict['photo']

    # photoelectric
    pair = dataDict['pair']

    # total
    total = dataDict['total']

    # convert to keV and cut at 500 keV not done for XCOM
    # indices = numpy.nonzero(energy<=500.)
    # energy = energy[indices]
    # photo  = photo[indices]
    # cohe   = cohe[indices]
    # incohe = incohe[indices]

    # I do not cut at 500 keV. I need to take the pair production
    total = photo + cohe + incohe + pair

    #now I am ready to write a Specfile
    text  = '#S %d %s\n' % (i, ele)
    text += '#N 5\n'
    labels = '#L PhotonEnergy[keV]'
    labels += '  Rayleigh(coherent)[barn/atom]' 
    labels += '  Compton(incoherent)[barn/atom]'
    labels += '  CoherentPlusIncoherent[barn/atom]'
    labels += '  Photoelectric[barn/atom]'
    labels += '  PairProduction[barn/atom]'
    labels += '  TotalCrossSection[barn/atom]\n'
    if not BARNS:
        labels = labels.replace("barn/atom", "cm2/g")
        factor = 1.0
    else:
        factor = Elements.Element[ele]['mass'] /(1.0E-24*AVOGADRO_NUMBER)
    text += labels
    if 0:
        fformat = "%g %g %g %g %g %g %g\n"
    else:
        fformat = "%.6E %.6E %.6E %.6E %.6E %.6E %.6E\n"
    outfile.write(text)
    for n in range(len(energy)):
        line = fformat % (energy[n],
                          cohe[n] * factor,
                          incohe[n] * factor,
                          (cohe[n] + incohe[n]) * factor,
                          photo[n] * factor,
                          pair[n] * factor,
                          total[n] * factor)
        outfile.write(line)
    outfile.write('\n')
outfile.close()