# plt.axvline(x= (noms(w_0)/(1000*2*np.pi))+(noms(w_pl)/(1000*2*np.pi)) , color='r', linestyle='--', label=r'Omega 0')
# plt.axvline(x= (noms(w_0)/(1000*2*np.pi))+(noms(w_mi)/(1000*2*np.pi)) , color='g', linestyle='--', label=r'Omega 0')
plt.ylabel(r'$U_c/U_er $')
plt.xlabel(r'$v \:/\: \si{\kilo\hertz}$')
plt.legend(loc='best')
plt.tight_layout(pad=0, h_pad=1.08, w_pad=1.08)
plt.savefig('build/U_gegen_v.pdf')
plt.clf()

#Zoom
Indem_max = np.argmax(U_c_ges/U_er_ges)

# positive Flanke
params = ucurve_fit(reg.reg_linear, noms(fre_ges[Indem_max-6:Indem_max-1]) ,  noms(U_c_ges[Indem_max-6:Indem_max-1]/U_er_ges[Indem_max-6:Indem_max-1]))
t_plot = np.linspace(32.5, 33.95, 2)
plt.plot(t_plot, reg.reg_linear(t_plot, *noms(params)), 'b-', label='$Fit_\t{1}$')


X = np.array([((np.amax(U_c_ges/U_er_ges))/unp.sqrt(2) - params[1]) / params[0]])

print('X')
print(X)

# negative Flanke
params = ucurve_fit(reg.reg_linear, noms(fre_ges[Indem_max+1:Indem_max+6]) , noms(U_c_ges[Indem_max+1:Indem_max+6]/U_er_ges[Indem_max+1:Indem_max+6]))
t_plot = np.linspace(33.95, 35, 2)
plt.plot(t_plot, reg.reg_linear(t_plot, *noms(params)), 'y-', label='$Fit_\t{2}$')

Y = np.array([((np.amax(U_c_ges/U_er_ges)/unp.sqrt(2)) - params[1]) / params[0]])
print('Y')
print(Y)
# write('build/D2.tex', make_SI(D2*1e5, r'\kilogram\square\meter\per\square\second', 'e-5', figures=1))

y = 4*np.pi**2*(Theta_Kugel+Theta_Aufhaengung)/T**2
params = ucurve_fit(reg.reg_linear, B, y)         # linearer Fit
m, D = params
write('build/m.tex', make_SI(m*1e3, r'\ampere\square\meter', 'e-3', figures=1))
write('build/D.tex', make_SI(D*1e5, r'\kilogram\square\meter\per\square\second', 'e-5', figures=1))
# D = 4*(np.pi**2)*(Theta_Kugel+Theta_Aufhaengung)/(T**2)
m_th = 1/B * (4*(np.pi**2) * (Theta_Kugel+Theta_Aufhaengung) / T**2 - D_ohneB)
m_th_unc = ufloat(np.mean(noms(m_th)), MeanError(noms(m_th)))
write('build/m_th.tex', make_SI(m_th_unc*1e3, r'\ampere\square\meter', 'e-3', figures=1))
# print(m_th)

t_plot = np.linspace(np.amin(B), np.amax(B), 100)
#
plt.plot(t_plot*1e3, reg.reg_linear(t_plot, *noms(params))*1e5, 'b-', label='Methode 1')
plt.plot(t_plot*1e3, reg.reg_linear(t_plot, np.mean(noms(m_th)), np.mean(noms(D)))*1e5, 'g-', label='Methode 2')
# plt.plot(B * 1e3, noms(y)*1e5, 'rx', label='Messdaten')
plt.errorbar(B * 1e3, noms(y) * 1e5, fmt='r.', yerr=stds(y) * 1e5, label='Messdaten')
## plt.xscale('log')    # logarithmische x-Achse
plt.xlim((t_plot[0]-1/np.size(B)*(t_plot[-1]-t_plot[0]))*1e3, (t_plot[-1]+1/np.size(B)*(t_plot[-1]-t_plot[0]))*1e3)
plt.xlabel(r'$B \:/\: \SI{e-3}{\tesla}$')
plt.ylabel(r'$\frac{4\pi^2 \Theta_\text{Gesamt}}{T^2} \:/\: \SI{e-5}{\kilogram\square\meter\per\square\second}$')
plt.legend(loc='best')
plt.tight_layout(pad=0, h_pad=1.08, w_pad=1.08)
plt.savefig('build/zeta.pdf')

