This file is indexed.

/usr/share/pyshared/PyMca/XRayTubeEbel.py is in pymca 4.5.0-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#/*##########################################################################
# Copyright (C) 2004-2011 European Synchrotron Radiation Facility
#
# This file is part of the PyMCA X-ray Fluorescence Toolkit developed at
# the ESRF by the Beamline Instrumentation Software Support (BLISS) group.
#
# This toolkit is free software; you can redistribute it and/or modify it 
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option) 
# any later version.
#
# PyMCA 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 General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# PyMCA; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307, USA.
#
# PyMCA follows the dual licensing model of Trolltech's Qt and Riverbank's PyQt
# and cannot be used as a free plugin for a non-free program. 
#
# Please contact the ESRF industrial unit (industry@esrf.fr) if this license 
# is a problem for you.
#############################################################################*/
import Elements
import numpy.oldnumeric as Numeric

DEBUG = 0

def continuumEbel(target, e0, e = None, window = None,
                  alphae = None, alphax = None,
                  transmission = None, targetthickness = None,
                  filterlist=None):
    """
    H. Ebel, X-Ray Spectrometry 28 (1999) 255-266
    Tube voltage from 5 to 50 kV
    Electron incident angle from 50 to 90 deg.
    X-Ray take off angle from 90 to 5 deg.
    def continuum(target, e, e0, window = None, mode=None)
    target = Tuple [Symbol, density, thickness] 
    e0 = Tube Voltage in kV     
    e  = Energy of interest
    window = Tube window [Formula, density, thickness]
    filterlist = Additional filters [[Formula, density, thickness], ...]
    """
    if type(target) == type([]):
        element = target[0]
        density = target[1]
        thickness = target[2]
    else:
        element   = target
        density   = Elements.Element[element]['density']
        thickness = 0.1
    if e is None:
        energy = Numeric.arange(e0 * 1.0)[1:]
    elif type(e) == type([]):
        energy = Numeric.array(e).astype(Numeric.Float)        
    elif type(e) == Numeric.ArrayType:
        energy = Numeric.array(e).astype(Numeric.Float)        
    else:
        energy = Numeric.array([e]).astype(Numeric.Float)

    if alphae is None:alphae = 75.0
    if alphax is None:alphax = 15.0
    if transmission is None:transmission = False

    sinalphae = Numeric.sin(Numeric.pi * alphae/180.)
    sinalphax = Numeric.sin(Numeric.pi * alphax/180.)
    sinfactor = sinalphae/sinalphax

    z = Elements.getz(element)
    const = 1.35e+09
    x = 1.109 - 0.00435*z + 0.00175 * e0


    #calculate intermediate constants from formulae (4) in Ebel's paper
    #eta in Ebel's paper
    m   = 0.1382 - 0.9211 / Numeric.sqrt(z)
    logz = Numeric.log(z)
    eta = 0.1904 - 0.2236 * logz + 0.1292 * pow(logz, 2) - 0.0149 * pow(logz, 3) 
    eta = eta * pow(e0, m) 

    #dephmax? in Ebel's paper
    p3 = 0.787e-05 * Numeric.sqrt(0.0135 * z) * pow(e0, 1.5) + \
         0.735e-06 * pow(e0, 2)
    rhozmax = (Elements.Element[element]['mass'] / z) * p3
    #print "max depth = ",2 * rhozmax

    #and finally we get rhoz 
    u0    = e0 / energy
    logu0 = Numeric.log(u0) 
    p1 = logu0 * (0.49269 - 1.09870 * eta + 0.78557 * pow(eta, 2))
    p2 = 0.70256 - 1.09865 * eta + 1.00460 * pow(eta, 2) + logu0
    rhoz = rhozmax * (p1 / p2)
    

    #the term dealing with the photoelectric absorption of the Bremsstrahlung
    tau = Numeric.array(Elements.getMaterialMassAttenuationCoefficients(element,
                                                          1.0,
                                                          energy)['photo'])

    if not transmission:
        rhelp = tau * 2.0 * rhoz * sinfactor
        if len(Numeric.nonzero(rhelp) <= 0.0):
            result = Numeric.zeros(Numeric.shape(rhelp), Numeric.Float)
            for i in range(len(rhelp)):
                if rhelp[i] > 0.0:
                    result [i] = const * z * pow(u0[i] - 1.0, x) * \
                         (1.0 - Numeric.exp(-rhelp[i])) / rhelp[i]
        else:
            result = const * z * pow(u0 - 1.0, x) * \
                 (1.0 - Numeric.exp(-rhelp)) / rhelp 

        #the term dealing with absorption in tube's window
        if window is not None:
            if window[2] != 0:
                w = Elements.getMaterialTransmission(window[0], 1.0, energy,
                                                density = window[1],
                                                thickness = window[2],
                                                listoutput=False)['transmission']
                result = result * w
        if filterlist is not None:
            w = 1
            for fwindow in filterlist:
                if fwindow[2] == 0:
                    continue
                w *= Elements.getMaterialTransmission(fwindow[0], 1.0, energy,
                                                density = fwindow[1],
                                                thickness = fwindow[2],
                                                listoutput=False)['transmission']
            result = result * w                
        return result
    #transmission case
    if targetthickness is None:
        #d = Elements.Element[target]['density']
        d = density
        ttarget = 2 * rhozmax
        print("WARNING target thickness assumed equal to maximum depth of %f cm" % (ttarget/d))
    else:
        #ttarget = targetthickness * Elements.Element[target]['density']
        ttarget = targetthickness * density
    generationdepth = min(ttarget, 2 * rhozmax)
    rhelp = tau * 2.0 * rhoz * sinfactor
    if len(Numeric.nonzero(rhelp) <= 0.0):
        result = Numeric.zeros(Numeric.shape(rhelp), Numeric.Float)
        for i in range(len(rhelp)):
            if rhelp[i] > 0.0:
                result [i] = const * z * pow(u0[i] - 1.0, x) * \
                     (Numeric.exp(-tau[i] *(ttarget-2.0*rhoz[i])/sinalphax)-Numeric.exp(-tau[i]*ttarget/sinalphax)) / rhelp[i]
    else:
        result = const * z * pow(u0 - 1.0, x) * \
             (Numeric.exp(-tau *(ttarget-2.0*rhoz)/sinalphax)-Numeric.exp(-tau*ttarget/sinalphax)) / rhelp
    #the term dealing with absorption in tube's window
    if window is not None:
        if window[2] != 0.0 :
            w = Elements.getMaterialTransmission(window[0], 1.0, energy,
                                            density = window[1],
                                            thickness = window[2]/sinalphax,
                                            listoutput=False)['transmission']
            result = result * w
    if filterlist is not None:
        for fwindow in filterlist:
            if fwindow[2] == 0:
                continue
            w = Elements.getMaterialTransmission(fwindow[0], 1.0, energy,
                                            density = fwindow[1],
                                            thickness = fwindow[2],
                                            listoutput=False)['transmission']
            result = result * w 
    return result

def characteristicEbel(target, e0, window = None,
                       alphae = None, alphax = None,
                       transmission = None, targetthickness = None,
                       filterlist=None):
    """
    H. Ebel, X-Ray Spectrometry 28 (1999) 255-266
    Tube voltage from 5 to 50 kV
    Electron incident angle from 50 to 90 deg.
    X-Ray take off angle from 90 to 5 deg.
    def continuum(target, e, e0, window = None, mode=None)
    target = Tuple [Symbol, density, thickness] 
    e0 = Tube Voltage in kV     
    e  = Energy of interest
    window = Tube window [Formula, density, thickness] 
    """
    if type(target) == type([]):
        element = target[0]
        density = target[1]
        thickness = target[2]
        if targetthickness is None:
            targetthickness = target[2] 
    else:
        element   = target
        density   = Elements.Element[element]['density']
        thickness = 0.1

    if alphae is None:alphae = 75.0
    if alphax is None:alphax = 15.0
    if transmission is None:transmission = False

    sinalphae = Numeric.sin(Numeric.pi * alphae/180.)
    sinalphax = Numeric.sin(Numeric.pi * alphax/180.)
    sinfactor = sinalphae/sinalphax

    z = Elements.getz(element)
    const = 6.0e+13
    #K Shell
    energy = Elements.Element[element]['binding']['K']
    #get the energy of the characteristic lines
    lines = Elements._getUnfilteredElementDict(element, None, photoweights = True)

    if 0:
        #L shell lines will have to be entered directly by the user
        #L shell 
        lpeaks = []
        for label in lines['L xrays']:
            lpeaks.append([lines[label]['energy'],
                              lines[label]['rate'], 
                              element+' '+label])
        lfluo = Elements._filterPeaks(peaklist, ethreshold = 0.020,
                                            ithreshold = 0.001,
                                            nthreshold = 6,
                                            absoluteithreshold = False,
                                            keeptotalrate = True)
        lfluo.sort()
    peaklist = []
    rays = 'K xrays'
    if rays in lines.keys():
        #K shell
        for label in lines[rays]:
            peaklist.append([lines[label]['energy'],
                              lines[label]['rate'], 
                              element+' '+label])
        fl = Elements._filterPeaks(peaklist, ethreshold = 0.020,
                                        ithreshold = 0.001,
                                        nthreshold = 4,
                                        absoluteithreshold = False,
                                        keeptotalrate = True)
        
        fl.sort()
        if (energy > 0) and (e0 > energy):
            zk = 2.0
            bk = 0.35
        else:
            for i in range(len(fl)):
                fl[i][1] = 0.00
            return fl

    u0    = e0 / energy
    logu0 = Numeric.log(u0) 

    #stopping factor
    oneovers = (Numeric.sqrt(u0) * logu0 + 2 * (1.0 - Numeric.sqrt(u0)))
    oneovers = oneovers / (u0 * logu0 + 1.0 - u0)
    oneovers = 1.0 + 16.05 * Numeric.sqrt(0.0135*z/energy) * oneovers
    oneovers = (zk*bk/z) * (u0 * logu0 + 1.0 - u0) * oneovers

    #backscattering factor
    r = 1.0 - 0.0081517 * z + 3.613e-05 * z * z +\
        0.009583 * z * Numeric.exp(-u0) + 0.001141 * e0

    #Absorption correction
    #calculate intermediate constants from formulae (4) in Ebel's paper
    #eta in Ebel's paper
    m   = 0.1382 - 0.9211 / Numeric.sqrt(z)
    logz = Numeric.log(z)
    eta = 0.1904 - 0.2236 * logz + 0.1292 * pow(logz, 2) - 0.0149 * pow(logz, 3) 
    eta = eta * pow(e0, m) 

    #depmax? in Ebel's paper
    p3 = 0.787e-05 * Numeric.sqrt(0.0135 * z) * pow(e0, 1.5) + \
         0.735e-06 * pow(e0, 2)
    rhozmax = (Elements.Element[element]['mass'] / z) * p3

    #and finally we get rhoz 
    p1 = logu0 * (0.49269 - 1.09870 * eta + 0.78557 * pow(eta, 2))
    p2 = 0.70256 - 1.09865 * eta + 1.00460 * pow(eta, 2) + logu0
    rhoz = rhozmax * (p1 / p2)

    #the term dealing with the photoelectric absorption
    energylist = []
    for i in range(len(fl)):
        energylist.append(fl[i][0])
    tau = Numeric.array(Elements.getMaterialMassAttenuationCoefficients(element,
                                                              1.0,
                                                              energylist)['photo'])
    if not transmission:
        rhelp = tau * 2.0 * rhoz * sinfactor
        w = None
        if window is not None:
            if window[2] != 0.0:
                w = Elements.getMaterialTransmission(window[0], 1.0,
                                            energylist,
                                            density = window[1],
                                            thickness = window[2],
                                            listoutput=False)['transmission']
        if filterlist is not None:
            for fwindow in filterlist:
                if fwindow[2] == 0:
                    continue
                if w is None:
                    w = Elements.getMaterialTransmission(fwindow[0], 1.0,
                                            energylist,
                                            density = fwindow[1],
                                            thickness = fwindow[2],
                                            listoutput=False)['transmission']
                else:
                    w *= Elements.getMaterialTransmission(fwindow[0], 1.0,
                                            energylist,
                                            density = fwindow[1],
                                            thickness = fwindow[2],
                                            listoutput=False)['transmission']
        for i in range(len(fl)):
            if rhelp[i] > 0.0 :
                rhelp[i] = (1.0 - Numeric.exp(-rhelp[i])) / rhelp[i]
            else:
                rhelp[i] = 0.0 
            intensity = const * oneovers * r * Elements.getomegak(element) * rhelp[i]
            #the term dealing with absorption in tube's window
            if w is not None:
                intensity = intensity * w[i]
            fl[i][1] = intensity * fl[i][1]
        return fl

    #transmission case
    if targetthickness is None:
        d = density
        ttarget = 2 * rhozmax
        print("WARNING target thickness assumed equal to maximum depth of %f cm" % (ttarget/d))
    else:
        ttarget = targetthickness * density
    generationdepth = min(ttarget, 2 * rhozmax)
    rhelp = tau * 2.0 * rhoz * sinfactor
    w = None
    if (window is not None) or (filterlist is not None):
        if window is not None:
            if window[2] != 0.0:
                w = Elements.getMaterialTransmission(window[0], 1.0,
                                        energylist,
                                        density = window[1],
                                        thickness = window[2]/sinalphax,
                                        listoutput=False)['transmission']
        if filterlist is not None:
            for fwindow in filterlist:
                if w is None:
                    w = Elements.getMaterialTransmission(fwindow[0], 1.0,
                                            energylist,
                                            density = fwindow[1],
                                            thickness = fwindow[2],
                                            listoutput=False)['transmission']
                else:
                    w *= Elements.getMaterialTransmission(fwindow[0], 1.0,
                                            energylist,
                                            density = fwindow[1],
                                            thickness = fwindow[2],
                                            listoutput=False)['transmission']
        for i in range(len(fl)):
            if rhelp[i] > 0.0:
                rhelp[i] = (Numeric.exp(-tau[i] *(ttarget-2.0*rhoz)/sinalphax)-Numeric.exp(-tau[i]*ttarget/sinalphax)) / rhelp[i]
            else:
                rhelp[i] = 0.0
            intensity = const * oneovers * r * Elements.getomegak(element) * rhelp[i]
            if w is not None:
                intensity = intensity * w[i]
            fl[i][1] = intensity * fl[i][1]
    return fl


