La nuit

Calcul $h/M$ la nuit, entre 22h et 6h.


In [104]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

import pandas as pd

In [105]:
from influxdb import DataFrameClient

Download the data


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 )


2017-10-04 20:00:00 2017-10-05 04:00:00

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]:
[<matplotlib.lines.Line2D at 0x7fb492dce940>]

Fit spline


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]:
[<matplotlib.lines.Line2D at 0x7fb49321dfd0>]

In [118]:
spline.get_coeffs()


Out[118]:
array([ 23.22081493,  23.12580772,  22.82738884,  22.57059294])

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');


iterate


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' )


no data for 2017-10-19 20:00:00
Out[124]:
(array([], dtype=float64), array([], dtype=float64))

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 )


no data for 2017-06-08 20:00:00
no data for 2017-06-09 20:00:00
no data for 2017-06-10 20:00:00
no data for 2017-06-11 20:00:00
no data for 2017-06-12 20:00:00
no data for 2017-06-13 20:00:00
no data for 2017-06-14 20:00:00
no data for 2017-06-15 20:00:00
no data for 2017-06-16 20:00:00
no data for 2017-06-17 20:00:00
no data for 2017-06-18 20:00:00
no data for 2017-06-19 20:00:00
no data for 2017-06-20 20:00:00
no data for 2017-06-21 20:00:00
no data for 2017-06-22 20:00:00
no data for 2017-06-23 20:00:00
no data for 2017-06-24 20:00:00
no data for 2017-06-25 20:00:00
no data for 2017-06-26 20:00:00
no data for 2017-06-27 20:00:00
no data for 2017-06-28 20:00:00
no data for 2017-06-29 20:00:00
no data for 2017-10-18 20:00:00
no data for 2017-10-19 20:00:00
no data for 2017-10-20 20:00:00

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]:
(-10, 30)

In [129]:
np.median( all_hM )


Out[129]:
2.6679813214091741e-06

Summer time


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]:
(-10, 30)

In [102]:
np.median( all_hM )


Out[102]:
5.5608037449464079e-06

In [103]:
all_hM.mean()


Out[103]:
8.2369078991713554e-06

-- Write to InfluxDB --


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)


no data for 2017-06-08 20:00:00
no data for 2017-06-09 20:00:00
no data for 2017-06-10 20:00:00
no data for 2017-06-11 20:00:00
no data for 2017-06-12 20:00:00
no data for 2017-06-13 20:00:00
no data for 2017-06-14 20:00:00
no data for 2017-06-15 20:00:00
no data for 2017-06-16 20:00:00
no data for 2017-06-17 20:00:00
no data for 2017-06-18 20:00:00
no data for 2017-06-19 20:00:00
no data for 2017-06-20 20:00:00
no data for 2017-06-21 20:00:00
no data for 2017-06-22 20:00:00
no data for 2017-06-23 20:00:00
no data for 2017-06-24 20:00:00
no data for 2017-06-25 20:00:00
no data for 2017-06-26 20:00:00
no data for 2017-06-27 20:00:00
no data for 2017-06-28 20:00:00
no data for 2017-06-29 20:00:00
no data for 2017-10-18 20:00:00
no data for 2017-10-19 20:00:00
no data for 2017-10-20 20:00:00

In [ ]: