/**
  * Convert a frequency constant (or array thereof) to string(s).
  *
  * @param $frequency
  *    Input frequency constant.
  * @return bool|string
  *    Appropriate string, e.g. 'month'.
  * @throws Exception
  *    On unknown frequency.
  */
 public static function frequencyToStr($frequency)
 {
     // Handle an array of frequencies.
     if (is_array($frequency)) {
         $return = array();
         foreach ($frequency as $f) {
             array_push($return, \NegaWattNormalizerTimeManagerBase::frequencyToStr($f));
         }
         return $return;
     }
     // Handle a single frequency.
     switch ($frequency) {
         case \NegaWattNormalizerTimeManagerInterface::YEAR:
             return 'year';
         case \NegaWattNormalizerTimeManagerInterface::MONTH:
             return 'month';
         case \NegaWattNormalizerTimeManagerInterface::DAY:
             return 'day';
         case \NegaWattNormalizerTimeManagerInterface::HOUR:
             return 'hour';
         case \NegaWattNormalizerTimeManagerInterface::MINUTE:
             return 'minute';
         default:
             throw new \Exception(format_string('Frequency "@freq" not known.', array('@freq' => $frequency)));
     }
 }
 /**
  * Process raw entities in a certain time frame.
  *
  * Process raw-electricity entities between two given timestamps, and create
  * the appropriate normalized-electricity entities.
  *
  * @param int $from_timestamp
  *    Timestamp of beginning of time frame.
  * @param int $to_timestamp
  *    Timestamp of end of time frame.
  * @param int $frequency
  *    Current frequency.
  *
  * @return array
  *    The processed normalized entities in the form array[timestamp][rate-type] = entity.
  */
 public function processRawEntities($from_timestamp, $to_timestamp, $frequency)
 {
     // Output debug message.
     self::debugMessage('in processRawEntities.', 2);
     self::debugMessage(format_string('frequency: @frequency, time-frame: [@time_from,@time_to]', array('@frequency' => \NegaWattNormalizerTimeManagerBase::frequencyToStr($frequency))), 2, $from_timestamp, $to_timestamp);
     // Create data provider.
     $data_provider = $this->getDataProviderManager()->createDataProvider($from_timestamp, $to_timestamp, $frequency, $this->getMeterNode());
     // Get data from raw table.
     $result = $data_provider->getDataFromRawTable();
     // Loop for received rate types and save the entities.
     $processed_entities = array();
     $prev_record = $data_provider->getRawEntityBefore();
     $prev_time_delta = NULL;
     $time_delta = NULL;
     foreach ($result as $record) {
         // Allow the sub-classed meter-normalizer to process the data
         $processed_record = $this->processRawEntity($record, $prev_record);
         // Test for bad meter-reading
         if ($processed_record->kwh === NULL) {
             $this->handleBadMeterReading($processed_record);
         } else {
             if ($this->hasBadMeterReadings()) {
                 // Update retroactively all the bad readings with the average power
                 // consumption at the bad-readings period.
                 $this->endBadMeterReadingsProcessing($processed_record);
             }
         }
         // Add a new normalized entity.
         $this->addNormalizedEntity($prev_record, $processed_record, $prev_time_delta, $time_delta, $processed_entities, $data_provider, $frequency);
         // Save record and time-delta for next iteration.
         $prev_record = $record;
         $prev_time_delta = $time_delta;
     }
     // The last timestamp processed is the timestamp of last iteration.
     $last_processed = $prev_record ? $prev_record->timestamp : NULL;
     // Output debug message.
     self::debugMessage(format_string('total entities: @count, last processed: @time_from', array('@count' => count($processed_entities))), 3, $last_processed);
     return array('entities' => &$processed_entities, 'last_processed' => $last_processed);
 }