Avatar

russbiggs

4 public vals
Joined March 2, 2023
1
2
3
4
5
import { nowcastPMAqi } from "https://esm.town/v/russbiggs/nowcastPMAqi";
export const exampleNowCastAQI = nowcastPMAqi([
1.8, 2, 2.4, 3.6, 4.2, 5.2, 6.2, 7.4, 6.7, 9.9, 5.9, 5.3, 4.2,
]);
1
2
3
export let foo = async (bar) => {
console.email(`This is ${bar}`);
};
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
import { nowcastPMAqi } from "https://esm.town/v/russbiggs/nowcastPMAqi";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export let emailNowcastPMAqi = async () => {
const dateFrom = new Date(Date.now() - 12 * 60 * 60 * 1000); // get previous 12 hours of data
const dateTo = new Date();
let res = await fetchJSON(
"https://api.openaq.org/v2/measurements?" +
new URLSearchParams({
limit: "100",
page: "1",
location_id: "827", //
date_from: dateFrom.toISOString(),
date_to: dateTo.toISOString(),
parameter: "pm25",
order_by: "datetime",
})
);
const values = res.results.map((o) => o.value);
const aqi = await nowcastPMAqi(values);
console.email(
`The AQI at ${res.results[0].location} is ${aqi}`,
`The AQI report`
);
};
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));
};
Next