#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat May 28 16:06:15 2022

@author: tex
"""

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


os.chdir('/media/tex/PRO_SYLVIE/physiquespe/TIPE/chaufferette/')
#os.chdir('D:/')
donnees=open('mesures.txt','r')
lignes=donnees.readlines()
donnees.close()
taille=len(lignes)

temps=[]
T1=[]#température sur le dos de la main
T2=[]#température entre le gant et la chaufferette
T3=[]#température entre la chaufferette et la main 
for i in range(0,len(lignes)-1):
    temps.append((i-1)*5.)
    l=lignes[i]
    l=l.strip()
    l=l.split('\t')
    for i in range(len(l)):
        l[i]=l[i].replace(',','.')
    T1.append(float(l[0]))
    T2.append(float(l[1]))
    T3.append(float(l[2]))
temps=np.array(temps)
T1=np.array(T1)
T2=np.array(T2)
T3=np.array(T3)

plt.plot(temps,T1)
plt.plot(temps,T2)
plt.plot(temps,T3)
plt.legend(['T1','T2','T3'])
plt.grid()
plt.show()

from scipy.optimize import curve_fit

ti=500

j=0
while temps[j]<ti:
    j=j+1
    
print('T20=',T2[j])



def modele(x,tau,Ti,Tf):
    return Tf+(Ti-Tf)*np.exp(-(x-ti)/tau)

tempsm=[temps[i] for i in range(j,len(temps))]
Tm=[T2[i] for i in range(j,len(temps))]

tempsm=np.array(tempsm)
Tm=np.array(Tm)
        
plt.plot(tempsm,Tm,c='blue')



tau=3000
Ti=T2[j]
Tf=T1[0]


bestvals, covar = curve_fit(modele,tempsm,Tm,[tau,Ti,Tf])
print('tau,Ti,Tf',bestvals)
#tau,Ti,Tf [2544.62388563   46.60594263   32.32176918]


tau=bestvals[0]
Ti=bestvals[1]
Tf=bestvals[2]
T_fit =modele(tempsm,tau,Ti,Tf,)
plt.plot(tempsm,T_fit, label="modele",c='red')
plt.legend()
plt.grid()
plt.show()





