Statistics.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Yr\Forecast\Tabular;
  3. use App\Yr\Forecast\Tabular\Time\AbstractUnit;
  4. use App\Yr\Forecast\Tabular\Time\Temperature;
  5. use App\Yr\Forecast\Tabular\Time\WindSpeed;
  6. /**
  7. * Make simple statistic on analysed time objects,
  8. * such as highest/lowest wind speed and temperature,
  9. * average wind speed and temperature etc.
  10. *
  11. * @author Joachim M. Giæver (joachim[]giaever.org)
  12. */
  13. class Statistics {
  14. private $temp = [];
  15. private $wind = [];
  16. private $count = 0;
  17. public function __construct() {
  18. $this->temp = $this->wind = [
  19. 'high' => null,
  20. 'low' => null,
  21. 'mean' => 0
  22. ];
  23. }
  24. /**
  25. * Analyse a single Time-object
  26. *
  27. * @param Time $t The time object.
  28. * @return Statistics
  29. */
  30. public function analyse(Time $t): self {
  31. $this->analyseHihgLow($t->getTemperature());
  32. $this->analyseHihgLow($t->getWindSpeed());
  33. $this->temp['mean'] += $t->getTemperature()->getValue();
  34. $this->wind['mean'] += $t->getWindSpeed()->getValue();
  35. return $this;
  36. }
  37. private function analyseHihgLow(AbstractUnit $au): self {
  38. $unit = null;
  39. if ($au instanceof Temperature)
  40. $unit = &$this->temp;
  41. elseif ($au instanceof WindSpeed)
  42. $unit = &$this->wind;
  43. else
  44. return $this;
  45. if ($unit['low'] == null || $au->getValue() < $unit['low']->getValue())
  46. $unit['low'] = $au;
  47. if ($unit['high'] == null || $au->getValue() < $unit['high']->getValue())
  48. $unit['high'] = $au;
  49. return $this;
  50. }
  51. }
  52. ?>