# Berechnung des Erdmagnetfeldes
B_Erde = (D_mitB-D_ohneB)/m
write('build/B_Erde.tex', make_SI(B_Erde*1e6, r'\micro\tesla', figures=1))
#
# plt.plot(t_plot, reg.reg_linear(t_plot, *noms(params)), 'b-', label='Fit')
# plt.xlim(t_plot[0], t_plot[-1])
# # plt.xlabel(r'$t \:/\: \si{\milli\second}$')
# # plt.ylabel(r'$U \:/\: \si{\kilo\volt}$')
# plt.legend(loc='best')
# plt.tight_layout(pad=0, h_pad=1.08, w_pad=1.08)
# plt.savefig('build/test-plot.pdf')

# Ablesen der Grenzfrequenzen und Umrechnen
f_gr = 9
f_gr1 = 5.60
f_gr2 = 14.73
f_gr3 = 18.15

f_gr = np.exp(reg.reg_linear(f_gr, noms(m1), noms(b1)))
f_gr1 = np.exp(reg.reg_linear(f_gr1, noms(m2), noms(b2)))
f_gr2 = np.exp(reg.reg_linear(f_gr2, noms(m2), noms(b2)))
f_gr3 = np.exp(reg.reg_linear(f_gr3, noms(m2), noms(b2)))

write('build/Z_w_gr.tex', make_SI(Wellenwiderstand(2*np.pi*f_gr), r'\ohm', figures=0))
write('build/f_mess.tex', make_SI(f_gr*1e-3, r'\kilo\hertz', 'e-3',figures=1))
write('build/f1_mess.tex', make_SI(f_gr1*1e-3, r'\kilo\hertz', 'e-3',figures=1))
write('build/f2_mess.tex', make_SI(f_gr2*1e-3, r'\kilo\hertz', 'e-3',figures=1))
write('build/f3_mess.tex', make_SI(f_gr3*1e-3, r'\kilo\hertz', 'e-3',figures=1))

# Theoriewerte der Grenzfrequenzen
w_th = 2 / np.sqrt(L*C1)
w1_th = np.sqrt(2/(L*C1))
w2_th = np.sqrt(2/(L*C2))
w3_th = np.sqrt( 2/L * (C1+C2)/(C1*C2) )
    'build/Tabelle_Verdampfungskurve.tex',
    [],
    [r'$T \:/\: \si{\kelvin}$',
    r'$p \:/\: \si{\bar}$',
    r'$\frac{1}{T} \:/\: 10^{-3}\si{\per\kelvin}$',
    r'$\ln{(p/\si{\pascal})}$']))

# Fit Verdampfungskurve
params = ucurve_fit(reg.reg_linear, 1/T1, np.log(p1), p0=[-1, 1])
m1, b1 = params
write('build/m1.tex', make_SI(m1, r'\kelvin', '', 1))   # 1 signifikante Stelle
write('build/b1.tex', make_SI(b1, r'', '', 1))   # 1 signifikante Stelle

# Plot ln(p) vs 1/T  -> Verdampfungskurve
T_plot = np.linspace(np.amin(1/T1), np.amax(1/T1), 100)
plt.plot(T_plot*1e3, reg.reg_linear(T_plot, *noms(params)), 'b-', label='Fit')
plt.plot(1/T1*1e3, np.log(p1), '.r', label='Messdaten')
plt.xlim(1e3*(T_plot[0]-1/np.size(T1)*(T_plot[-1]-T_plot[0])), 1e3*(T_plot[-1]+1/np.size(T1)*(T_plot[-1]-T_plot[0])))
plt.xlabel(r'$T^{-1} \:/\: 10^{-3}\si{\per\kelvin}$')
plt.ylabel(r'$\ln(p / \si{\pascal})$')
plt.legend(loc='best')
plt.tight_layout(pad=0, h_pad=1.08, w_pad=1.08)
plt.savefig('build/Verdampfungskurve.pdf')

R = const.physical_constants["molar gas constant"]      # value, unit, error
R_unc = ufloat(R[0],R[2])
write('build/R.tex', make_SI(R_unc, r'\joule\per\mol\per\kelvin'))
L1 = -R_unc * m1
write('build/L.tex', make_SI(L1*1e-3, r'\kilo\joule\per\mol', '', 1))      # eine signifikante Stelle

#####################################################################################################################################################