This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
airquality.php
256 lines (215 loc) · 8.67 KB
/
airquality.php
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
Class airquality
{
var $station = NULL;
var $measurement = NULL;
var $validMeasurements = Array();
var $result = Array();
// ------------------------------------------------------------------------
// Constructor
// Checks that station is number
public function __construct($station, $measurement)
{
$this->validMeasurements['particulateslt10um'] = TRUE;
$this->validMeasurements['nitrogendioxide'] = TRUE;
$this->validMeasurements['particulateslt2.5um'] = TRUE;
$this->validMeasurements['carbonmonoxide'] = TRUE;
$this->validMeasurements['ozone'] = TRUE;
$this->validMeasurements['sulphurdioxide'] = TRUE;
$this->validMeasurements['odorsulphurcompounds'] = TRUE;
// Validate input
if ( !( is_numeric($station) && $station==(int)$station ) )
{
throw new Exception("ss (station) must be a number");
}
if ( !$this->validMeasurement($measurement) )
{
throw new Exception("p (measurement) is invalid");
}
$this->station = $station;
$this->measurement = $measurement;
// Scraping
$this->evaluateMeasurement();
}
// ------------------------------------------------------------------------
//
public function validMeasurement($measurement)
{
if ($this->validMeasurements[$measurement] || "qualityIndex" == $measurement)
{
return TRUE;
}
else
{
return FALSE;
}
}
// ------------------------------------------------------------------------
//
public function evaluateMeasurement()
{
if ("qualityIndex" == $this->measurement)
{
$this->evaluateQualityIndex($type);
}
else
{
$scraper = new scraper($this->station, $this->measurement);
$this->result = $scraper->returnResult();
$this->calculateIndex();
}
}
// ------------------------------------------------------------------------
// Calculates air quality index
public function evaluateQualityIndex()
{
// Goes through all measurements, tries to pick the time & metadata from each, since we don't know which measurement is available. This will lead to the sourceURL will be from the last measurement.
foreach ($this->validMeasurements as $measurementName => $temp)
{
try
{
$scraper = new scraper($this->station, $measurementName);
$partsTemp = $scraper->returnResult();
// print_r ($partsTemp); exit("DEBUG");
$this->result['latest']['parts'][$measurementName] = $partsTemp['latest']['data'];
$this->result['latest']['time'] = $partsTemp['latest']['time'];
$this->result['metadata'] = $partsTemp['metadata'];
}
catch (Exception $e)
{
$this->result['latest']['parts'][$measurementName] = NULL;
}
}
$this->result['metadata']['measurement'] = "qualityIndex";
// Values from http://www.hsy.fi/seututieto/ilmanlaatu/tiedotus/indeksi/Sivut/default.aspx
// All units are micrograms/m3
if (NULL === $this->result['latest']['parts']['nitrogendioxide'] && NULL === $this->result['latest']['parts']['particulateslt2.5um'] && NULL === $this->result['latest']['parts']['particulateslt10um'] && NULL === $this->result['latest']['parts']['carbonmonoxide'] && NULL === $this->result['latest']['parts']['ozone'] && NULL === $this->result['latest']['parts']['sulphurdioxide'] && NULL === $this->result['latest']['parts']['odorsulphurcompounds'])
{
throw new Exception("This station doesn't yet have an air quality index for today.");
}
elseif ($this->result['latest']['parts']['nitrogendioxide'] > 200 || $this->result['latest']['parts']['particulateslt2.5um'] > 75 || $this->result['latest']['parts']['particulateslt10um'] > 200 || $this->result['latest']['parts']['carbonmonoxide'] > 30000 || $this->result['latest']['parts']['ozone'] > 180 || $this->result['latest']['parts']['sulphurdioxide'] > 350 || $this->result['latest']['parts']['odorsulphurcompounds'] > 50)
{
$this->result['latest']['index'] = 5;
$this->result['latest']['FI'] = "erittäin huono";
$this->result['latest']['EN'] = "very bad";
}
elseif ($this->result['latest']['parts']['nitrogendioxide'] > 150 || $this->result['latest']['parts']['particulateslt2.5um'] > 50 || $this->result['latest']['parts']['particulateslt10um'] > 100 || $this->result['latest']['parts']['carbonmonoxide'] > 20000 || $this->result['latest']['parts']['ozone'] > 140 || $this->result['latest']['parts']['sulphurdioxide'] > 250 || $this->result['latest']['parts']['odorsulphurcompounds'] > 20)
{
$this->result['latest']['index'] = 4;
$this->result['latest']['FI'] = "huono";
$this->result['latest']['EN'] = "bad";
}
elseif ($this->result['latest']['parts']['nitrogendioxide'] > 70 || $this->result['latest']['parts']['particulateslt2.5um'] > 25 || $this->result['latest']['parts']['particulateslt10um'] > 50 || $this->result['latest']['parts']['carbonmonoxide'] > 8000 || $this->result['latest']['parts']['ozone'] > 100 || $this->result['latest']['parts']['sulphurdioxide'] > 80 || $this->result['latest']['parts']['odorsulphurcompounds'] > 10)
{
$this->result['latest']['index'] = 3;
$this->result['latest']['FI'] = "välttävä";
$this->result['latest']['EN'] = "mediocre";
}
elseif ($this->result['latest']['parts']['nitrogendioxide'] > 40 || $this->result['latest']['parts']['particulateslt2.5um'] > 10 || $this->result['latest']['parts']['particulateslt10um'] > 20 || $this->result['latest']['parts']['carbonmonoxide'] > 4000 || $this->result['latest']['parts']['ozone'] > 60 || $this->result['latest']['parts']['sulphurdioxide'] > 20 || $this->result['latest']['parts']['odorsulphurcompounds'] > 5)
{
$this->result['latest']['index'] = 2;
$this->result['latest']['FI'] = "tyydyttävä";
$this->result['latest']['EN'] = "satisfactory";
}
else
{
$this->result['latest']['index'] = 1;
$this->result['latest']['FI'] = "hyvä";
$this->result['latest']['EN'] = "good";
}
// $this->debugThisArray($this->result);
}
// ------------------------------------------------------------------------
// Adds index to a measurement
public function calculateIndex()
{
// limts as micrograms/m3
$indexMaxLimits['nitrogendioxide'][1] = 40;
$indexMaxLimits['nitrogendioxide'][2] = 70;
$indexMaxLimits['nitrogendioxide'][3] = 150;
$indexMaxLimits['nitrogendioxide'][4] = 200;
$indexMaxLimits['particulateslt2.5um'][1] = 10;
$indexMaxLimits['particulateslt2.5um'][2] = 25;
$indexMaxLimits['particulateslt2.5um'][3] = 50;
$indexMaxLimits['particulateslt2.5um'][4] = 75;
$indexMaxLimits['particulateslt10um'][1] = 20;
$indexMaxLimits['particulateslt10um'][2] = 50;
$indexMaxLimits['particulateslt10um'][3] = 100;
$indexMaxLimits['particulateslt10um'][4] = 200;
$indexMaxLimits['carbonmonoxide'][1] = 4000;
$indexMaxLimits['carbonmonoxide'][2] = 8000;
$indexMaxLimits['carbonmonoxide'][3] = 20000;
$indexMaxLimits['carbonmonoxide'][4] = 30000;
$indexMaxLimits['ozone'][1] = 60;
$indexMaxLimits['ozone'][2] = 100;
$indexMaxLimits['ozone'][3] = 140;
$indexMaxLimits['ozone'][4] = 180;
$indexMaxLimits['sulphurdioxide'][1] = 20;
$indexMaxLimits['sulphurdioxide'][2] = 80;
$indexMaxLimits['sulphurdioxide'][3] = 250;
$indexMaxLimits['sulphurdioxide'][4] = 350;
$indexMaxLimits['odorsulphurcompounds'][1] = 5;
$indexMaxLimits['odorsulphurcompounds'][2] = 10;
$indexMaxLimits['odorsulphurcompounds'][3] = 20;
$indexMaxLimits['odorsulphurcompounds'][4] = 50;
$data = $this->result['latest']['data'];
$measurement = $this->result['metadata']['measurement'];
if ("qualityIndex" == $measurement)
{
// TODO: Move index calculations here
}
else
{
if ($data > $indexMaxLimits[$measurement][4])
{
$index = 5;
$FI = "erittäin huono";
$EN = "very bad";
}
elseif ($data > $indexMaxLimits[$measurement][3])
{
$index = 4;
$FI = "huono";
$EN = "bad";
}
elseif ($data > $indexMaxLimits[$measurement][2])
{
$index = 3;
$FI = "välttävä";
$EN = "mediocre";
}
elseif ($data > $indexMaxLimits[$measurement][1])
{
$index = 2;
$FI = "tyydyttävä";
$EN = "satisfactory";
}
else
{
$index = 1;
$FI = "hyvä";
$EN = "good";
}
}
$this->result['latest']['index'] = $index;
$this->result['latest']['FI'] = $FI;
$this->result['latest']['EN'] = $EN;
}
// ------------------------------------------------------------------------
//
public function returnResultArray()
{
$this->result['error'] = FALSE;
return $this->result;
}
// ------------------------------------------------------------------------
// Debug: prints array as JSON and exits
public function debugThisArray($array)
{
$array['DEBUG'] = "------------------------------ DEBUG MODE ON ------------------------------";
header('Content-Type: application/json; charset=utf-8');
echo json_encode($array);
exit();
}
}
?>