# -*- coding: utf-8 -*-
from __future__ import division
import numpy as np
from numpy import pi
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib
import os,sys
from matplotlib import rc
plt.rc('text', usetex=True)
plt.rc('font', family='serif',serif='Times',size=54)
I=0
folder_tag='gv'
folder=folder_tag[:2]+'out'+folder_tag[2:]
folder_tagh='gvh'
folderh=folder_tagh[:2]+'out'+folder_tagh[2:]

def folder_func(prefix,suffix,integer):
	return prefix+'out'+suffix+str(integer)

gvnums=[1,2,3,4,5,6,7,8]
np_s=100

folders_gv=[]
folders_gvh=[]

for suffix in ['','h']:
    for i in gvnums:
        exec("folders_gv%s.append(folder_func('gv',suffix,i))"%(suffix))

es=np.zeros(len(gvnums))
gs=np.zeros((len(gvnums),np_s))
vs=np.zeros((len(gvnums),np_s))
Ts1,Ts2=np.zeros((np_s,np_s,len(gvnums))),np.zeros((np_s,np_s,len(gvnums)))
i=-1
for folder in folders_gv:
    Ts1[:,:,i]=np.loadtxt(folder+'/T1.txt')
    Ts2[:,:,i]=np.loadtxt(folder+'/T2.txt')
    vs[i,:]=np.loadtxt(folder+'/vs.txt')
    gs[i,:]=np.loadtxt(folder+'/gs.txt')
    es[i],f=np.loadtxt(folder+'/params.txt')[:-1]
    i=i+1
inds=np.argsort(es,axis=0)
Ts1n,Ts2n,vsn,gsn,esn=np.zeros((np_s,np_s,len(gvnums))),np.zeros((np_s,np_s,len(gvnums))),np.zeros((len(gvnums),np_s)),np.zeros((len(gvnums),np_s)),np.zeros((len(gvnums)))
for i in range(len(inds)):
    Ts1n[:,:,i]=Ts1[:,:,inds[i]]
    Ts2n[:,:,i]=Ts2[:,:,inds[i]]
    gsn[i,:]=gs[inds[i],:]
    vsn[i,:]=vs[inds[i],:]
    esn[i]=es[inds[i]]
Ts1,Ts2,gs,vs,es=Ts1n,Ts2n,gsn,vsn,esn

esh=np.zeros(len(gvnums))
x0sh=np.zeros(len(gvnums))
gsh=np.zeros((len(gvnums),np_s))
wxsh=np.zeros((len(gvnums),np_s))
vsh=np.zeros((len(gvnums),np_s))
Ts1h,Ts2h=np.zeros((np_s,np_s,len(gvnums))),np.zeros((np_s,np_s,len(gvnums)))
i=-1
for folder in folders_gvh:
    Ts1h[:,:,i]=np.loadtxt(folder+'/T1.txt')
    Ts2h[:,:,i]=np.loadtxt(folder+'/T2.txt')
    wxsh[i,:]=np.loadtxt(folder+'/wxs.txt')
    gsh[i,:]=np.loadtxt(folder+'/gs.txt')
    esh[i],f=np.loadtxt(folder+'/params.txt')[:-1]
    x0sh[i]=np.loadtxt(folder+'/x0.txt')
    vsh[i,:]=wxsh[i,:]*x0sh[i]
    i=i+1
indsh=np.argsort(esh,axis=0)
Ts1nh,Ts2nh,vsnh,gsnh,esnh=np.zeros((np_s,np_s,len(gvnums))),np.zeros((np_s,np_s,len(gvnums))),np.zeros((len(gvnums),np_s)),np.zeros((len(gvnums),np_s)),np.zeros((len(gvnums)))
for i in range(len(indsh)):
    Ts1nh[:,:,i]=Ts1h[:,:,indsh[i]]
    Ts2nh[:,:,i]=Ts2h[:,:,indsh[i]]
    gsnh[i,:]=gsh[indsh[i],:]
    vsnh[i,:]=vsh[indsh[i],:]
    esnh[i]=esh[indsh[i]]
Ts1h,Ts2h,gsh,vsh,esh=Ts1nh,Ts2nh,gsnh,vsnh,esnh

nj=np_s
nv=np_s

fsize=(24,18)
fig=plt.figure(figsize=fsize)
letters=['a','b']
colours=['chartreuse','indigo','dodgerblue','fuchsia','red','black','forestgreen','royalblue','darkorchid']
linestyles=["-","--","-.",":","-","--","-.",":","-","--","-.",":"]
colours=colours[:len(gvnums)]
shading='flat'
cmap='inferno'
fontsizela=77
ax1=fig.add_subplot(211)
ax2=fig.add_subplot(212)
matplotlib.pyplot.subplots_adjust(hspace=0.25)
boundaries=np.zeros((len(gvnums),nj))
T_lim=0.5
for k in range(len(gvnums)):
    for j in range(nj):
        for i in range(nv):
            if Ts2[i,j,k]>T_lim:
                boundaries[k,j]=vs[k,i]
                break
        if boundaries[k,j]==0:
            if gsh[k,j]<0.2:
                boundaries[k,j]=np.min(vs[k,:])
            else:
                boundaries[k,j]=np.max(vs[k,:])
for i in range(len(gvnums)):
    exec("l%d=ax1.plot(boundaries[i,:],gs[i,:],color=colours[i],linestyle=linestyles[i],linewidth=4)"%(i+1))
ax1.set_xlabel('$\mathit{v}$',labelpad=-20,fontsize=fontsizela)
ax1.set_ylabel('$\mathit{g}$',rotation=0,fontsize=fontsizela)
ax1.yaxis.set_label_coords(-0.12,0.35)
ax1.set_xlim(np.min(vsh),np.max(vsh))
ax1.set_ylim(np.min(gs),np.max(gs))
fig.text(0.135, 0.835, '(%s)'%(letters[0]), fontsize=64, color='black')

boundariesh=np.zeros((len(gvnums),nj))
for k in range(len(gvnums)):
    for j in range(nj):
        for i in range(nv):
            if Ts2h[i,j,k]>T_lim:
                boundariesh[k,j]=vsh[k,i]
                break
        if boundariesh[k,j]==0:
            if gs[k,j]<0.2:
                boundariesh[k,j]=np.min(vsh[k,:])
            else:
                boundariesh[k,j]=np.max(vsh[k,:])

for i in range(len(gvnums)):
    exec("l%d=ax2.plot(boundariesh[i,:],gsh[i,:],color=colours[i],linestyle=linestyles[i],linewidth=4)"%(i+1))
ax2.set_xlabel('$\\omega_x \mathit{x_0}$',fontsize=fontsizela)
ax2.set_ylabel('$\mathit{g}$',rotation=0,fontsize=fontsizela)
ax2.yaxis.set_label_coords(-0.12,0.35)
ax2.set_xlim(np.min(vsh),np.max(vsh))
ax2.set_ylim(np.min(gsh),np.max(gsh))
labels,ls=[],[]
for i in range(len(gvnums)):
    exec("ls.append(l%d)"%(i+1))
    labels.append('$\\epsilon$=%3.2f'%es[i])
ncol=int(len(gvnums)/2)
fig.legend(labels,loc='upper center',ncol=ncol,fontsize=46,frameon=False)
fig.text(0.135, 0.41, '(%s)'%(letters[1]), fontsize=64, color='black')
plt.savefig('fig8.eps',pad_inches=0)
