-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaqi.h
executable file
·367 lines (338 loc) · 7.28 KB
/
aqi.h
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**
* @file aqi.h
* @author Aaron Solochek <[email protected]>
* @brief Functions for computing AQI from pollution concentrations based on
* https://www.airnow.gov/sites/default/files/2020-05/aqi-technical-assistance-document-sept2018.pdf
* @version 0.1
* @date 2023-08-26
*
* @copyright Copyright (c) 2023
*
*/
#include <math.h>
#define BREAK_COUNT 7 // All tables need to have the same size
/**
* @brief Breakpoint structure for AQI tables
*
*/
typedef struct
{
float lo;
float hi;
} breakpoint;
/**
* @brief AQI breakpoints
*
*/
const breakpoint aqi_breaks[] =
{
{0, 50},
{51, 100},
{101, 150},
{151, 200},
{201, 300},
{301, 400},
{401, 500}
};
/**
* @brief PM₂․₅, µg/m³ 24-hour
*
*/
const breakpoint pm2_5_breaks[] =
{
{0, 12},
{12.1, 35.4},
{35.5, 55.4},
{55.5, 150.4},
{150.5, 250.4},
{250.5, 350.4},
{350.5, 500.4}
};
/**
* @brief PM₁₀, µg/m³ 24-hour
*
*/
const breakpoint pm10_breaks[] =
{
{0, 54},
{55, 154},
{155, 254},
{255, 354},
{355, 424},
{425, 504},
{505, 604}
};
/**
* @brief O₃, ppm 8-hour
*
*/
const breakpoint o3_8h_breaks[] =
{
{0.000, 0.054},
{0.055, 0.070},
{0.071, 0.085},
{0.086, 0.105},
{0.106, 0.200},
{0.405, 0.504}, // undefined in spec
{0.505, 0.604} // undefined in spec
};
/**
* @brief O₃, ppm 1-hour
*
*/
const breakpoint o3_1h_breaks[] =
{
{0.000, 0.054}, // undefined in spec, using 8h value
{0.055, 0.124}, // undefined in spec, using 8h values
{0.125, 0.164},
{0.165, 0.204},
{0.205, 0.404},
{0.405, 0.504},
{0.505, 0.604}
};
/**
* @brief CO, ppm 8-hour
*
*/
const breakpoint co_breaks[] =
{
{0.0, 4.4},
{4.5, 9.4},
{9.5, 12.4},
{12.5, 15.4},
{15.5, 30.4},
{30.5, 40.4},
{40.5, 50.4}
};
/**
* @brief SO₂ ppb 1-hour
*
*/
const breakpoint so2_breaks[] =
{
{0, 35},
{36, 75},
{76, 185},
{186, 304},
{305, 604},
{605, 804},
{805, 1004}
};
/**
* @brief NO₂, ppb 1-hour
*
*/
const breakpoint no2_breaks[] =
{
{0, 53},
{54,100},
{101, 360},
{361, 649},
{650, 1249},
{1250, 1649},
{1650, 2049}
};
/**
* @brief Find the index of the breakpoint less than or equal to the concentration
*
* @param val The truncated concentration of the pollutant
* @param map The breakpoint table corresponding to the pollutant
* @return Index into the breakpoint table of the low concentration
*/
int low_index(float val, const breakpoint map[])
{
int i = BREAK_COUNT;
while(--i >= 0)
{
if(map[i].lo <= val)
break;
}
return i;
}
/**
* @brief Find the index of the breakpoint greater than or equal to the concentration
*
* @param val The truncated concentration of the pollutant
* @param map The breakpoint table corresponding to the pollutant
* @return Index into the breakpoint table of the high concentration
*/
int high_index(float val, const breakpoint map[])
{
int i = 0;
for(i = 0; i < BREAK_COUNT; i++)
{
if(map[i].hi >= val)
break;
}
return i;
}
/**
* @brief Truncate a float value to 1 decimal place
*
* @param val The value to truncate
* @return The truncated value
*/
float trunc1dp(float val)
{
val *= 10.0;
return truncf(val) / 10.0;
}
/**
* @brief Truncate a float value to 3 decimal places
*
* @param val The value to truncate
* @return The truncated value
*/
float trunc3dp(float val)
{
val *= 1000.0;
return truncf(val) / 1000.0;
}
/**
* @brief Calculate the AQI for a given concentration and breakpoint table
*
* @param val Concentration of pollutant
* @param map Breakpoint table
* @return Calculated AQI
*/
int calculate_aqi(float val, const breakpoint map[])
{
int high_idx = high_index(val, map);
int low_idx = low_index(val, map);
float conc_hi = map[high_idx].hi;
float conc_lo = map[low_idx].lo;
int aqi_hi = aqi_breaks[high_idx].hi;
int aqi_lo = aqi_breaks[low_idx].lo;
return round(((float)aqi_hi - (float)aqi_lo) / (conc_hi - conc_lo) * (val - conc_lo) + aqi_lo);
}
/**
* @brief Get the PM₂․₅ AQI value
*
* @param raw Pollutant concentration in µg/m³
* @return AQI
*/
int get_pm2_5_aqi(float raw)
{
float val = trunc1dp(raw);
return calculate_aqi(val, pm2_5_breaks);
}
/**
* @brief Get the PM₁₀ AQI value
*
* @param raw Pollutant concentration in µg/m³
* @return AQI
*/
int get_pm10_aqi(float raw)
{
float val = truncf(raw);
return calculate_aqi(val, pm10_breaks);
}
/**
* @brief Get the 8h ozone AQI -- This is the generally required one
*
* @param raw Pollutant concentration in ppm
* @return AQI
*/
int get_8h_ozone_aqi(float raw)
{
float val = trunc3dp(raw);
if(val > 0.2)
return calculate_aqi(val, o3_1h_breaks);
else
return calculate_aqi(val, o3_8h_breaks);
}
/**
* @brief Get the 1h ozone AQI
*
* @param raw Pollutant concentration in ppm
* @return AQI
*/
int get_1h_ozone_aqi(float raw)
{
float val = trunc3dp(raw);
// Technically the 1h Ozone AQI isn't defined for concentrations under .125
//return val < 0.125 ? 0 : calculate_aqi(val, o3_1h_breaks);
//but I compute it anyway
return calculate_aqi(val, o3_1h_breaks);
}
/**
* @brief Get the ozone AQI
*
* @param raw_8h Pollutant concentration in ppm averaged over 8 hours
* @param raw_1h Pollutant concentration in ppm averaged over 1 hour
* @return AQI
*/
int get_ozone_aqi(float raw_8h, float raw_1h)
{
int aqi_8h = get_8h_ozone_aqi(raw_8h);
int aqi_1h = get_1h_ozone_aqi(raw_1h);
return aqi_1h > aqi_8h ? aqi_1h : aqi_8h;
}
/**
* @brief Get the CO AQI
*
* @param raw Pollutant concentration in ppm
* @return AQI
*/
int get_co_aqi(float raw)
{
float val = trunc1dp(raw);
return calculate_aqi(val, co_breaks);
}
/**
* @brief Get the SO₂ AQI
*
* @param raw Pollutant concentration in ppb
* @return AQI
*/
int get_so2_aqi(float raw)
{
float val = truncf(raw);
return calculate_aqi(val, so2_breaks);
}
/**
* @brief Get the NO₂ aqi object
*
* @param raw Pollutant concentration in ppb
* @return AQI
*/
int get_no2_aqi(float raw)
{
float val = truncf(raw);
return calculate_aqi(val, no2_breaks);
}
/**
* @brief Get the overall AQI given all pollutants. Enter 0 for unused sensors
*
* @param raw_pm25 PM₂․₅ concentration in µg/m³
* @param raw_pm10 PM₁₀ concentration in µg/m³
* @param raw_o3_1h O₃ concentration in ppm over 1 hour
* @param raw_o3_8h O₃ concentration in ppm over 8 hour
* @param raw_co CO concentration in ppm
* @param raw_so2 SO₂ concentration in ppb
* @param raw_no2 NO₂ concentration in ppb
* @return Final AQI
*/
int get_total_aqi(float raw_pm25,
float raw_pm10,
float raw_o3_1h,
float raw_o3_8h,
float raw_co,
float raw_so2,
float raw_no2)
{
int aqis[6] = {0};
aqis[0] = get_pm2_5_aqi(raw_pm25);
aqis[1] = get_pm10_aqi(raw_pm10);
aqis[2] = get_ozone_aqi(raw_o3_8h, raw_o3_1h);
aqis[3] = get_co_aqi(raw_co);
aqis[4] = get_so2_aqi(raw_so2);
aqis[5] = get_no2_aqi(raw_no2);
int max = 0;
for(int i=0; i < 6; i++)
{
if(aqis[i] > max)
max = aqis[i];
}
return max;
}