
from sys import platform as sys_pf
import matplotlib
import platform
if platform.system() == 'Darwin':
    matplotlib.use('MacOSX')

import numpy as np
import scipy
from scipy.optimize import curve_fit
from scipy import constants as cst
import UsefulFunctions as uf
import os
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import h5py
import pandas as pd
from matplotlib.ticker import EngFormatter
from matplotlib import rc
import LoadData
#rc('text', usetex=True)
from tkinter import Tk
from tkinter.filedialog import askopenfilenames
import pickle as pkl
import scipy.signal as sig

root = Tk()
root.withdraw()
formCurrent = EngFormatter(unit='A', places=3)
formA = EngFormatter(unit='A', places=3)
formB = EngFormatter(unit='s', places=3)

file1 = '/Volumes/lben-archives/2020 - CURRENT/Mukesh_Archives/Axopatch/04122020_LOCAL/test_append/joy25p139_1MKCl_12nm_A30trace_5_filtered.dat'
file2 = '/Volumes/lben-archives/2020 - CURRENT/Mukesh_Archives/Axopatch/04122020_LOCAL/test_append/joy25p139_1MKCl_12nm_A30trace_8.dat'
# file3 = '/Volumes/lben-archives/2019 - CURRENT/Mukesh_Archives/Chimera/2019-11-06/Trial 2/2019-11-06/chip 2 0 mV/chp2_1MKCl_0mV_Axo.dat'
# file4 = '/Volumes/lben-archives/2019 - CURRENT/Mukesh_Archives/Chimera/2019-11-06/Trial 2/2019-11-06/chip 2 0 mV/chp2_1MKCl_0mV_Axo.dat'


dat1 = uf.OpenFile(file1)
#data = np.fromfile(datafilename, np.dtype('<u2'))

current_array1 = np.array(dat1['i1'])
dat = np.append([current_array, samplerate_array], dat [['i1'],['samplerate']])

dat2 = uf.OpenFile(file2)
current_array2 = np.array(dat2['i1'])
samplerate = dat2['samplerate']
# dat = np.append([current_array,samplerate_array], dat [['i1raw'],['samplerate']])

a = np.append(current_array1, current_array2)
out = {}
out['i1'] = a
out['samplerate'] = samplerate
file = open('Test.p', 'wb')
pkl.dump(out, file)
file.close()
print(len(a))

file = open('Test.p', 'rb')
psd_file = pkl.load(file)
print(psd_file)
print(psd_file['i1'])
print(psd_file['samplerate'])

plt.plot(psd_file['i1raw'])
plt.show()

# fig = plt.figure(1, figsize=(10, 8))
# ax = fig.add_subplot(111)
#
# f, Pxx = sig.welch(psd_file['i1raw'], psd_file['samplerate'], nperseg=2**18)
# ax.plot(f, Pxx*1e24)
#
# ax.set_xlabel('Frequency (Hz)')
# ax.set_ylabel(r'PSD ($\frac{pA^2}{Hz}$)')
# ax.set_yscale('log')
# ax.set_xscale('log')
# plt.show()