aboutsummaryrefslogtreecommitdiff
blob: 1b324a845be8f81292e6e654fbc4b7b37df355d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 11/30/17
 * Time: 11:27 AM
 *
 *
 */

Namespace AppBundle\Utils;

class Aqi {
    public function calculateAQI($gasName, $concentration, $table) {
        $bpLow = 1;
        $bpHi = 2;
        $bpLowIndex = 1;
        $bpHiIndex = 1;

        $arr = $table ->{$gasName} ->{'breakpoints'};
        foreach ($arr as $index => $value) {
            if ($value <= $concentration && $table->{$gasName}->{'breakpoints'}[$index + 1] >= $concentration) {
                $bpLow = $value;
                $bpLowIndex = $index;
            }

            if ($value >= $concentration && $table->{$gasName}->{'breakpoints'}[$index - 1] <= $concentration) {
                $bpHi = $value;
                $bpHiIndex = $index;
            }

        };



        $airQualityIndex = (($table->{$gasName}->{'aq'}[$bpHiIndex] - $table->{$gasName}->{'aq'}[$bpLowIndex]) / ($bpHi - $bpLow)) * ($concentration - $bpLow) + $table->{$gasName}->{'aq'}[$bpLowIndex];

        return $airQualityIndex;

    }
}

?>