#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jun  4 19:54:55 2022

@author: macbookraphael
"""
import numpy as np
import matplotlib.pyplot as plt
import csv
from math import sqrt, pi
from matplotlib.patches import Polygon
from scipy.optimize import curve_fit

# with open('Triton.csv') as source:
#     donnees = csv.DictReader(source, delimiter=",")
#     descripteurs = donnees.fieldnames
#     donnees = list(donnees)

# # exemple de ce qu'on peut visualiser :
# for (clef, valeur) in donnees[0].items():
#     print(f"{clef} : {valeur}")
    
# print("\nLes satellites étudiés sont :")
# for s in donnees:
#     print(s['Satellite'])
    
    
a = [354800**3, 5513400**3, 15728000**3, 22422000**3, 46695000**3]
T = [5.877**2, 360.14**2, 1879.71**2, 2914.71**2, 9115.91**2]


a = np.array(a) * 1000 # 1 km = 1000 m
T = np.array(T) * 24 * 3600 # 1 j = 24 h * 3600 s


abscisse, ordonnee = a, T
plt.plot(abscisse, ordonnee, 'o')
plt.xlabel("$a^3$ ($m^3$)")
plt.ylabel("$T^2$ ($s^2$)")
plt.show()

def modele_lineaire(x, k):
    return k * x

modele, _ = curve_fit(modele_lineaire, abscisse, ordonnee)
k = modele[0]
print(f"k = {k:#.2g} USI")

# Comparaison avec la valeur théorique :
M = 1.02*10**26
G = 6.67*10**(-11)
k_theorie = 4 * pi**2 / (G * M)
print(f"k_theorie = {k_theorie:#.2g} USI")

# écart relatif
print(f"écart relatif entre théorie et expérience : {(k - k_theorie)/k_theorie:.0%}")


### PENSER  A  ZOOMER VERS L'ORIGINE POUR VOIR LES PREMIERS POINTS ###
plt.plot(abscisse, k * a, 'r-')
plt.plot(abscisse, ordonnee, 'o')
plt.xlabel("$a^3$ ($m^3$)")
plt.ylabel("$T^2$ ($s^2$)")
plt.show()


