Public
Script
October 23, 2023
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
Nobody has commented on this val yet: be the first!
October 23, 2023