In [104]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
influxDB: http://127.0.0.1:8083/
Grafana: http://127.0.0.1:3000/dashboard/db/weatherdata?orgId=1
In [105]:
from influxdb import DataFrameClient
In [106]:
# see https://influxdb-python.readthedocs.io/en/latest/examples.html#tutorials-pandas
"""Instantiate the connection to the InfluxDB client."""
host, port = 'localhost', 8086
user, password = 'root', 'root'
dbname = 'weatherdata'
protocol = 'json' # 'line'
CLIENT = DataFrameClient(host, port, user, password, dbname)
In [107]:
d1 = '2017-10-04'
In [108]:
d1_DT = pd.to_datetime( d1 )
d2_DT = d1_DT + pd.to_timedelta('1d')
d1_DT = d1_DT.replace(hour=22)
d2_DT = d2_DT.replace(hour=6)
d1_DT = d1_DT.tz_localize('Europe/Paris')
d2_DT = d2_DT.tz_localize('Europe/Paris')
d1 = d1_DT.astimezone('UTC').strftime('%Y-%m-%d %H:%M:%S')
d2 = d2_DT.astimezone('UTC').strftime('%Y-%m-%d %H:%M:%S')
In [109]:
print( d1, d2 )
In [110]:
query = "SELECT T_int FROM openenergymonitor WHERE time >= '%s' and time <= '%s'"%(d1, d2)
T_int_data = CLIENT.query(query)
query = "SELECT temperature FROM darksky WHERE time >= '%s' and time <= '%s'"%(d1, d2)
T_ext_data = CLIENT.query(query)
In [111]:
def toarray( DF ):
data_dict = DF.to_dict(orient='list')
time_sec = DF.index.astype(np.int64) // 1e9 # conversion en secondes
time_sec = time_sec - time_sec[0]
#time_h = time_sec / 3600
data = [ k for k in data_dict.values() ][0]
return time_sec, data
In [112]:
time_Tint, T_int = toarray( T_int_data['openenergymonitor'] )
time_Text, T_ext = toarray( T_ext_data['darksky'] )
In [113]:
plt.plot( time_Tint, T_int )
plt.plot( time_Text, T_ext )
Out[113]:
In [114]:
from scipy.interpolate import UnivariateSpline
In [115]:
xnew = np.linspace( 0, time_Tint[-1], 200 )
In [116]:
spline = UnivariateSpline( time_Tint, T_int )
In [117]:
plt.plot( time_Tint, T_int )
plt.plot( xnew, spline( xnew ) )
plt.plot( time_Text, T_ext-np.mean(T_ext)+np.mean(T_int) )
Out[117]:
In [118]:
spline.get_coeffs()
Out[118]:
In [119]:
spline_dTdt = spline.derivative()
In [120]:
deltaT = T_ext - spline( time_Text )
dTdt = spline_dTdt( time_Text )
h_M = dTdt / deltaT
In [122]:
plt.plot( 1e6*h_M );
plt.ylabel('1e6 h_M');
In [123]:
# une fonction pour tout faire:
def get_hM( d1 ):
d1_DT = pd.to_datetime( d1 )
d2_DT = d1_DT + pd.to_timedelta('1d')
d1_DT = d1_DT.replace(hour=22)
d2_DT = d2_DT.replace(hour=6)
d1_DT = d1_DT.tz_localize('Europe/Paris')
d2_DT = d2_DT.tz_localize('Europe/Paris')
d1 = d1_DT.astimezone('UTC').strftime('%Y-%m-%d %H:%M:%S')
d2 = d2_DT.astimezone('UTC').strftime('%Y-%m-%d %H:%M:%S')
query = "SELECT T_int FROM openenergymonitor WHERE time >= '%s' and time <= '%s'"%(d1, d2)
T_int_data = CLIENT.query(query)
if not T_int_data:
print('no data for %s'%d1)
return np.array([]), np.array([])
query = "SELECT temperature FROM darksky WHERE time >= '%s' and time <= '%s'"%(d1, d2)
T_ext_data = CLIENT.query(query)
time_Tint, T_int = toarray( T_int_data['openenergymonitor'] )
time_Text, T_ext = toarray( T_ext_data['darksky'] )
spline = UnivariateSpline( time_Tint, T_int )#, s=1.05 )
spline_dTdt = spline.derivative()
deltaT = T_ext - spline( time_Text )
dTdt = spline_dTdt( time_Text )
h_M = dTdt / deltaT
#T_ext_data['darksky'].tz_convert('Europe/Paris')
time_index = T_ext_data['darksky'].index
return time_index, h_M
In [124]:
# debug:
get_hM( '2017-10-19' )
Out[124]:
In [125]:
# iterate :
firstday = '2017-06-08'
lastday = '2017-11-05'
daterange = pd.date_range(start=firstday, end=lastday, freq='D', normalize=True)
daterange_iso = daterange.strftime('%Y-%m-%d')
In [126]:
all_hM = []
for day in daterange_iso:
time, h_M = get_hM( day )
if len( h_M )>0:
all_hM.append( h_M )
plt.plot( h_M, alpha=0.5 )
In [127]:
all_hM = np.concatenate( all_hM )
all_hM = np.array( all_hM ).flatten()
In [128]:
plt.figure( figsize=(14, 4) )
plt.hist(1e6*all_hM, bins='auto') ;
plt.axvline(x=1e6*all_hM.mean(), color='r' );
plt.axvline(x=1e6*np.median( all_hM ), color='r' );
plt.xlim([-10, 30])
Out[128]:
In [129]:
np.median( all_hM )
Out[129]:
In [98]:
# iterate :
firstday = '2017-07-01'
lastday = '2017-07-14'
#firstday = '2017-08-07'
#lastday = '2017-08-21'
daterange = pd.date_range(start=firstday, end=lastday, freq='D', normalize=True)
daterange_iso = daterange.strftime('%Y-%m-%d')
In [99]:
all_hM = []
for day in daterange_iso:
time, h_M = get_hM( day )
if len( h_M )>0:
all_hM.append( h_M )
plt.plot( h_M, alpha=0.5 )
In [100]:
all_hM = np.concatenate( all_hM )
all_hM = np.array( all_hM ).flatten()
In [101]:
plt.figure( figsize=(14, 4) )
plt.hist(1e6*all_hM, bins='auto') ;
plt.axvline(x=1e6*all_hM.mean(), color='r' );
plt.axvline(x=np.median( 1e6*all_hM ), color='r' );
plt.xlim([-10, 30])
Out[101]:
In [102]:
np.median( all_hM )
Out[102]:
In [103]:
all_hM.mean()
Out[103]:
In [130]:
measurement_name = 'analyse'
for day in daterange_iso:
time, h_M = get_hM( day )
if len( time )==0:
continue
DF = pd.DataFrame( {'h_M':h_M}, index=time )
# Ajoute colone par colones pour retirer les NaN...
for c in DF.columns:
CLIENT.write_points(DF[[c]].dropna(), measurement_name)
In [ ]: