import numpy as np
import matplotlib.pyplot as plt
import matplotlib

mplparams = {
    'text.usetex': True, 
    'axes.linewidth': 1, 
    'axes.grid': False,
    'axes.labelweight': 'normal',
    'font.family': 'DejaVu Sans',
    'font.size': 20
}
matplotlib.rcParams.update(mplparams)

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

# Get data

GW230814_mV_opt = 3.8047747748e-13
GW231123_mV_opt = 1.5016116116e-13
GW230814_fmin, GW230814_fmax = 103, 186
GW231123_fmin, GW231123_fmax = 31, 55

GW230814_mVs, GW231123_mVs = [], []
GW230814_1FAP, GW230814_5FAP, GW230814_10FAP = [], [], []
GW230814_1FAP_unc, GW230814_5FAP_unc, GW230814_10FAP_unc = [], [], []
GW231123_1FAP, GW231123_5FAP, GW231123_10FAP = [], [], []
GW231123_1FAP_unc, GW231123_5FAP_unc, GW231123_10FAP_unc = [], [], []

GW230814_data = np.loadtxt("GW230814_constraints.txt")
GW231123_data = np.loadtxt("GW231123_constraints.txt")

for line in GW230814_data:
    if line[0] == 1:
        GW230814_mVs.append(line[1])
        GW230814_1FAP.append(line[2])
        GW230814_1FAP_unc.append(line[3])
    elif line[0] == 5:
        GW230814_5FAP.append(line[2])
        GW230814_5FAP_unc.append(line[3])
    elif line[0] == 10:
        GW230814_10FAP.append(line[2])
        GW230814_10FAP_unc.append(line[3])

for line in GW231123_data:
    if line[0] == 1:
        GW231123_mVs.append(line[1])
        GW231123_1FAP.append(line[2])
        GW231123_1FAP_unc.append(line[3])
    elif line[0] == 5:
        GW231123_5FAP.append(line[2])
        GW231123_5FAP_unc.append(line[3])
    elif line[0] == 10:
        GW231123_10FAP.append(line[2])
        GW231123_10FAP_unc.append(line[3])

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

# Plot constraints

fig, axs = plt.subplots(1, 2, figsize = [12, 5])

axs[0].errorbar(GW230814_mVs, GW230814_1FAP, yerr = GW230814_1FAP_unc, marker = "o", linestyle = "-", color = "tab:orange", label = r"1\% $P_{\rm fa}$")
axs[0].errorbar(GW230814_mVs, GW230814_5FAP, yerr = GW230814_5FAP_unc, marker = "o", linestyle = "-", color = "tab:blue", label = r"5\% $P_{\rm fa}$")
axs[0].errorbar(GW230814_mVs, GW230814_10FAP, yerr = GW230814_10FAP_unc, marker = "o", linestyle = "-", color = "tab:purple", label = r"10\% $P_{\rm fa}$")
axs[0].axvline(GW230814_mV_opt, color="black", linestyle="--", label = r"$m_V^{\rm opt}$")

axs0_2 = axs[0].twiny()
axs0_2.set_xlim(GW230814_fmin - 4.15, GW230814_fmax + 4.15)
axs0_2.set_xticks([100, 125, 150, 175])
axs0_2.set_xlabel(r"$f_0$ [Hz]", labelpad=10)

axs[1].errorbar(GW231123_mVs, GW231123_1FAP, yerr = GW231123_1FAP_unc, marker = "o", linestyle = "-", color = "tab:orange")
axs[1].errorbar(GW231123_mVs, GW231123_5FAP, yerr = GW231123_5FAP_unc, marker = "o", linestyle = "-", color = "tab:blue")
axs[1].errorbar(GW231123_mVs, GW231123_10FAP, yerr = GW231123_10FAP_unc, marker = "o", linestyle = "-", color = "tab:purple")
axs[1].axvline(GW231123_mV_opt, color="black", linestyle="--")

axs1_2 = axs[1].twiny()
axs1_2.set_xlim(GW231123_fmin - 1.2, GW231123_fmax + 1.2)
axs1_2.set_xticks([30, 38, 46, 54])
axs1_2.set_xlabel(r"$f_0$ [Hz]", labelpad=10)

axs[0].set_ylim(0, 0.5)
axs[1].set_ylim(0, 0.5)
axs[0].set_xlabel(r"$m_V$ [eV]")
axs[0].set_ylabel(r"$P_{\rm det}$")
axs[1].set_xlabel(r"$m_V$ [eV]")
axs[0].grid(which="major")
axs[1].grid(which="major")
axs[0].minorticks_on()
axs[1].minorticks_on()
axs[0].tick_params(axis="y", which="minor", left=False)
axs[1].tick_params(axis="y", which="minor", left=False)
fig.legend(loc="upper center", bbox_to_anchor=(0.5, 1.1), fontsize = 20, ncol = 4)

fig.tight_layout()
#fig.savefig("Pdet_v_mV.pdf", transparent=True, bbox_inches="tight")
plt.show()