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
/*
US EPA Particulate Matter NowCast AQI (Air Quality Index) Algorithm
The US EPA generally uses 24 hours periods of data to calculate AQI.
To calculate AQI at an hourly scale the NowCast algorithm is a more appropriate choice
Algorithm explained more fully at: https://observablehq.com/@openaq/epa-pm-nowcast
inputs an array of measurements values of particulate matter
returns an NowCast air quality index number between 0-500
0 to 50 Good
51 to 100 Moderate
101 to 150 Unhealthy for Sensitive Groups
151 to 200 Unhealthy
201 to 300 Very Unhealthy
301 to 500 Hazardous
*/
export let nowcastPMAqi = (values: number[]): number => {
const min = Math.min(...values);
const max = Math.max(...values);
const range = max - min;
const scaledRateOfChange = range / max;
const weight = 1 - scaledRateOfChange < 0.5 ? 0.5 : 1 - scaledRateOfChange;
const weightedValues = values.map((o, i) => o * Math.pow(weight, i));
const weightedValuesSum = weightedValues.reduce((a, b) => a + b);
const weightFactorSum = values
.map((o, i) => Math.pow(weight, i))
.reduce((a, b) => a + b);
return parseFloat((weightedValuesSum / weightFactorSum).toFixed(1));
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
2
midnightlightning avatar

I believe this function stops one step short of returning the actual "AQI" value. The last step of this function is to calculate the weightedValuesSum / weightFactorSum which in the referenced Observable notebook is the nowCastConcentration value (a weighted average pm25 concentration value).

To turn it into an "AQI" number, there's one more step needed: mapping that to bands of what levels of concentration are safe or not (the concentrationToAqi function in the Observable notebook). Which this function seems to be missing?

Also note, the concentrationBreakpoints used in the notebook [0,12.0,35.4,150,200,300,500] are from a previous standard. In May 2024, the guidelines were updated and should now be [0,9.0,35.4,55.4,125.4,225.4]

russbiggs avatar

You are absolutely right. I have forgotten to add the final step of converting from the concentration to the actual AQI, even though i based its off the notebook... Ill update accordingly and with the updated EPA breakpoints

October 23, 2023