write('build/Abschirmkonstante_Zink.tex', make_SI(sigma_zink, r' ', figures=2))
write('build/Abschirmkonstante_Zirkonium.tex', make_SI(sigma_zir, r' ', figures=2))

#Moseley-Diagramm

E_k = (E_zink, E_ger, E_zir)
Z   = (30,32,40) # Zn, Ge, Zr
E_k_wurzel = np.sqrt(E_k)
params = ucurve_fit(reg_linear, Z, E_k_wurzel)
m,b = params
write('build/hcRydbergonstante.tex', make_SI(4/3*m**2, r'\electronvolt', figures=1))
write('build/Rydbergonstante.tex', make_SI(4/3*m**2/(h*c), r'\per\meter', figures=1))

plt.clf
t_plot = np.linspace(25,45, 100)
plt.plot(t_plot , reg_linear(t_plot, *noms(params)), 'b-', label='Fit')
plt.plot(Z, E_k_wurzel, 'rx', label='Messdaten')
plt.xlabel(r'Kernladungszahl  $Z$')
plt.ylabel(r'$\sqrt{E_\textrm{k} \:/\: \si{\kilo\electronvolt}}$')
plt.legend(loc='best')
plt.savefig("build/Moseley_Diagramm.pdf")
plt.close

################################ FREQUENTLY USED CODE ################################
#
########## IMPORT ##########
# t, U, U_err = np.genfromtxt('data.txt', unpack=True)
# t *= 1e-3


########## ERRORS ##########
params_gitterkonstante = ucurve_fit(
    reg_linear, sin_phi_helium, lambda_helium)

g, offset = params_gitterkonstante                  # g in m, offset Einheitenfrei
write('build/gitterkonstante.tex', make_SI(g * 1e9, r'\nano\meter', figures=1))
write('build/offset.tex', make_SI(offset * 1e9, r'\nano\meter', figures=1))
write('build/Tabelle_messdaten_kalium.tex', make_table([phi_kalium*180/np.pi],[1]))
write('build/Tabelle_messdaten_natrium.tex', make_table([phi_natrium*180/np.pi],[1]))
write('build/Tabelle_messdaten_rubidium.tex', make_table([phi_rubidium*180/np.pi],[1]))

##### PLOT lineare Regression #####
t_plot = np.linspace(np.amin(sin_phi_helium), np.amax(sin_phi_helium), 2)
plt.xlim(t_plot[0] - 1 / np.size(sin_phi_helium) * (t_plot[-1] - t_plot[0]),
         t_plot[-1] + 1 / np.size(sin_phi_helium) * (t_plot[-1] - t_plot[0]))
plt.plot(t_plot,
         reg_linear(t_plot, *noms(params_gitterkonstante))* 1e9,
         'b-', label='Fit')
plt.plot(sin_phi_helium,
         lambda_helium * 1e9,
         'rx', label='Messdaten')
plt.ylabel(r'$\lambda \:/\: \si{\nano\meter}$')
plt.xlabel(r'$\sin(\varphi)$')
plt.legend(loc='best')
plt.tight_layout(pad=0, h_pad=1.08, w_pad=1.08)
plt.savefig('build/aufgabenteil_a_plot.pdf')
plt.clf()
#### Ende Plot ####

#### TABELLE ####
write('build/Tabelle_a.tex', make_table([lambda_helium*1e9, -phi_helium, -sin_phi_helium],[1, 3, 3]))
write('build/Tabelle_a_texformat.tex', make_full_table(
    source_table = 'build/Tabelle_Silber.tex',
    stacking = [0,2],              # Hier aufpassen: diese Zahlen bezeichnen diejenigen resultierenden Spaltennummern, die Multicolumns sein sollen
    units = [
    r'$N_\textrm{\Delta t}$',
    r'$t \:/\: \si{\second}$',
    r'$N_\textrm{\Delta t}$',
    r'$t \:/\: \si{\second}$']))         # default = '-'



params = ucurve_fit(reg_linear, t, np.log(noms(Ind)))                                       # linearer Fit
m,b = params
write('build/Indium_m.tex', make_SI(m*1e4, r'\per\second', '1e-4' ,figures=1))
write('build/Indium_b.tex', make_SI(b, r'', figures=1))
t_plot = np.linspace(0, 4000, 2)
plt.plot(t_plot, np.exp(reg_linear(t_plot, *noms(params))), 'b-', label='Fit')
plt.errorbar(t, noms(Ind), fmt='rx', yerr=stds(Ind), label='Messdaten')                      # mit Fehlerbalken
plt.yscale('log')                                                                            # logarithmische x-Achse
plt.xlabel(r'$t \:/\: \si{\second}$')
plt.ylabel(r'$ \text{ln}(N_\textrm{\Delta t}) $')
plt.legend(loc='best')
plt.savefig('build/Indium_plot.pdf')
plt.clf()

