Browse Source

Added additional stats

Joachim M. Giæver 4 years ago
parent
commit
46b2b1703f
4 changed files with 147 additions and 7 deletions
  1. 85 0
      README.md
  2. 10 6
      index.php
  3. 39 1
      src/Yr/Forecast/Tabular/Statistics.php
  4. 13 0
      src/Yr/Forecast/Tabular/Time/AbstractUnit.php

+ 85 - 0
README.md

@@ -419,6 +419,12 @@ average wind speed and temperature etc.
 |[analyse](#analyse-appyrforecasttabularstatistics)|[Statistics](#statistics-appyrforecasttabular)|public| Analyse a single Time-object|
 |[getAverageTemperature](#getaveragetemperature-appyrforecasttabularstatistics)|[AbstractUnit](#abstractunit-appyrforecasttabulartime)|public| |
 |[getAverageWindSpeed](#getaveragewindspeed-appyrforecasttabularstatistics)|[AbstractUnit](#abstractunit-appyrforecasttabulartime)|public| |
+|[getHighestTemperature](#gethighesttemperature-appyrforecasttabularstatistics)|[Temperature](#temperature-appyrforecasttabulartime)|public| |
+|[getLowestTemperature](#getlowesttemperature-appyrforecasttabularstatistics)|[Temperature](#temperature-appyrforecasttabulartime)|public| |
+|[getHighestWindspeed](#gethighestwindspeed-appyrforecasttabularstatistics)|[WindSpeed](#windspeed-appyrforecasttabulartime)|public| |
+|[getLowestWindSpeed](#getlowestwindspeed-appyrforecasttabularstatistics)|[WindSpeed](#windspeed-appyrforecasttabulartime)|public| |
+|[getAverageSymbols](#getaveragesymbols-appyrforecasttabularstatistics)|array|public| |
+|[getMostCommonSymbol](#getmostcommonsymbol-appyrforecasttabularstatistics)|array|public| |
 
 #### Method details
 
@@ -467,6 +473,66 @@ Returns: [AbstractUnit](#abstractunit-appyrforecasttabulartime)
 ---
 
 
+##### getHighestTemperature `App\Yr\Forecast\Tabular\Statistics`
+```php
+public function getHighestTemperature();
+```
+
+Returns: [Temperature](#temperature-appyrforecasttabulartime)
+
+---
+
+
+##### getLowestTemperature `App\Yr\Forecast\Tabular\Statistics`
+```php
+public function getLowestTemperature();
+```
+
+Returns: [Temperature](#temperature-appyrforecasttabulartime)
+
+---
+
+
+##### getHighestWindspeed `App\Yr\Forecast\Tabular\Statistics`
+```php
+public function getHighestWindspeed();
+```
+
+Returns: [WindSpeed](#windspeed-appyrforecasttabulartime)
+
+---
+
+
+##### getLowestWindSpeed `App\Yr\Forecast\Tabular\Statistics`
+```php
+public function getLowestWindSpeed();
+```
+
+Returns: [WindSpeed](#windspeed-appyrforecasttabulartime)
+
+---
+
+
+##### getAverageSymbols `App\Yr\Forecast\Tabular\Statistics`
+```php
+public function getAverageSymbols();
+```
+
+Returns: array
+
+---
+
+
+##### getMostCommonSymbol `App\Yr\Forecast\Tabular\Statistics`
+```php
+public function getMostCommonSymbol();
+```
+
+Returns: array
+
+---
+
+
 ### Time `App\Yr\Forecast\Tabular`
 
 
@@ -688,6 +754,7 @@ Time-object entity should inherit this
 |[__construct](#__construct-appyrforecasttabulartimeabstractunit)||public| |
 |[transformClass](#transformclass-appyrforecasttabulartimeabstractunit)|[AbstractUnit](#abstractunit-appyrforecasttabulartime)|protected| |
 |[add](#add-appyrforecasttabulartimeabstractunit)|[AbstractUnit](#abstractunit-appyrforecasttabulartime)|public| Addition method|
+|[sub](#sub-appyrforecasttabulartimeabstractunit)|[AbstractUnit](#abstractunit-appyrforecasttabulartime)|public| Subtraction method|
 |[mul](#mul-appyrforecasttabulartimeabstractunit)|[AbstractUnit](#abstractunit-appyrforecasttabulartime)|public| Multiplication method|
 |[div](#div-appyrforecasttabulartimeabstractunit)|[AbstractUnit](#abstractunit-appyrforecasttabulartime)|public| Divide method|
 |[getValue](#getvalue-appyrforecasttabulartimeabstractunit)|float|public| Get the value|
@@ -745,6 +812,24 @@ Returns: [AbstractUnit](#abstractunit-appyrforecasttabulartime)
 ---
 
 
+##### sub `App\Yr\Forecast\Tabular\Time\AbstractUnit`
+```php
+public function sub(AbstractUnit $with);
+```
+ Subtraction method
+
+
+Parameters
+
+| Type | Variable | Description |
+|---|---|---|
+|[AbstractUnit](#time-appyrforecasttabular)|$with|Unit to sub with|
+
+Returns: [AbstractUnit](#abstractunit-appyrforecasttabulartime)
+
+---
+
+
 ##### mul `App\Yr\Forecast\Tabular\Time\AbstractUnit`
 ```php
 public function mul(AbstractUnit $with);

+ 10 - 6
index.php

@@ -11,21 +11,25 @@ $range = $forecast->getTabular()->getBetween(
 );
 
 echo '<pre>';
-foreach ($range as $time)
-    echo $time . "\n";
-
-echo "\n<hr />\n";
-foreach ($range->getVariations()->getData() as $data) 
-    echo sprintf("%s: %s\n", $data['time']->getFrom()->format("H:i"), join(", ", $data['entities']));
+foreach ($range->getVariations()->getData() as $data) {
+    echo $data['time']->getFrom()->format("H:i");
+}
+    //echo sprintf("%s: %s\n", $data['time']->getFrom()->format("H:i"), join(", ", $data['entities']));
 
 $f = current(iterator_to_array($range->getIterator()));
 echo $f->getWindSpeed()->convertTo('ft/s');
 
+echo "\n";
 $stat = $range->getStatistics();
 
+echo "\n";
 echo $stat->getAverageTemperature();
+echo "\n";
 echo $stat->getAverageWindSpeed();
 
+echo "\n";
+var_dump($stat->getAverageSymbols());
+var_dump($stat->getMostCommonSymbol());
 echo "\n";
 foreach ($forecast->getCredit() as $c)
     echo $c . "\n";

+ 39 - 1
src/Yr/Forecast/Tabular/Statistics.php

@@ -51,7 +51,8 @@ class Statistics {
         if (!isset($this->symbol[$symboldId]))
             $this->symbol[$symboldId] = [
                 'symbol' => $t->getSymbol()->getName(),
-                'count' => 1
+                'count' => 1,
+                'avg' => 0,
             ];
         else
             $this->symbol[$symboldId]['count']++;
@@ -72,6 +73,43 @@ class Statistics {
         );
     }
 
+    public function getHighestTemperature(): Temperature {
+        return $this->temp['high'];
+    }
+
+    public function getLowestTemperature(): Temperature {
+        return $this->temp['low'];
+    }
+
+    public function getHighestWindspeed(): WindSpeed {
+        return $this->wind['high'];
+    }
+
+    public function getLowestWindSpeed(): WindSpeed {
+        return $this->wind['low'];
+    }
+
+    public function getAverageSymbols(): array {
+        if ($s = current($this->symbol))
+           if ($s != null && $s['avg'] != 0)
+               return $this->symbol;
+
+        $this->symbol = array_map(function(array $symbol) {
+            $symbol['avg'] = new CustomUnit($symbol['count'] / $this->count * 100, '%');
+            return $symbol;
+        }, $this->symbol);
+
+        usort($this->symbol, function(array $a, array $b) {
+            return (int)($a['avg']->sub($b['avg']))->getValue();
+        });
+
+        return $this->symbol;
+    }
+
+    public function getMostCommonSymbol(): array {
+        return current(array_reverse($this->getAverageSymbols()));
+    }
+
     private function analyseHihgLow(AbstractUnit $au): self {
         $unit = null;
 

+ 13 - 0
src/Yr/Forecast/Tabular/Time/AbstractUnit.php

@@ -57,6 +57,19 @@ abstract class AbstractUnit implements DiffInterface {
         );
     }
 
+    /**
+     * Subtraction method
+     *
+     * @param AbstractUnit $with Unit to sub with
+     * @thows \InvalidArgumentException
+     * */
+    public function sub(AbstractUnit $with): self {
+        $this->canOperate($with, true);
+        return $this->transformClass(
+            new CustomUnit($this->getValue() - $with->getValue(), $with->getUnit())
+        );
+    }
+
     /**
      * Multiplication method
      *