# -*- coding: utf-8 -*-
"""
Created on Wed Nov  6 07:02:58 2019

@author: goldman3g
"""
#import numpy as np
import matplotlib.pyplot as plt
import os
import csv
import sys
import re

plt.close('all')

#path = r"D:\Dropbox\Crew\Clark\PRE-6"
#path = r"D:\Dropbox\Crew\Jiabei\191111"
#path = r"D:\Dropbox\Crew\Clark\translocation"
#path = r"D:\Dropbox\Crew\Clark\Translocation\More_Examples"
#path = r"D:\Dropbox\Crew\Clark\PRE-6_TrypIRES"
#path = r"D:\Dropbox\Crew\Clark\Translocation\Good_Translocation_Traces"
#path = r"D:\Dropbox\Crew\Arpan\Translocation\Non-ALEX Traces for Yale"
path = r"E:\Data\3.16.2020\s1503\Rearranged"

def gfn():       # Get file names from directory
    file_list=os.listdir(path)
#    print (file_list)
    return file_list

plt.close('all')

file_list = gfn()
print ("Files: ", len(file_list))

pplist = []
for fn in file_list:
    if "pairProf" in fn:
#        print ("filename = ", fn)
        result = re.search('file(.*).c', fn)
        if (result != None):
            pplist.append(fn)
 #           print ("result = ", result)
            print (fn, result.group(1))

print ("PairProfile .csv files: ", len(pplist))
##print (pplist) 


i = 0
while True: 
    
    print ("\ni = ", i)
    fullname = path + "\\" + pplist[i]
    print ("Full file name = ", fullname)
    fl = open(fullname)
    with open(fullname) as csvfile:
        
        data = list(csv.reader(csvfile))
    
#    print (len(data))
    
    h = int(len(data)/2)
    
    rownum = 0	
    don = []
    acc = []
    alx = []
    t = []
    FRET = []
    dt = 0.2
    
    
    for row in data:
        if rownum == 0:
            print (row)
            ttle = row
        elif rownum < h+1:
            thisDon = float(row[0])/1000                 
            don.append(thisDon)
            thisAcc = float(row[1])/1000
            acc.append(thisAcc)
#            if thisDon > 0:
#                FRET.append(thisAcc/(thisDon+thisAcc))
            thisFRET = thisAcc/(thisDon+thisAcc)
            
            '''
            if ((thisFRET > -0.1) and (thisFRET < 1.1) and (thisDon > 10.0)): 
                FRET.append(thisFRET)
            else:
               FRET.append(0.0)
            '''    
            FRET.append(thisFRET)
            t.append(dt*rownum-1)   
        else:
            alx.append(float(row[1])/1000)
                                    
        rownum += 1
        
    csvfile.close()
    
    print ("Frames: ", len(don))
    
    #for i in range (0,h-1):
    #    print (i, don[i], acc[i], alx[i])
        
    #for i in range (0,h-1):
    #    print (i, alx[i])
    
    
    
#    fig1 = plt.figure(1)
    
    fig1, (sb1, sb2, sb3) = plt.subplots(3, sharex=True, figsize=(10, 7))
    fig1.suptitle(pplist[i], fontsize=16)
    pmngr = plt.get_current_fig_manager()
# to put it into the upper left corner for example:
    pmngr.window.setGeometry(50, 50, 900, 700)

    ylab = ''.join (('                                                ',
                 'FRET           ',
                 'ALEX Intensity     ',
                 'Intensity'))
    plt.ylabel(ylab, fontsize=16)
    
    sb1.set_title(ttle[0]+",   "+ttle[1], fontsize=16)
    sb1.plot(t, don, 'lime', linewidth=0.5)
    sb1.plot(t, acc, 'r', linewidth=0.5)
    sb1.grid("On")   
    
    sb2.plot(t, alx, 'r', linewidth=0.5)
    sb2.grid("On")   
    
    sb3.plot(t, FRET, 'blue', linewidth=0.5)
    plt.ylim(-0.1, 1.1) 
    sb3.grid("On")   

    
    #plt.ylabel('                                                           FRET                  ALEX Intensity                Intensity')
    
    plt.xlabel('Time (s)', fontsize=16)  
#    plt.pause(0.5)  
    plt.show()  
    plt.pause(0.5)  
    
    g = str(input("CR, (S)ave, (B)ack. (Q)uit: ")) 
    print (g)
    plt.close("all")
    if ((g == "b") or (g == "B")):
        i -= 2
    if g == "q" or g == "Q":
        plt.close("all")
        sys.exit()
    if g == "s" or g == "S":
        file = open(path + "\\" + 'GoodOnes.txt', 'a') 
        textstr = str(i)+' '+pplist[i]+'\n' 
        file.write(textstr) 
        file.close() 
    i += 1 
    if i >= len(pplist):    
        i = 0
    if i < 0:
        i = len(pplist)-1
            