lambda_Ind = -m

T = np.log(2)/lambda_Ind # in s
write('build/Halbwertszeit_Indium_s.tex', make_SI(T, r'\second', figures=1))
T = T/60 # in min
write('build/Halbwertszeit_Indium_h.tex', make_SI(T, r'\minute', figures=1))
    units = [r'$U \:/\: \si{\volt}$',
    r'$Z$',
    r'$N \:/\: \si{\per\second}$',
    r'$I \:/\: 10^{10}\si{\micro\ampere}$',
    r'$\Delta Q \:/\: \si{\elementarycharge}$']))


##### Fit ####
no_of_first_ignored_values = 3
no_of_last_ignored_values = 5
params = ucurve_fit(reg_linear, U[no_of_first_ignored_values:-no_of_last_ignored_values],
N[no_of_first_ignored_values:-no_of_last_ignored_values])   # skip first 3 and last 5 values as they dont belong to the plateau
write('build/plateaulaenge.tex', make_SI(U[-no_of_last_ignored_values] - U[no_of_first_ignored_values], r'\volt', figures = 0))
a, b = params
write('build/parameter_a.tex', make_SI(a, r'\per\second\per\volt', figures=1))
write('build/plateaumitte.tex', make_SI(reg_linear(500, *noms(params)), r'\per\second', figures=1) )   # Der Wert in der Hälfte des Plateaus als Referenz für die plateausteigung
write('build/plateausteigung.tex', make_SI(a*100*100/reg_linear(500, *noms(params)), r'\percent', figures=1))  #   %/100V lässt sich nicht mit siunitx machen -> wird in latex hart reingeschrieben
write('build/parameter_b.tex', make_SI(b, r'\per\second', figures=1))

#### Plot ####
t_plot = np.linspace(350, 650, 2)
plt.xlim(290,710)
plt.plot(t_plot, reg_linear(t_plot, *noms(params)), 'b-', label='Fit')
U[no_of_first_ignored_values:-no_of_last_ignored_values]
plt.errorbar(U[no_of_first_ignored_values:-no_of_last_ignored_values],
    noms(N[no_of_first_ignored_values:-no_of_last_ignored_values]),
    fmt='bx', yerr=stds(N[no_of_first_ignored_values:-no_of_last_ignored_values]),
    label='Messdaten auf dem Plateau')
plt.errorbar(U[0:no_of_first_ignored_values],
    noms(N[0:no_of_first_ignored_values]),
    fmt='rx', yerr=stds(N[0:no_of_first_ignored_values]),
params_gitterkonstante = ucurve_fit(reg_linear, sin_phi_helium, lambda_helium)

g, offset = params_gitterkonstante  # g in m, offset Einheitenfrei
write("build/gitterkonstante.tex", make_SI(g * 1e9, r"\nano\meter", figures=1))
write("build/offset.tex", make_SI(offset * 1e9, r"\nano\meter", figures=1))
write("build/Tabelle_messdaten_kalium.tex", make_table([phi_kalium * 180 / np.pi], [1]))
write("build/Tabelle_messdaten_natrium.tex", make_table([phi_natrium * 180 / np.pi], [1]))
write("build/Tabelle_messdaten_rubidium.tex", make_table([phi_rubidium * 180 / np.pi], [1]))

##### PLOT lineare Regression #####
t_plot = np.linspace(np.amin(sin_phi_helium), np.amax(sin_phi_helium), 2)
plt.xlim(
    t_plot[0] - 1 / np.size(sin_phi_helium) * (t_plot[-1] - t_plot[0]),
    t_plot[-1] + 1 / np.size(sin_phi_helium) * (t_plot[-1] - t_plot[0]),
)
plt.plot(t_plot, reg_linear(t_plot, *noms(params_gitterkonstante)) * 1e9, "b-", label="Fit")
plt.plot(sin_phi_helium, lambda_helium * 1e9, "rx", label="Messdaten")
plt.ylabel(r"$\lambda \:/\: \si{\nano\meter}$")
plt.xlabel(r"$\sin(\varphi)$")
plt.legend(loc="best")
plt.tight_layout(pad=0, h_pad=1.08, w_pad=1.08)
plt.savefig("build/aufgabenteil_a_plot.pdf")
plt.clf()
#### Ende Plot ####

#### TABELLE ####
write("build/Tabelle_a.tex", make_table([lambda_helium * 1e9, -phi_helium, -sin_phi_helium], [1, 3, 3]))
write(
    "build/Tabelle_a_texformat.tex",
    make_full_table(
        "Messdaten zur Bestimmung der Gitterkonstante.",