import matplotlib
#matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import platform
if platform.system() == 'Darwin':
    matplotlib.use('MacOSX')
import os
import numpy as np
from matplotlib.ticker import EngFormatter
from matplotlib.font_manager import FontProperties
import LoadData
from tkinter.filedialog import askopenfilenames
formCurrent = EngFormatter(unit='A', places=3)
formA = EngFormatter(unit='A', places=3)
formB = EngFormatter(unit='s', places=3)
#Set font properties
fontP = FontProperties()
fontP.set_size('small')
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42

filenames = askopenfilenames()

for i, file in enumerate(filenames):
    dat = LoadData.OpenFile(file)

    fig1 = plt.figure(1, figsize=(8, 4))
    ax_part1 = fig1.add_subplot(2, 1, 1)

    t = np.arange(len(dat['i1']))/dat['samplerate']
    current_array = np.array(dat['i1'] * 1e9)
    voltage_array = np.array((dat['v1']))

    ax_part1.plot(t[::], current_array[::])
    ax_part1.set_xlabel('t (s)')
    ax_part1.set_ylabel('I (nA)')
    # ax_part1.plot(t [::], current_array[::])
    # ax_part1.set_ylim(0, 10)
    # ax_part1.set_xlim(4.2, 80)
    # # print('Mean_G (nS)', np.mean(conductance_array[::]))
    # # print('Std_G (nS)', np.std(conductance_array[::]))
    # # print('Mean_I (nA)', np.mean(current_array[::]))
    print('I_rms (nA)', np.std(current_array[::])) #Irms value
    print(filenames)
    # fig2 = plt.figure(1, figsize=(8, 4))
    ax_part2 = fig1.add_subplot(2, 1, 2, sharex=ax_part1)
    t = np.arange(len(dat['v1']))/dat['samplerate']
    ax_part2.plot(t, dat['v1']*1e3, label = file[72:])
    ax_part2.set_xlabel('t (s)')
    ax_part2.set_ylabel('V (mV)')
    # ax_part1.set_xlim(0, 1)
    # ax_part1.set_ylim(4.5, 6)
    # ax_part1.legend()
    # ax_part2.legend()

# plt.savefig("file_path_.svg")