import type { AqiStandard } from './types' type Bp = [cLo: number, cHi: number, iLo: number, iHi: number] // HJ 633-2012 (China), 24h breakpoints, μg/m³. const CN_PM25: Bp[] = [[0, 35, 0, 50], [35, 75, 50, 100], [75, 115, 100, 150], [115, 150, 150, 200], [150, 250, 200, 300], [250, 350, 300, 400], [350, 500, 400, 500]] const CN_PM10: Bp[] = [[0, 50, 0, 50], [50, 150, 50, 100], [150, 250, 100, 150], [250, 350, 150, 200], [350, 420, 200, 300], [420, 500, 300, 400], [500, 600, 400, 500]] // US EPA PM2.5 breakpoints as used by aqicn.org (pre-2024 table). const US_PM25: Bp[] = [[0, 12, 0, 50], [12.1, 35.4, 51, 100], [35.5, 55.4, 101, 150], [55.5, 150.4, 151, 200], [150.5, 250.4, 201, 300], [250.5, 350.4, 301, 400], [350.5, 500.4, 401, 500]] function iaqi(c: number, table: Bp[]): number { for (const [cLo, cHi, iLo, iHi] of table) if (c <= cHi) return Math.round(((iHi - iLo) / (cHi - cLo)) * (Math.max(c, cLo) - cLo) + iLo) return 500 } function concFromIndex(i: number, table: Bp[]): number { for (const [cLo, cHi, iLo, iHi] of table) if (i <= iHi) return ((cHi - cLo) / (iHi - iLo)) * (Math.max(i, iLo) - iLo) + cLo return table[table.length - 1][1] } /** China AQI from PM2.5/PM10 concentrations (the two that dominate in practice). */ export function cnAqiFromConc(pm25: number | null, pm10: number | null): number | null { const parts = [pm25 != null ? iaqi(pm25, CN_PM25) : null, pm10 != null ? iaqi(pm10, CN_PM10) : null].filter((x): x is number => x != null) return parts.length ? Math.max(...parts) : null } export function levelFor(aqi: number | null, standard: AqiStandard): string | null { if (aqi == null) return null if (standard === 'aqi_us') { if (aqi <= 50) return '优' if (aqi <= 100) return '中等' if (aqi <= 150) return '对敏感人群不健康' if (aqi <= 200) return '不健康' if (aqi <= 300) return '非常不健康' return '危险' } if (aqi <= 50) return '优' if (aqi <= 100) return '良' if (aqi <= 150) return '轻度污染' if (aqi <= 200) return '中度污染' if (aqi <= 300) return '重度污染' return '严重污染' } /** Rough mainland-China box. Model AQI (CAMS) is unreliable there, so we never present it as a reading. */ export function inChina(lat?: number, lon?: number, location?: string): boolean { if (typeof lat === 'number' && typeof lon === 'number') return lat > 18 && lat < 54 && lon > 73 && lon < 135 return /[\u4e00-\u9fff]/.test(location ?? '') } export type AqicnReading = { aqi: number; standard: AqiStandard; level: string | null; dominant: string | null; updateTime: string | null; url: string; pm25Estimate: number | null; note: string } /** * Real station reading from aqicn.org's public city page (no signup). aqicn reports US-EPA AQI. * For `aqi_cn`, convert only when PM2.5 dominates (then the US index maps cleanly back to a concentration); * otherwise return the US value and say so via `standard: 'aqi_us'`. */ export async function aqicnReading(cityEn: string, standard: AqiStandard): Promise { const slug = cityEn.trim().toLowerCase().replace(/\s+/g, '-') const url = `https://aqicn.org/city/${encodeURIComponent(slug)}/` const { fetchRetry } = await import('./scrape') const res = await fetchRetry(url, { headers: { 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124 Safari/537.36', accept: 'text/html' } }) if (!res.ok) throw new Error(`aqicn.org HTTP ${res.status}`) const html = await res.text() const m = html.match(/"aqi":(\d+),"time":\{"v":"[^"]*","utc":\{"v":\d+,"tz":"[^"]*","s":"([^"]+)"/) if (!m) throw new Error('aqicn.org: no AQI on page') const us = Number(m[1]) const dominant = html.match(/"dominentpol":"([a-z0-9]+)"/)?.[1] ?? null if (standard === 'aqi_cn' && dominant === 'pm25') { const pm25 = Math.round(concFromIndex(us, US_PM25) * 10) / 10 const cn = cnAqiFromConc(pm25, null) ?? us return { aqi: cn, standard: 'aqi_cn', level: levelFor(cn, 'aqi_cn'), dominant, updateTime: m[2], url, pm25Estimate: pm25, note: `aqicn.org station US AQI ${us} (PM2.5) converted to China AQI` } } return { aqi: us, standard: 'aqi_us', level: levelFor(us, 'aqi_us'), dominant, updateTime: m[2], url, pm25Estimate: null, note: `aqicn.org station reading, US AQI ${us} (dominant ${dominant ?? 'unknown'}); not converted to ${standard}` } }