import pandas as pd
import matplotlib.pyplot as plt

%matplotlib inline

"""
Explanation: Importamos las librerías necesarias
End of explanation
"""


import ext_datos as ext
import procesar as pro
import time_plot as tplt

"""
Explanation: Importamos las librerías creadas para trabajar
End of explanation
"""


dia1 = ext.extraer_data('dia1')

motoresdia1 = pro.procesar(dia1)

from scipy import integrate
import pandas as pd
import numpy as np

def integrate_method(self, how='trapz', unit='s'):
    '''Numerically integrate the time series.

    @param how: the method to use (trapz by default)
    @return 

    Available methods:
     * trapz - trapezoidal
     * cumtrapz - cumulative trapezoidal
     * simps - Simpson's rule
     * romb - Romberger's rule

    See http://docs.scipy.org/doc/scipy/reference/integrate.html for the method details.
    or the source code
    https://github.com/scipy/scipy/blob/master/scipy/integrate/quadrature.py
    '''
    available_rules = set(['trapz', 'cumtrapz', 'simps', 'romb'])
    if how in available_rules:
        rule = integrate.__getattribute__(how)
    else:
        print('Unsupported integration rule: %s' % (how))
        print('Expecting one of these sample-based integration rules: %s' % (str(list(available_rules))))
        raise AttributeError
    
    result = rule(self.values, self.index.astype(np.int64) / 10**9)
    #result = rule(self.values)
    return result

pd.TimeSeries.integrate = integrate_method

motoresdia1['pot_m1'] = motoresdia1.busCurrent_m1*motoresdia1.busVoltage_m1
motoresdia1['pot_m2'] = motoresdia1.busCurrent_m2*motoresdia1.busVoltage_m2

motoresdia1['pot_total'] = motoresdia1.pot_m1+motoresdia1.pot_m2

motoresdia1.busCurrent_m1.plot()
motoresdia1.busCurrent_m2.plot()

tplt.horam([motoresdia1.pot_m1,motoresdia1.pot_m2,motoresdia1.pot_total],'10:25','10:40')

motoresdia1[['motorRpm_m1','motorRpm_m1','pot_m1', 'pot_m2','pot_total']]['2014-11-13 12:00:00':'2014-11-13 12:15:00']

motoresdia1.pot_total.dropna().integrate()/3600

"""
Explanation: Generamos los datasets de todos los días


En primer lugar se extraen los datos de todos los archivos
    de cada día y se genera una lista de tablas separadas por motor
End of explanation
"""