def generateLists(target, e0, window = None,
                  alphae = None, alphax = None,
                  transmission = None, targetthickness=None,
                  filterlist=None):
    e0w = 1.0 * e0
    x1min =  1.4
    step1 =  0.2
    #x1min = 8.0
    #step1 =  0.15
    x2min = min(e0-2*step1, 20.0)
    #step2 =  0.3
    if x2min < 20:
        step2 = step1
    else:
        step2 =  0.5
    x3min = e0w
    x1    = Numeric.arange(x1min, x2min+step1, step1)
    x2    = Numeric.arange(x2min+step1, x3min, step2)

    #get K shell characteristic lines and intensities
    fllines = characteristicEbel(target, e0, window,
                                 alphae=alphae, alphax=alphax,
                                 transmission = transmission,
                                 targetthickness=targetthickness,
                                 filterlist=filterlist)
    
    energy = Numeric.ones(len(x1) + len(x2)).astype(Numeric.Float)
    energy[0:len(x1)] = energy[0:len(x1)] * x1
    energy[len(x1):(len(x1)+len(x2))] = energy[len(x1):(len(x1)+len(x2))] * x2
    #energy[(len(x1)+len(x2)):] = energy[(len(x1)+len(x2)):] * x3
    #print "len(energy) = ",len(energy)
    energyweight = continuumEbel(target, e0, energy, window,
                                 alphae=alphae, alphax=alphax,
                                 transmission = transmission,
                                 targetthickness=targetthickness,
                                 filterlist=filterlist)
    energyweight[0:len(x1)] *= step1
    energyweight[len(x1):(len(x1)+len(x2))] *= step2
    energyweight[len(x1)] *= (energy[len(x1)] - energy[len(x1) - 1])/step2 
    finalenergy = Numeric.zeros(len(fllines)+len(energyweight), Numeric.Float)
    finalweight = Numeric.zeros(len(fllines)+len(energyweight), Numeric.Float)
    scatterflag = Numeric.zeros(len(fllines)+len(energyweight))
    finalenergy[len(fllines):] = energy[0:]
    finalweight[len(fllines):] = energyweight[0:]/1.0e7
    for i in range(len(fllines)):
        finalenergy[i] = fllines[i][0]
        finalweight[i] = fllines[i][1]/1.0e7
        scatterflag[i] = 1
    return finalenergy, finalweight, scatterflag
    
    
