1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { sum } from "https://esm.town/v/stevekrouse/sum";
export const 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 = sum(weightedValues);
const weightFactorSum = sum(
values.map((o, i) => Math.pow(weight, i)),
);
return parseFloat((weightedValuesSum / weightFactorSum).toFixed(1));
};
// Forked from @russbiggs.nowcastPMAqi
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
3
midnightlightning avatar

This seems to not follow the current calculations of how "AQI" is calculated? According to the document at https://www.airnow.gov/sites/default/files/2020-05/aqi-technical-assistance-document-sept2018.pdf, when looking at pm2.5 pollution, there's certain μg/m3 ranges that get mapped to the AQI color band ranges, and then the value is interpolated within that band.

stevekrouse avatar

@russbiggs who runs OpenAQ wrote this code and explains why he used the NowCast algorithm here even though the EPA uses a different one: https://www.val.town/v/russbiggs/nowcastPMAqi#L5-8

Maybe comment on his val if you have questions?

midnightlightning avatar

Commented here. Digging into it deeper, that function seems to not be returning an "AQI" value, but rather "a weighted concentration value of pm25 pollution" (effectively "how much junk is in the air", not "how healthy is that concentration to humans")

October 23, 2023