#!/usr/bin/python3

import numpy
import matplotlib.pyplot
import os
import sys

matplotlib.pyplot.ion()

if (int(sys.argv[1]) == 0):
    neg = numpy.genfromtxt("epsilon_neg.dat")
    pos = numpy.genfromtxt("epsilon_pos.dat")
    column = 0
elif (int(sys.argv[1]) == 1):
    neg = numpy.genfromtxt("gamma_neg.dat")
    pos = numpy.genfromtxt("gamma_pos.dat")
    column = 1

fig,ax=matplotlib.pyplot.subplots(figsize=(10,8))

for i in range(2,len(neg[0,:])):
    ax.plot(neg[:,column], neg[:,i], 'k:')
for i in range(2,len(pos[0,:])):
    ax.plot(pos[:,column], pos[:,i], 'k-')

NNuc = int(os.sys.argv[2])
indx = int(int(NNuc-1)/2)
fermi = []
epsgam = float(os.sys.argv[3])

mindist = 1e5
for i in range(0, len(neg[:,column])):
    if (abs(neg[i,column] - epsgam) < mindist):
        mindist = abs(neg[i,column] - epsgam)
        epsgamind = i
        
    ens=sorted(list(neg[i,column+1:])+list(pos[i,column+1:]))
    fermi.append(ens[indx])

ens=list(neg[epsgamind,column+1:])+list(pos[epsgamind,column+1:])
pars=[-1 for i in range(0,len(neg[epsgamind,column+1:]))]+[1 for i in range(0,len(pos[epsgamind,column+1:]))]
labels = [(ens[i],pars[i]) for i in range(len(ens))]
labels= sorted(labels,key=lambda x : x[0])

negctr = 1
posctr = 1
for i in range(0, len(labels)):
#    labels.append("%i(%i)"%(str(i+1)
    label=""
    if labels[i][1] == -1:
        label = "-%i"%(negctr)
        negctr+=1
    else:
        label = "+%i"%(posctr)
        posctr+=1
        
    ax.text(epsgam,labels[i][0], "%i(%s)"%(i+1,label), ha='center', va='bottom')

ax.plot(pos[:,column], fermi, 'r--')
ax.set_ylim(sum(fermi)/len(fermi)-0.5,sum(fermi)/len(fermi)+0.5);
    
matplotlib.pyplot.show(block=True);
