# file to evaluate a measurement
import os.path, glob
from os.path import join
import json

import pandas as pd
import numpy as np
import matplotlib.pyplot as pl

from helper import *
from style import *

path_data_repo = "../data_supplementary"


meas_list = [
    {
        "path": "Exp_Shapiro_steps_f_90_Hz_I_m_1.1_barrier_height_V_0.25.csv",
        "v_max": 1,
    },
    {
        "path": "Exp_Shapiro_steps_f_90_Hz_I_m_1.1_barrier_height_V_0.34.csv",
        "v_max": 1,
    },
    {
        "path": "Exp_Shapiro_steps_f_90_Hz_I_m_0.8_barrier_height_V_0.45.csv", # same data as Fig.2  Exp_Shapiro_steps_f_90_Hz_I_m_0.8.csv
        'v_max': 0.8,
    },
    {   "path":"Exp_Shapiro_steps_f_90_Hz_I_m_0.5_barrier_height_V_0.57.csv",
           "v_max":1,
    },
    {
        "path": "Exp_Shapiro_steps_f_90_Hz_I_m_0.4_barrier_height_V_0.71.csv",
        "v_max": 1,
        },
]



set_plot_style()

fig, ax = pl.subplots(nrows=1, ncols=5, sharey=True, figsize=(15,3.5))
axs = ax.flatten()




for i, meas in enumerate(meas_list):
    # Read Experiment Data
    data = pd.read_csv(os.path.join(path_data_repo,meas['path']))
    # Calculate chemical potential difference
    data["chem_pot"] = mu_N(data["N"] * (1 + data['delta_z'])) - mu_N(
        data["N"] * (1 - data['delta_z']))
    # get frequency
    f = data["f"].to_numpy()[0]
    # get modulation amplitude
    I_m = data["I_m"].to_numpy()[0] # I_m / I_c
    # get critical current from func keyword
    I_c = current(1, v_c=1)
    # get the barrier height
    bar_power = data["V"].to_numpy()[0]

    data = data[["delta_z", "vel", "chem_pot"]]
    # calculate the average of the data for the unique velocities
    grouped = data.groupby(by="vel").agg(['mean', 'sem'])
    vel = grouped.index.to_numpy()

    z = grouped['delta_z', 'mean']
    z_err = grouped['delta_z', 'sem']

    mu = grouped['chem_pot', 'mean']
    mu_err = grouped['chem_pot', 'sem']

    I = current(vel, v_c=0.42)


    style = get_style(RPTU_COLORS["nacht"])
    if i == 2:
        style = get_style(RPTU_COLORS["mango"])

    # calc the current
    I = current(vel, v_c=0.42)

    v_max = meas["v_max"]

    axs[i].errorbar(I[vel <= v_max]*1e-3,mu[vel <= v_max],mu_err[vel <= v_max], **style, label=f'$V_\mathrm{{B}} = {bar_power:.2f}$ µ')

    axs[i].set_xlabel(r"Current $I$ [$10^3$/s]", fontsize = 12)
    axs[i].legend(fontsize = 10)
    axs[i].axhline(y = 90, color = 'dimgrey', ls = '--')

    if i == 3:
        axs[i].legend(loc = "upper left",fontsize = 10)
        axs[i].annotate(text='$1 \cdot f_\mathrm{m}$', xy=(240, 90), verticalalignment='center', fontsize=10,
                        bbox={'boxstyle': 'round', 'fc': "white", 'ec': 'dimgrey', 'ls': '-', 'lw': 1.5}, zorder=5)
    elif i == 4:
        axs[i].annotate(text='$1 \cdot f_\mathrm{m}$', xy=(120, 90), verticalalignment='center', fontsize=10,
                        bbox={'boxstyle': 'round', 'fc': "white", 'ec': 'dimgrey', 'ls': '-', 'lw': 1.5}, zorder=5)
    else:
        axs[i].annotate(text='$1 \cdot f_\mathrm{m}$', xy=(0, 90), verticalalignment='center', fontsize=10,
                 bbox={'boxstyle': 'round', 'fc': "white", 'ec': 'dimgrey', 'ls': '-', 'lw': 1.5}, zorder=5)

    #pl.show()

axs[3].set_xlim(0,300)
axs[4].set_xlim(0,150)
axs[0].set_ylabel("$\Delta \mu$  [Hz]", fontsize = 12)

pl.ylim(-50,300)

pl.tight_layout()
#pl.savefig(fr'ShSt_barrier_heigth.png', dpi = 300)

pl.show()