import tarfile

fname_base = '/gh/data/example/lfp_set_PsTs/out.29419325.'
Nfiles = 10
for n in range(Nfiles):
    fname = fname_base + str(n) + '.tar.gz'
    tar = tarfile.open(fname, "r:gz")
    tar.extractall('/gh/data/example/lfp_set_PsTs/' + str(n) + '/')
    tar.close()

"""
Explanation: Plot output from OSG
In the demo to run python on the open science grid (OSG), we generated compressed numpy files that contain our results.
In this notebook, we extract these results and plot them on our data to confirm that our program ran correctly.
1. iterate through tar files and unzip them
End of explanation
"""


import numpy as np

Ps = np.zeros(Nfiles, dtype=np.ndarray)
Ts = np.zeros(Nfiles, dtype=np.ndarray)
for n in range(Nfiles):
    Ps[n] = np.load('/gh/data/example/lfp_set_PsTs/' + str(n) + '/out/Ps_data.npy')
    Ts[n] = np.load('/gh/data/example/lfp_set_PsTs/' + str(n) + '/out/Ts_data.npy')

"""
Explanation: 2. Load each array of Ps and Ts
End of explanation
"""


lfps = np.zeros(Nfiles, dtype=np.ndarray)
for n in range(Nfiles):
    if n == 0:
        lfps[n] = np.load('/gh/data/example/lfp_set/' + str(10) + '.npy')
    else:
        lfps[n] = np.load('/gh/data/example/lfp_set/' + str(n) + '.npy')

"""
Explanation: 3. Load signals
End of explanation
"""


import matplotlib.pyplot as plt
%matplotlib inline

plt.figure(figsize=(10,10))
for n in range(Nfiles):
    plt.subplot(Nfiles, 1, n+1)
    plt.plot(lfps[n], 'k')
#     plt.plot(Ps[n], lfps[n][Ps[n]], 'bo')
#     plt.plot(Ts[n], lfps[n][Ts[n]], 'ro')
    if n == Nfiles-1:
        plt.xlabel('Time (ms)')
    else:
        plt.xticks([])
    plt.ylim((-3000,3000))
    plt.yticks([-3000,0,3000])
    if n == 0:
        plt.ylabel('Voltage (uV)')
    

"""
Explanation: 4. Plot peaks and troughs on top of signals
End of explanation
"""


