def test_util_findDateIndex():
    # Testing index finder for specific dates.
    a = datetime.datetime(2014, 1, 1, 0, 0, 0, 0)
    b = datetime.datetime(2014, 1, 2, 0, 0, 0, 0)
    datetime_list = numpy.array(set_up_datetimes(a, b, 3600)).flatten()
    testDateIndex = dtt.findDateIndex(
        datetime_list, datetime.datetime(2014, 1, 1, 7, 0, 0, 0))
    assert testDateIndex == 0
    testDateIndex = dtt.findDateIndex(
        datetime_list, datetime.datetime(2014, 2, 1, 0, 0, 0, 0))
    assert testDateIndex == 24
def test_util_findDateIndex():
    # Testing index finder for specific dates.
    a = datetime.datetime(2014, 1, 1, 0, 0, 0, 0)
    b = datetime.datetime(2014, 1, 2, 0, 0, 0, 0)
    datetime_list = numpy.array(set_up_datetimes(a, b, 3600)).flatten()
    testDateIndex = dtt.findDateIndex(
        datetime_list,
        datetime.datetime(2014, 1, 1, 7, 0, 0, 0)
        )
    assert testDateIndex == 0
    testDateIndex = dtt.findDateIndex(
        datetime_list,
        datetime.datetime(2014, 2, 1, 0, 0, 0, 0)
        )
    assert testDateIndex == 24
    def execute(self):
        # Called after User hits GO
        """
        Calculates weather sensitivity using Spearman rank.
        Also, outputs data points for energy signature scatter plot.
        """
        self.out.log("Starting Day Time Temperature Analysis", logging.INFO)

        # Gather loads and outside air temperatures. Reduced to an hourly average
        
        load_query = self.inp.get_query_sets('load', group_by='hour',
                                             group_by_aggregation=Avg,
                                             exclude={'value':None},
                                             wrap_for_merge=True)
        oat_query = self.inp.get_query_sets('oat', group_by='hour', 
                                             group_by_aggregation=Avg,
                                             exclude={'value':None},
                                             wrap_for_merge=True)

        # Get conversion factor
        base_topic = self.inp.get_topics()
        meta_topics = self.inp.get_topics_meta()
        load_unit = meta_topics['load'][base_topic['load'][0]]['unit']
        temperature_unit = meta_topics['oat'][base_topic['oat'][0]]['unit']
        
        load_convertfactor = cu.conversiontoKWH(load_unit)
        
        # Match the values by timestamp
        merged_load_oat = self.inp.merge(load_query, oat_query) 

        load_values = []
        oat_values = []
        datetime_values = []

        for x in merged_load_oat:
            if temperature_unit == 'celcius':
                convertedTemp = cu.convertCelciusToFahrenheit(x['oat'][0])
            elif temperature_unit == 'kelvin':
                convertedTemp = cu.convertKelvinToCelcius(
                                cu.convertCelciusToFahrenheit(x['oat'][0]))
            else: 
                convertedTemp = x['oat'][0]
                
            load_values.append(x['load'][0] * load_convertfactor) #Converted to kWh
            oat_values.append(convertedTemp)
            datetime_values.append(dt.datetime.strptime(x['time'],'%Y-%m-%d %H'))
            
        indexList = {}
        indexList['trainingStart'] = ttow.findDateIndex(datetime_values, self.baseline_start)
        self.out.log('@trainingStart '+str(indexList['trainingStart']), logging.INFO)
        indexList['trainingStop'] = ttow.findDateIndex(datetime_values, self.baseline_stop)
        self.out.log('@trainingStop '+str(indexList['trainingStop']), logging.INFO)
        indexList['predictStart'] = ttow.findDateIndex(datetime_values, self.savings_start)
        self.out.log('@predictStart '+str(indexList['predictStart']), logging.INFO)
        indexList['predictStop'] = ttow.findDateIndex(datetime_values, self.savings_stop)
        self.out.log('@predictStop '+str(indexList['predictStop']), logging.INFO)
        
        for indx in indexList.keys():
            if indexList[indx] == None:
                self.out.log("Date not found in the datelist", logging.WARNING)
                
        # Break up data into training and prediction periods.
        timesTrain = datetime_values[indexList['trainingStart']:indexList['trainingStop']]
        timesPredict = datetime_values[indexList['predictStart']:indexList['predictStop']]

        valsTrain = load_values[indexList['trainingStart']:indexList['trainingStop']]
        valsActual = load_values[indexList['predictStart']:indexList['predictStop']]

        oatsTrain = oat_values[indexList['trainingStart']:indexList['trainingStop']]
        oatsPredict = oat_values[indexList['predictStart']:indexList['predictStop']]

        # Generate other information needed for model.
        timeStepMinutes = (timesTrain[1] - timesTrain[0]).total_seconds()/60  
        # TODO: Should this be calculated in the utility function
        binCt = 6  # TODO: Allow caller to pass this in as an argument.

        # Form the temperature-time-of-week model.
        self.out.log("Starting baseline model", logging.INFO)
        ttowModel = ttow.formModel(timesTrain, 
                                   oatsTrain, 
                                   valsTrain,
                                   timeStepMinutes, 
                                   binCt)

        # Apply the model.
        self.out.log("Applying baseline model", logging.INFO)
        valsPredict = ttow.applyModel(ttowModel, timesPredict, oatsPredict)
        
        # Output for scatter plot
        prevSum = 0
        for ctr in range(len(timesPredict)):
            # Calculate cumulative savings. 
            prevSum += (valsPredict[ctr] - valsActual[ctr])
            self.out.insert_row("DayTimeTemperatureModel", { 
                                "datetimeValues": timesPredict[ctr],
                                "measured": valsActual[ctr],
                                "predicted": valsPredict[ctr],
                                "cumulativeSum": prevSum
                                })
    def execute(self):
        # Called after User hits GO
        """
        Calculates weather sensitivity using Spearman rank.
        Also, outputs data points for energy signature scatter plot.
        """
        self.out.log("Starting application: whole building energy savings.",
                     logging.INFO)

        # Gather loads and outside air temperatures. Reduced to an hourly average
        self.out.log("Querying database.", logging.INFO)
        load_query = self.inp.get_query_sets('load',
                                             group_by='hour',
                                             group_by_aggregation=Avg,
                                             exclude={'value': None},
                                             wrap_for_merge=True)
        oat_query = self.inp.get_query_sets('oat',
                                            group_by='hour',
                                            group_by_aggregation=Avg,
                                            exclude={'value': None},
                                            wrap_for_merge=True)

        self.out.log("Getting unit conversions.", logging.INFO)
        base_topic = self.inp.get_topics()
        meta_topics = self.inp.get_topics_meta()

        load_unit = meta_topics['load'][base_topic['load'][0]]['unit']
        self.out.log("Convert loads from [{}] to [kW].".format(load_unit),
                     logging.INFO)
        load_convertfactor = cu.getFactor_powertoKW(load_unit)

        temperature_unit = meta_topics['oat'][base_topic['oat'][0]]['unit']
        self.out.log(
            "Convert temperatures from [{}] to [F].".format(temperature_unit),
            logging.INFO)

        # Match the values by timestamp
        merged_load_oat = self.inp.merge(load_query, oat_query)

        load_values = []
        oat_values = []
        datetime_values = []

        for x in merged_load_oat:
            if temperature_unit == 'celcius':
                convertedTemp = cu.convertCelciusToFahrenheit(x['oat'][0])
            elif temperature_unit == 'kelvin':
                convertedTemp = cu.convertKelvinToCelcius(
                    cu.convertCelciusToFahrenheit(x['oat'][0]))
            else:
                convertedTemp = x['oat'][0]

            load_values.append(x['load'][0] *
                               load_convertfactor)  #Converted to kWh
            oat_values.append(convertedTemp)
            datetime_values.append(x['time'])

        indexList = {}
        indexList['trainingStart'] = ttow.findDateIndex(
            datetime_values, self.baseline_start)
        self.out.log('@trainingStart ' + str(indexList['trainingStart']),
                     logging.INFO)
        indexList['trainingStop'] = ttow.findDateIndex(datetime_values,
                                                       self.baseline_stop)
        self.out.log('@trainingStop ' + str(indexList['trainingStop']),
                     logging.INFO)
        indexList['predictStart'] = ttow.findDateIndex(datetime_values,
                                                       self.savings_start)
        self.out.log('@predictStart ' + str(indexList['predictStart']),
                     logging.INFO)
        indexList['predictStop'] = ttow.findDateIndex(datetime_values,
                                                      self.savings_stop)
        self.out.log('@predictStop ' + str(indexList['predictStop']),
                     logging.INFO)

        for indx in indexList.keys():
            if indexList[indx] == None:
                self.out.log("Date not found in the datelist", logging.WARNING)

        # Break up data into training and prediction periods.
        timesTrain = datetime_values[
            indexList['trainingStart']:indexList['trainingStop']]
        timesPredict = datetime_values[
            indexList['predictStart']:indexList['predictStop']]

        valsTrain = load_values[
            indexList['trainingStart']:indexList['trainingStop']]
        valsActual = load_values[
            indexList['predictStart']:indexList['predictStop']]

        oatsTrain = oat_values[
            indexList['trainingStart']:indexList['trainingStop']]
        oatsPredict = oat_values[
            indexList['predictStart']:indexList['predictStop']]

        # Generate other information needed for model.
        timeStepMinutes = (timesTrain[1] - timesTrain[0]).total_seconds() / 60
        # TODO: Should this be calculated in the utility function
        binCt = 6  # TODO: Allow caller to pass this in as an argument.

        # Form the temperature-time-of-week model.
        self.out.log("Finding baseline model", logging.INFO)
        ttowModel = ttow.formModel(timesTrain, oatsTrain, valsTrain,
                                   timeStepMinutes, binCt)

        # Apply the model.
        self.out.log("Applying baseline model", logging.INFO)
        valsPredict = ttow.applyModel(ttowModel, timesPredict, oatsPredict)

        # Output for scatter plot
        prevSum = 0
        for ctr in range(len(timesPredict)):
            # Calculate cumulative savings.
            prevSum += (valsPredict[ctr] - valsActual[ctr])
            local_time = str(
                self.inp.localize_sensor_time(base_topic['load'][0],
                                              timesPredict[ctr]))
            self.out.insert_row(
                "DayTimeTemperatureModel", {
                    "datetimeValues": local_time,
                    "measured": valsActual[ctr],
                    "predicted": valsPredict[ctr],
                    "cumulativeSum": prevSum
                })