File: GenerateEPDL97CrossSections.py

package info (click to toggle)
pymca 5.8.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,392 kB
  • sloc: python: 155,456; ansic: 15,843; makefile: 116; sh: 73; xml: 55
file content (345 lines) | stat: -rw-r--r-- 13,501 bytes parent folder | download | duplicates (3)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#/*##########################################################################
#
# The PyMca X-Ray Fluorescence Toolkit
#
# Copyright (c) 2004-2015 European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#############################################################################*/
__author__ = "V.A. Sole - ESRF Data Analysis"
__contact__ = "sole@esrf.fr"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__doc__= "Generate specfile from all EPL97 cross sections in keV and barn"
import os
import sys
import EADLSubshells
import EPDL97Parser as EPDLParser
Elements = EPDLParser.Elements
AVOGADRO_NUMBER = EPDLParser.AVOGADRO_NUMBER
import numpy
log = numpy.log
exp = numpy.exp
getTotalCoherentCrossSection = EPDLParser.getTotalCoherentCrossSection
getTotalIncoherentCrossSection = EPDLParser.getTotalIncoherentCrossSection
getTotalPhotoelectricCrossSection = EPDLParser.getTotalPhotoelectricCrossSection
getPartialPhotoelectricCrossSection = EPDLParser.getPartialPhotoelectricCrossSection
getTotalPairCrossSection = EPDLParser.getTotalPairCrossSection
getTotalTripletCrossSection = EPDLParser.getTotalTripletCrossSection

def getHeader(filename):
    text  = '#F %s\n' % filename
    text += '#U00 This file is a direct conversion to specfile format of \n'
    text += '#U01 the original EPDL97 photoelectric cross sections contained\n'
    text += '#U02 in the EPDL97.DAT file from the library.\n'
    text += '#U03 EPDL97 itself can be found at:\n'
    text += '#U04           http://www-nds.iaea.org/epdl97/libsall.htm\n'
    text += '#U05\n'
    text += '#U06 The command used to generate this file has been:\n'
    if len(sys.argv) > 3:
        text += '#U07 %s %s %s %s\n' % (os.path.basename(__file__),\
                                           sys.argv[1], sys.argv[2], sys.argv[3])
    else:
        text += '#U07 %s %s %s\n' % (os.path.basename(__file__),\
                                        sys.argv[1], sys.argv[2])
    text += '\n'
    return text

