12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- namespace App\Yr\Forecast\Tabular;
- use App\Yr\Forecast\Tabular\Time\AbstractUnit;
- use App\Yr\Forecast\Tabular\Time\CustomUnit;
- use App\Yr\Forecast\Tabular\Time\Temperature;
- use App\Yr\Forecast\Tabular\Time\WindSpeed;
- /**
- * Make simple statistic on analysed time objects,
- * such as highest/lowest wind speed and temperature,
- * average wind speed and temperature etc.
- *
- * @author Joachim M. Giæver (joachim[]giaever.org)
- */
- class Statistics {
- private $temp = [];
- private $wind = [];
- private $count = 0;
- private $symbol = [];
- public function __construct() {
- $this->temp = $this->struct();
- $this->wind = $this->struct();
- }
- private function struct(): array {
- return [
- 'high' => null,
- 'low' => null,
- 'mean' => null
- ];
- }
- /**
- * Analyse a single Time-object
- *
- * @param Time $t The time object.
- * @return Statistics
- */
- public function analyse(Time $t): self {
- $this->analyseHihgLow($t->getTemperature());
- $this->analyseHihgLow($t->getWindSpeed());
- $this->temp['mean'] = $this->temp['mean'] == null ? $t->getTemperature() : $this->temp['mean']->add($t->getTemperature());
- $this->wind['mean'] = $this->wind['mean'] == null ? $t->getWindSpeed() : $this->wind['mean']->add($t->getWindSpeed());
- $symboldId = $t->getSymbol()->getNumber();
- if (!isset($this->symbol[$symboldId]))
- $this->symbol[$symboldId] = [
- 'symbol' => $t->getSymbol()->getName(),
- 'count' => 1
- ];
- else
- $this->symbol[$symboldId]['count']++;
- $this->count++;
- return $this;
- }
- public function getAverageTemperature(): AbstractUnit {
- return $this->temp['mean']->div(
- new CustomUnit($this->count, $this->temp['mean']->getUnit())
- );
- }
- public function getAverageWindSpeed(): AbstractUnit {
- return $this->wind['mean']->div(
- new CustomUnit($this->count, $this->wind['mean']->getUnit())
- );
- }
- private function analyseHihgLow(AbstractUnit $au): self {
- $unit = null;
- if ($au instanceof Temperature)
- $unit = &$this->temp;
- elseif ($au instanceof WindSpeed)
- $unit = &$this->wind;
- else
- return $this;
- if ($unit['low'] == null || $au->getValue() < $unit['low']->getValue())
- $unit['low'] = $au;
- if ($unit['high'] == null || $au->getValue() < $unit['high']->getValue())
- $unit['high'] = $au;
- return $this;
- }
- }
- ?>
|