# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as itp
from math import *
def interp(x, max_gap=0.05):
sections = [[x[0]]]
# divide data into monotonic sections
for i in range(1, len(x)):
if x[i-1,0] < x[i,0] and x[i-1,1] >= x[i,1]:
sections[-1].append(x[i])
else:
sections.append([x[i]])
# interpolate within each section
for si, s in enumerate(sections):
if len(s) >= 2:
# use third-order polynomial of logarithmized data
spline = itp.make_interp_spline([log(d[0]) for d in s],
[np.log(d[1:]) for d in s], bc_type="natural")
for i in range(len(s) - 1, 0, -1):
nsub = log(s[i-1][0] / s[i][0]) / log(1 - max_gap)
if nsub > 1:
nsub = int(ceil(nsub))
xnew = s[i-1][0] * (s[i][0] / s[i-1][0]) ** (np.arange(1, nsub) / nsub)
s = s[:i] + [np.concatenate(([xnew[j]], d)) for j, d in enumerate(np.exp(spline(np.log(xnew))))] + s[i:]
sections[si] = s
return np.concatenate(sections)
# data from https://physics.nist.gov/PhysRefData/XrayMassCoef/tab3.html
mu_H = interp(np.fromstring("""
1.90000E-4 1.000E+3 1.00000E-3 7.217E+0 1.50000E-3 2.148E+0 2.00000E-3 1.059E+0
3.00000E-3 5.612E-1 4.00000E-3 4.546E-1 5.00000E-3 4.193E-1 6.00000E-3 4.042E-1
8.00000E-3 3.914E-1 1.00000E-2 3.854E-1 1.50000E-2 3.764E-1 2.00000E-2 3.695E-1
3.00000E-2 3.570E-1 4.00000E-2 3.458E-1 5.00000E-2 3.355E-1 6.00000E-2 3.260E-1
8.00000E-2 3.091E-1 1.00000E-1 2.944E-1 1.50000E-1 2.651E-1 2.00000E-1 2.429E-1
3.00000E-1 2.112E-1
""", sep=" ").reshape((-1, 2)))
mu_C = interp(np.fromstring("""
1.00000E-3 2.211E+3 1.50000E-3 7.002E+2 2.00000E-3 3.026E+2 3.00000E-3 9.033E+1
4.00000E-3 3.778E+1 5.00000E-3 1.912E+1 6.00000E-3 1.095E+1 8.00000E-3 4.576E+0
1.00000E-2 2.373E+0 1.50000E-2 8.071E-1 2.00000E-2 4.420E-1 3.00000E-2 2.562E-1
4.00000E-2 2.076E-1 5.00000E-2 1.871E-1 6.00000E-2 1.753E-1 8.00000E-2 1.610E-1
1.00000E-1 1.514E-1 1.50000E-1 1.347E-1 2.00000E-1 1.229E-1 3.00000E-1 1.066E-1
""", sep=" ").reshape((-1, 2)))
mu_N = interp(np.fromstring("""
1.00000E-3 3.311E+3 1.50000E-3 1.083E+3 2.00000E-3 4.769E+2 3.00000E-3 1.456E+2
4.00000E-3 6.166E+1 5.00000E-3 3.144E+1 6.00000E-3 1.809E+1 8.00000E-3 7.562E+0
1.00000E-2 3.879E+0 1.50000E-2 1.236E+0 2.00000E-2 6.178E-1 3.00000E-2 3.066E-1
4.00000E-2 2.288E-1 5.00000E-2 1.980E-1 6.00000E-2 1.817E-1 8.00000E-2 1.639E-1
1.00000E-1 1.529E-1 1.50000E-1 1.353E-1 2.00000E-1 1.233E-1 3.00000E-1 1.068E-1
""", sep=" ").reshape((-1, 2)))
mu_O = interp(np.fromstring("""
1.00000E-3 4.590E+3 1.50000E-3 1.549E+3 2.00000E-3 6.949E+2 3.00000E-3 2.171E+2
4.00000E-3 9.315E+1 5.00000E-3 4.790E+1 6.00000E-3 2.770E+1 8.00000E-3 1.163E+1
1.00000E-2 5.952E+0 1.50000E-2 1.836E+0 2.00000E-2 8.651E-1 3.00000E-2 3.779E-1
4.00000E-2 2.585E-1 5.00000E-2 2.132E-1 6.00000E-2 1.907E-1 8.00000E-2 1.678E-1
1.00000E-1 1.551E-1 1.50000E-1 1.361E-1 2.00000E-1 1.237E-1 3.00000E-1 1.070E-1
""", sep=" ").reshape((-1, 2)))
mu_Na = interp(np.fromstring("""
1.00000E-3 6.542E+2 1.03542E-3 5.960E+2 1.07210E-3 5.429E+2 1.07210E-3 6.435E+3
1.50000E-3 3.194E+3 2.00000E-3 1.521E+3 3.00000E-3 5.070E+2 4.00000E-3 2.261E+2
5.00000E-3 1.194E+2 6.00000E-3 7.030E+1 8.00000E-3 3.018E+1 1.00000E-2 1.557E+1
1.50000E-2 4.694E+0 2.00000E-2 2.057E+0 3.00000E-2 7.197E-1 4.00000E-2 3.969E-1
5.00000E-2 2.804E-1 6.00000E-2 2.268E-1 8.00000E-2 1.796E-1 1.00000E-1 1.585E-1
1.50000E-1 1.335E-1 2.00000E-1 1.199E-1 3.00000E-1 1.029E-1
""", sep=" ").reshape((-1, 2)))
mu_P = interp(np.fromstring("""
1.00000E-3 1.913E+3 1.50000E-3 6.547E+2 2.00000E-3 3.018E+2 2.14550E-3 2.494E+2
2.14550E-3 2.473E+3 3.00000E-3 1.118E+3 4.00000E-3 5.242E+2 5.00000E-3 2.860E+2
6.00000E-3 1.726E+2 8.00000E-3 7.660E+1 1.00000E-2 4.035E+1 1.50000E-2 1.239E+1
2.00000E-2 5.352E+0 3.00000E-2 1.700E+0 4.00000E-2 8.096E-1 5.00000E-2 4.916E-1
6.00000E-2 3.494E-1 8.00000E-2 2.324E-1 1.00000E-1 1.865E-1 1.50000E-1 1.432E-1
2.00000E-1 1.250E-1 3.00000E-1 1.055E-1
""", sep=" ").reshape((-1, 2)))
mu_Ca = interp(np.fromstring("""
1.00000E-3 4.867E+3 1.50000E-3 1.714E+3 2.00000E-3 7.999E+2 3.00000E-3 2.676E+2
4.00000E-3 1.218E+2 4.03810E-3 1.187E+2 4.03810E-3 1.023E+3 5.00000E-3 6.026E+2
6.00000E-3 3.731E+2 8.00000E-3 1.726E+2 1.00000E-2 9.341E+1 1.50000E-2 2.979E+1
2.00000E-2 1.306E+1 3.00000E-2 4.080E+0 4.00000E-2 1.830E+0 5.00000E-2 1.019E+0
6.00000E-2 6.578E-1 8.00000E-2 3.656E-1 1.00000E-1 2.571E-1 1.50000E-1 1.674E-1
2.00000E-1 1.376E-1 3.00000E-1 1.116E-1
""", sep=" ").reshape((-1, 2)))
mu_Fe = interp(np.fromstring("""
1.00000E-3 9.085E+3 1.50000E-3 3.399E+3 2.00000E-3 1.626E+3 3.00000E-3 5.576E+2
4.00000E-3 2.567E+2 5.00000E-3 1.398E+2 6.00000E-3 8.484E+1 7.11200E-3 5.319E+1
7.11200E-3 4.076E+2 8.00000E-3 3.056E+2 1.00000E-2 1.706E+2 1.50000E-2 5.708E+1
2.00000E-2 2.568E+1 3.00000E-2 8.176E+0 4.00000E-2 3.629E+0 5.00000E-2 1.958E+0
6.00000E-2 1.205E+0 8.00000E-2 5.952E-1 1.00000E-1 3.717E-1 1.50000E-1 1.964E-1
2.00000E-1 1.460E-1 3.00000E-1 1.099E-1
""", sep=" ").reshape((-1, 2)))
mu_Sn = interp(np.fromstring("""
1.00000E-3 8.157E+3 1.50000E-3 3.296E+3 2.00000E-3 1.665E+3 3.00000E-3 6.143E+2
3.92880E-3 3.114E+2 3.92880E-3 9.285E+2 4.00000E-3 9.393E+2 4.15610E-3 8.469E+2
4.15610E-3 1.145E+3 4.30764E-3 1.060E+3 4.46470E-3 9.712E+2 4.46470E-3 1.117E+3
5.00000E-3 8.471E+2 6.00000E-3 5.294E+2 8.00000E-3 2.500E+2 1.00000E-2 1.384E+2
1.50000E-2 4.664E+1 2.00000E-2 2.146E+1 2.92001E-2 7.760E+0 2.92001E-2 4.360E+1
3.00000E-2 4.121E+1 4.00000E-2 1.942E+1 5.00000E-2 1.070E+1 6.00000E-2 6.564E+0
8.00000E-2 3.029E+0 1.00000E-1 1.676E+0 1.50000E-1 6.091E-1 2.00000E-1 3.260E-1
3.00000E-1 1.639E-1
""", sep=" ").reshape((-1, 2)))
mu_Pb = interp(np.fromstring("""
1.00000E-3 5.210E+3 1.50000E-3 2.356E+3 2.00000E-3 1.285E+3 2.48400E-3 8.006E+2
2.48400E-3 1.397E+3 2.53429E-3 1.726E+3 2.58560E-3 1.944E+3 2.58560E-3 2.458E+3
3.00000E-3 1.965E+3 3.06640E-3 1.857E+3 3.06640E-3 2.146E+3 3.30130E-3 1.796E+3
3.55420E-3 1.496E+3 3.55420E-3 1.585E+3 3.69948E-3 1.442E+3 3.85070E-3 1.311E+3
3.85070E-3 1.368E+3 4.00000E-3 1.251E+3 5.00000E-3 7.304E+2 6.00000E-3 4.672E+2
8.00000E-3 2.287E+2 1.00000E-2 1.306E+2 1.30352E-2 6.701E+1 1.30352E-2 1.621E+2
1.50000E-2 1.116E+2 1.52000E-2 1.078E+2 1.52000E-2 1.485E+2 1.55269E-2 1.416E+2
1.58608E-2 1.344E+2 1.58608E-2 1.548E+2 2.00000E-2 8.636E+1 3.00000E-2 3.032E+1
4.00000E-2 1.436E+1 5.00000E-2 8.041E+0 6.00000E-2 5.021E+0 8.00000E-2 2.419E+0
8.80045E-2 1.910E+0 8.80045E-2 7.683E+0 1.00000E-1 5.549E+0 1.50000E-1 2.014E+0
2.00000E-1 9.985E-1 3.00000E-1 4.031E-1
""", sep=" ").reshape((-1, 2)))
plt.figure()
plt.plot(mu_Pb[:,0] * 1e3, mu_Pb[:,1], label="$_{82}$Pb", color="#77ac30")
plt.plot(mu_Sn[:,0] * 1e3, mu_Sn[:,1], label="$_{50}$Sn", color="#0088bd")
plt.plot(mu_Fe[:,0] * 1e3, mu_Fe[:,1], label="$_{26}$Fe", color="#d95319")
plt.plot(mu_Ca[:,0] * 1e3, mu_Ca[:,1], label="$_{20}$Ca", color="#edb120")
plt.plot(mu_P[:,0] * 1e3, mu_P[:,1], label="$_{15}$P", color="#7e2f8e")
plt.plot(mu_Na[:,0] * 1e3, mu_Na[:,1], label="$_{11}$Na", color="#555555")
plt.plot(mu_O[:,0] * 1e3, mu_O[:,1], label="$_8$O", color="#cc1122")
plt.plot(mu_N[:,0] * 1e3, mu_N[:,1], label="$_7$N", color="#5577ff")
plt.plot(mu_C[:,0] * 1e3, mu_C[:,1], label="$_6$C", color="#000000")
plt.plot(mu_H[:,0] * 1e3, mu_H[:,1], label="$_1$H", color="#aaaaaa")
plt.gca().set_yscale('log')
plt.xlim(0, 250)
plt.ylim(1e-1, 1e3)
plt.ylabel(r"$\mu/\rho$ [cm${}^2$/g]")
plt.xlabel("E [keV]")
plt.legend(borderaxespad=0.8, framealpha=1)
plt.grid()
plt.tight_layout()
plt.savefig("X-ray_attenuation_spectra_elements_mass.svg")