if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("Usage:")
        print("python EPDL97GenerateCrossSections SPEC_output_filename barns_flag [short_output_flag]")
        sys.exit(0)

    SHORT_OUTPUT_FLAG = 0
    if len(sys.argv) > 3:
        SHORT_OUTPUT_FLAG = int(sys.argv[3])


    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))

    shells = EADLSubshells.SHELL_LIST
    bad_shells = ['L (', 'L23',
                  'M (', 'M23', 'M45',
                  'N (', 'N23', 'N45', 'N67',
                  'O (', 'O23', 'O45', 'O67', 'O89',
                  'P (', 'P23', 'P45', 'P67', 'P89', 'P101',
                  'Q (', 'Q23', 'Q45', 'Q67']
    LONG_LABELS = True

    #find the first element for which EPDL has N1 or P1 shell attenuation data
    if SHORT_OUTPUT_FLAG:
        testShell = "N1"
    else:
        testShell = "P1"
    z = 0
    i = 0
    while z == 0:
        i += 1
        try:
            dummy = getPartialPhotoelectricCrossSection(i, testShell, getmode=True)
            z = i
        except IOError:
            pass

    firstNonZeroPhotoelectric = z

    for i in range(1, 101):
        print("i = %d element = %s" % (i, Elements[i-1]))
        #coherent
        energy_cohe, value_cohe, mode_cohe = getTotalCoherentCrossSection(i,
                                                                getmode=True)
        #incoherent
        energy_incohe, value_incohe, mode_incohe = getTotalIncoherentCrossSection(i,
                                                                getmode=True)

        #photoelectric
        energy_photo, value_photo, mode_photo = getTotalPhotoelectricCrossSection(i,
                                                                getmode=True)

        #check to see the energies:
        #for j in range(10):
        #    print energy_cohe[j], energy_incohe[j], energy_photo[j]

        #to select an appropriate energy grid as close as possible to the original
        #while keeping in mind the PyMca goals, I use the coherent energy grid till
        #the non-zero first value of the photoelectric cross section. At that point,
        #I use the photoelectric energy grid.
        energy = numpy.concatenate((energy_cohe[energy_cohe<energy_photo[0]],
                                   energy_photo))

        #now perform a log-log interpolation when needed
        #lin-lin interpolation:
        #
        #              y0 (x1-x) + y1 (x-x0)
        #        y = -------------------------
        #                     x1 - x0
        #
        #log-log interpolation:
        #
        #                  log(y0) * log(x1/x) + log(y1) * log(x/x0)
        #        log(y) = ------------------------------------------
        #                                  log (x1/x0)
        #
        cohe    = numpy.zeros(len(energy), numpy.float64)
        incohe  = numpy.zeros(len(energy), numpy.float64)
        photo   = numpy.zeros(len(energy), numpy.float64)
        total   = numpy.zeros(len(energy), numpy.float64)

        #get the partial photoelectric cross sections
        photo_dict = {}
        photo_label_list = []
        photo_long_label_list = []
        END = False
        for shell in EADLSubshells.SHELL_LIST:
            if shell[0:3] in bad_shells:
                continue
            if shell[0:4] in bad_shells:
                continue
            # do not generate partial photoelectric cross sections for these shells
            if SHORT_OUTPUT_FLAG:
                if shell[0] in ['N', 'O', 'P', 'Q']:
                    continue
            else:
                if shell[0] in ['P', 'Q']:
                    continue
            photo_long_label_list.append(shell)
            actual_shell = shell.replace(' ','').split("(")[0]
            photo_label_list.append(actual_shell)
            photo_dict[actual_shell] = {}
            ene = energy * 1
            v   = photo * 0.0
            value = photo * 0.0
            if not END:
                try:
                    ene, v, mode = getPartialPhotoelectricCrossSection(i, actual_shell,
                                                                    getmode=True)
                    #log-log interpolate in the final energy grid
                    value = photo * 0.0
                    for n in range(len(energy)):
                        x = energy[n]
                        if (x == ene[0]) and (energy[n+1] == x):
                            #avoid entering twice the absorption edges
                            continue
                        if x == ene[0]:
                            value[n]=v[0]
                            continue
                        if x < ene[0]:
                            continue
                        j1 = 0
                        while ene[j1] < x:
                            j1 += 1
                        j0 = j1 - 1
                        x0 = ene[j0]
                        x1 = ene[j1]
                        y0 = v[j0]
                        y1 = v[j1]
                        value[n] = exp((log(y0) * log(x1/x) + log(y1) * log(x/x0))/log(x1/x0))
                except IOError:
                    END = True
                    #print sys.exc_info()
                    ene = energy * 1
                    v   = photo * 0.0
            photo_dict[actual_shell]['read_energy'] = ene
            photo_dict[actual_shell]['read_value']  = v
            photo_dict[actual_shell]['value']  = value

        #coherent needs to interpolate
        indices = numpy.nonzero(energy_cohe<energy_photo[0])
        cohe[indices]  = value_cohe[indices]
        for n in range(len(indices),len(energy)):
            x = energy[n]
            j1 = len(indices)
            if energy_cohe[j1] == x:
                cohe[n] = value_cohe[j1]
                continue
            while energy_cohe[j1] < x:
                j1 += 1
            j0 = j1 - 1
            if j0 < 0:
                print(x, energy_cohe[0])
                raise ValueError("coherent")
            x0 = energy_cohe[j0]
            x1 = energy_cohe[j1]
            y0 = value_cohe[j0]
            y1 = value_cohe[j1]
            cohe[n] = exp((log(y0) * log(x1/x) + log(y1) * log(x/x0))/log(x1/x0))

        #compton needs to interpolate everything
        for n in range(len(energy)):
            x = energy[n]
            j1 = 0
            if energy_incohe[j1] == x:
                incohe[n] = value_incohe[j1]
                continue
            while energy_incohe[j1] < x:
                j1 += 1
            j0 = j1 - 1
            if j0 < 0:
                print(x, energy_incohe[0])
                raise ValueError("compton")
            x0 = energy_incohe[j0]
            x1 = energy_incohe[j1]
            y0 = value_incohe[j0]
            y1 = value_incohe[j1]
            incohe[n] = exp((log(y0) * log(x1/x) + log(y1) * log(x/x0))/log(x1/x0))

        #photoelectric does not need to interpolate anything
        j1 = 0
        for n in range(len(energy)):
            x = energy[n]
            if x < energy_photo[0]:
                continue
            j1 = 0
            photo[n] = value_photo[j1]
            j1 += 1
        photo[energy>=energy_photo[0]] = value_photo[:]


        #convert to keV and cut at 500 keV
        energy *= 1000.
        indices = numpy.nonzero(energy<=500.)
        energy = energy[indices]
        photo  = photo[indices]
        cohe   = cohe[indices]
        incohe = incohe[indices]

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

        #now I am ready to write a Specfile
        ele = Elements[i-1]
        text  = '#S %d %s\n' % (i, ele)
        text += '#N %d\n' % (7+len(photo_label_list))
        labels = '#L PhotonEnergy[keV]'
        labels += '  Rayleigh(coherent)[barn/atom]'
        labels += '  Compton(incoherent)[barn/atom]'
        labels += '  CoherentPlusIncoherent[barn/atom]'
        labels += '  Photoelectric[barn/atom]'
        if LONG_LABELS:
            for label in photo_long_label_list:
                labels += "  "+label.replace(" ","")+"[barn/atom]"
        else:
            for label in photo_label_list:
                labels += "  "+label+"[barn/atom]"
        labels += "  AllOthers[barn/atom]"
        labels += '  TotalCrossSection[barn/atom]\n'
        if not BARNS:
            labels = labels.replace("barn/atom", "cm2/g")
            factor = (1.0E-24*AVOGADRO_NUMBER)/EPDLParser.getAtomicWeights()[i-1]
        else:
            factor = 1.0
        text += labels
        if 0:
            fformat = "%g %g %g %g %g"
        else:
            fformat = "%.7E %.6E %.6E %.6E %.6E"
        outfile.write(text)
        cohe   *= factor
        incohe *= factor
        photo  *= factor
        total  *= factor
        for n in range(len(energy)):
            if energy[n] == (1000. * energy_photo[0]):
                # one additional line
                line = fformat % (energy[n],
                      cohe[n],
                      incohe[n],
                      cohe[n]+incohe[n],
                      0.0)
                for l in photo_label_list:
                    line += " 0."
                line += " 0.0 %.6E\n" % (cohe[n]+incohe[n])
                outfile.write(line)
            line = fformat % (energy[n],
                              cohe[n],
                              incohe[n],
                              cohe[n]+incohe[n],
                              photo[n])
            d = 0.0
            for l in photo_label_list:
                a = photo_dict[l]['value'][n] * factor
                #this tiny modification saves 20 Mbytes ...
                if a > 0.0:
                    line += " %.6E" % a
                else:
                    line += " 0."
                d += a
            restOfShells = photo[n]-d
            if (i < firstNonZeroPhotoelectric) or (restOfShells < 1.0E-7):
                line += " 0.0 %.6E\n" % (total[n])
            else:
                line += " %.6E %.6E\n" % (restOfShells, total[n])
            outfile.write(line)
        outfile.write('\n')
    outfile.close()