##############################################################
# Ce script plot la diminution de la taille du NROY
# au fur et a mesure des vagues
# 
# Arguments à renseigner par l'utilisateur : 
# WAVEN : numero de la vague final
# cutoff_wav : numero des vagues ou le cutoff change
# cutoff_val : valeur des cutoff correspondant
# ymin et ymax : bornes du graphique
#
# Script : 
# 1. commence a concatener les fichiers Remaining_space_after_wave_$i.txt
#    pour creer NROY_fraction_w1to'+str(WAVEN)+'.txt'
# 2. fait le tracé
#
# Maelle Coulon--Decorzens juin 2022
##############################################################

import numpy as np
import matplotlib.pyplot as plt
import os
import argparse

#arguments à renseigner par l'utilisateur
# WAVEN=12 #numero de la vague finale
# cutoff_wav=[1,5,8]  #numero de vague ou le cutoff change
# cutoff_val=[3,2.5,2] #valeur du cutoff
# ymin=0.01
# ymax=1

parser=argparse.ArgumentParser()
parser.add_argument("-cw",help="liste of wave number corresponing to cutoff change",nargs="+",type=int,default=[1,5,8])
parser.add_argument("-cv",help="liste of values of cutoff",nargs="+",type=float,default=[3,2.5,2])
parser.add_argument("-wn",help='Number of waves',type=int,default=10)
parser.add_argument("-min",help='Min value for NROY size',type=float,metavar="N",default=0.01)
parser.add_argument("-max",help='Max value for NROY size',type=float,metavar="N",default=1)
args=parser.parse_args()

WAVEN=args.wn
cutoff_wav=args.cw
cutoff_val=args.cv
ymin=args.min
ymax=args.max


#print("cutoff_val=",cutoff_val)
#print(type(cutoff_val))
#print("cutoff_val[0]=",cutoff_val[0])
#print("cutoff_val[1]=",cutoff_val[1])
#print("length cutoff_val=",len(cutoff_val))
#
#print("cutoff_wav=",cutoff_wav)
#print("cutoff_wav[0]=",cutoff_wav[0])
#print("cutoff_wav[1]=",cutoff_wav[1])
#print("length cutoff_wav=",len(cutoff_wav))


# concatenation des fichiers
name_file='NROY_fraction_w1to'+str(WAVEN)+'.txt'

os.system("if [ ! -f "+name_file+" ] ; then for i in `seq 1 "+str(WAVEN)+"` ; do NROY=`cat Remaining_space_after_wave_$i.txt` ; echo $NROY >> "+name_file+" ; done ; fi")

tab_NROY=np.loadtxt(name_file)
name_fig="plot_NROY_fraction_w1to"+str(WAVEN)+".png"
name_fig="NROY_fraction.pdf"

# tracé
pointille=['dashdot', 'dashed', 'dotted']
vagues=np.arange(1,WAVEN+1)
fig=plt.figure()
plt.yscale("log", base=10)
plt.plot(vagues,tab_NROY)
for i in range(len(cutoff_wav)) :
    plt.vlines(cutoff_wav[i], ymin ,ymax, label='cutoff = '+str(cutoff_val[i]), ls=pointille[i], color='grey')
plt.ylim(ymin,ymax)
plt.grid(axis='y', which='minor', color="#d7d7d7")
plt.xlabel('numero de la vague')
plt.ylabel('fraction du NROY')
plt.legend()
plt.savefig(name_fig)
print('figure enregistree sous : '+name_fig)