if __name__ == "__main__":
    import sys
    import getopt
    options = ''
    longoptions = ['target=', 'voltage=', 'wele=', 'window=', 'wthickness=','anglee=', 'anglex=',
                   'cfg=', 'deltae=', 'transmission=','tthickness=']
    opts, args = getopt.getopt(
        sys.argv[1:],
        options,
        longoptions)
    target  = 'Ag'
    voltage = 40
    wele    = 'Be'
    wthickness = 0.0125
    anglee     = 70
    anglex     = 50
    cfgfile = None
    transmission = None
    ttarget = None
    filterlist = None
    for opt,arg in opts:
        if opt in ('--target'):
            target = arg
        elif opt in ('--tthickness'):
            ttarget = float(arg)
        if opt in ('--cfg'):
            cfgfile = arg
        if opt in ('--voltage'):
            voltage = float(arg)
        if opt in ('--wthickness'):
            wthickness = float(arg)
        if opt in ('--wele','window'):
            wele = arg
        if opt in ('--transmission'):
            transmission=int(arg)
        if opt in ('--anglee', '--alphae'):
            anglee=float(arg)
        if opt in ('--anglex', '--alphax'):
            anglex=float(arg)
    try:
        e = Numeric.arange(voltage*10+1)[1:]/10
        y= continuumEbel(target, voltage, e,
                        [wele, Elements.Element[wele]['density'], wthickness],
                         alphae=anglee, alphax=anglex,
                         transmission=transmission,
                         targetthickness=ttarget,
                         filterlist=filterlist)
        fllines = characteristicEbel(target, voltage,
                        [wele, Elements.Element[wele]['density'], wthickness],
                         alphae=anglee, alphax=anglex,
                         transmission=transmission,
                         targetthickness=ttarget,
                         filterlist=filterlist)
        fsum = 0.0
        for l in fllines:
            print("%s %.4f %.3e" % (l[2],l[0],l[1]))
            fsum += l[1]
        energy, weight, scatter =  generateLists(target, voltage,
                        [wele, Elements.Element[wele]['density'], wthickness],
                         alphae=anglee, alphax=anglex,
                         transmission=transmission, targetthickness=ttarget,
                         filterlist=filterlist)

        f = open("Tube_%s_%.1f_%s_%.5f_ae%.1f_ax%.1f.txt" % (target,voltage,wele,wthickness,anglee,anglex),"w+")
        text = "energyweight="
        for i in range(len(energy)):
            if i == 0:
                text +=" %f" % weight[i]
            else:
                text +=", %f" % weight[i]
        text +="\n"
        f.write(text)
        text = "energy="
        for i in range(len(energy)):
            if i == 0:
                text +=" %f" % energy[i]
            else:
                text +=", %f" % energy[i]
        text +="\n"
        f.write(text)
        text = "energyflag="
        for i in range(len(energy)):
            if i == 0:
                text +=" %f" % 1
            else:
                text +=", %f" % 1
        text +="\n"
        f.write(text)
        text = "energyscatter="
        for i in range(len(energy)):
            if i == 0:
                text +=" %f" % scatter[i]
            else:
                text +=", %f" % scatter[i]
        text +="\n"
        f.write(text)
        f.close()
    except:
        print("Usage:")
        print("options = ",longoptions)
        sys.exit(0)