import netCDF4 as nc
import os
import sys
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import numpy as np
from pathlib import Path

#def plot_une_simu_(file,linestyle,color,linewidth,label):
#    if os.path.exists(file):
#        var=nc.Dataset(file).variables[var_name][0,:,0]
#        lat=nc.Dataset(file).variables["lat"][:]
#        #print(lat)
#        plt.plot(lat,var,linestyle,color=color,linewidth=linewidth,label=label)

epsx=0.1

labels={
"6ALRamip"   : "6ALRamip",
"6ALR"   : "6ALR",
"W3-115" : "Exp 1",
"W3-181" : "Exp 2",
"W2-050" : "Exp 3",
"W2-051" : "Exp 4",
}


def unef(name,col):
   # Lecture des variables et calcul des variables derivees
   file="SSTdiff/"+name+"/rt.nc"
   rt=nc.Dataset(file).variables["rsdt"][:,0,0]
   file="SSTdiff/"+name+"/tas.nc"
   tas=nc.Dataset(file).variables["tas"][:,0,0]
   ecs=-3.2/(rt/tas+0.5)
   plt.plot(ecs,label=labels[name],color=col)
   return

unef("6ALR",'grey')
unef("6ALRamip",'brown')
unef("W3-115",'darkviolet')
unef("W3-181",'blue')
unef("W2-050",'darkgreen')
unef("W2-051",'red')


plt.margins(x=0.,y=0.)

plt.xlabel("Year")
plt.ylabel("ECS (°C)")
plt.legend(loc="upper right")


plt.tight_layout()
#plt.show()
plt.savefig("ECSf.pdf")

