#bibliothèques
import numpy as np
import matplotlib.pyplot as plt
import csv

#lecture du fichier .csv
def readColCSV(fichier,sep,n):
    file=open(fichier,'r')
    reader=csv.reader(file,delimiter=sep)
    col=np.array([])
    for row in reader:
        notation_point=row[n].replace(',','.')
        col=np.append(col,notation_point)
    col=np.delete(col,0) #supprime la première valeur
    file.close()
    result=[float(i) for i in col]
    result=np.array(result)
    return result


#on met les valeurs qui nous intéressent dans des tableaux
#bien rentrer le nom du fichier
nom_fichier='velo2_correct.csv'
separateur=','
t=readColCSV(nom_fichier,separateur,0)
x=readColCSV(nom_fichier,separateur,1)
y=readColCSV(nom_fichier,separateur,2)

#rayon
R=np.sqrt(x**2+y**2)
print(R)

#nb de valeurs dans x et y
Nx=len(x)
Ny=len(y)

#calcul des vitesse
vx=np.array([(x[i+1]-x[i-1])/(t[i+1]-t[i-1]) for i in range (1,Nx-1)])
vy=np.array([(y[i+1]-y[i-1])/(t[i+1]-t[i-1]) for i in range (1,Ny-1)])
v=np.sqrt(vx**2+vy**2)
print(v)

#nb de valeurs dans vx et vy
Nvx=len(vx)
Nvy=len(vy)

#calcul de l'accélération
ax=np.array([(vx[i+1]-vx[i-1])/(t[i+1]-t[i-1]) for i in range (1,Nvx-1)])
ay=np.array([(vy[i+1]-vy[i-1])/(t[i+1]-t[i-1]) for i in range (1,Nvy-1)])
a=np.sqrt(ax**2+ay**2)


#moyennes et incertitudes types
#vitesse
vmoy=np.mean(v)
uv=np.std(v)/np.sqrt(len(v))
print('La vitesse moyenne est de ',vmoy,'m/s avec une incertitude type de ',uv,'m/s')
#acceleration
amoy=np.mean(a)
ua=np.std(a)/np.sqrt(len(a))
print('L accélération moyenne est de ',amoy,'m/s2 avec une incertitude type de ',ua,'m/s2')
#rayon
Rmoy=np.mean(R)
uR=np.std(R)/np.sqrt(len(R))
print('Le rayon moyen est de ',Rmoy,'m avec une incertitude type de ',uR,'m')
#calcul de l'accélération théorique
atheo=vmoy**2/Rmoy
#incertitude
uatheo=atheo*np.sqrt((2*uv/vmoy)**2+(uR/Rmoy)**2)
print('L accélération théorique est de ',atheo,'m/s2')

#Calcul de z
z=abs(amoy-atheo)/ua

print('z=',z)

# Creation du graphique
plt.figure(1)
plt.subplot(121)
plt.axis('equal')
plt.title('Trajectoire du système')
plt.xlabel('x (m)')
plt.ylabel('y (m)')
plt.plot(x, y, 'b+', label = 'Trajectoire')
plt.grid()
plt.subplot(122)
plt.axis('equal')
plt.title('Vecteurs')
plt.xlabel('x (m)')
plt.ylabel('y (m)')
plt.plot(x, y, 'b+', label = 'Trajectoire')
for i in range(len(vx)) :
  plt.quiver(x[i+1], y[i+1], vx[i], vy[i], angles = "xy", scale_units = "xy", scale = 10, color = "red")

for i in range(len(ax)) :
  plt.quiver(x[i+2], y[i+2], ax[i], ay[i], angles = "xy", scale_units = "xy", scale = 100, color = "blue")
plt.grid()
plt.show()