Update air quality

From atanasster/electionsbg · View interactive version

Update air quality is an AI agent skill from the GitHub repository atanasster/electionsbg. Refresh the air-quality monitoring-station data (data/air/index.json) by fetching the latest quarterly PM10 + PM2.5 CSVs from ИАОС via data.egov.bg. Use when the daily watch report flags `iaos_air_quality` as changed, when the user asks to refresh air quality / atmospheric monitoring / ИАОС data, wh

How to use this skill

Skill file

Update air quality skill

<!-- pipeline-pointer:start (generated from the registry; edit scripts/pipeline/registry) -->

Pipeline: handler CODE0 runs this unattended — pnpm run pipeline:ingest --only update-air-quality. The registry holds its commands, gates and publish set; this runbook covers what a script cannot do (operator steps, judgement calls, troubleshooting). When a step fails, start from the repair packet the run writes, not from the procedure below.

<!-- pipeline-pointer:end -->

Refreshes data/air/index.json — the per-município air-quality monitoring-station index that powers the My-Area "Качество на въздуха" tile. Covers ~37 município-attributed stations + ~14 background/mountain stations (Витиня, Копитото, Рожен — КФС, Гара Яна) from ИАОС's quarterly publication on data.egov.bg.

Source contract:

| Field | Source | Granularity | Cadence |

|---|---|---|---|

| latestReadings.pm10, maxObserved.pm10 | data.egov.bg resource 452acfd4 | station | quarterly |

| latestReadings.pm25, maxObserved.pm25 | data.egov.bg resource 0eefa354 | station | quarterly |

The CSVs do not carry station coordinates. Stations are identified by name only (София - Хиподрума, Пловдив - Каменица, В.Търново - РИОСВ, …); scripts/air/build_index.ts parses <município> - <subname> and looks up the município name in data/municipalities.json, with a small alias map for abbreviations (В.Търново → Велико Търново, Г.Оряховица → Горна Оряховица, …). Sofia citywide stations key under SOF00 and fan out to the 24 районы via the useAirQuality hook fallback.

When to run

| Trigger | Action |

|---|---|

| Daily watcher reports ИАОС air quality (data.egov.bg) as changed | Step 1 |

| User asks to "refresh air quality" / "update ИАОС stations" / "new air-quality quarter" | Step 1 |

| Adding NO₂ / O₃ / SO₂ / CO to the per-station record | Step 0 then Step 1 |

| Fresh clone with no data/air/index.json or zero stations | Step 1 |

ИАОС publishes once per quarter. A weekly check window catches in-quarter corrections (which do happen).

Step 0 (optional) — Add a new pollutant

ИАОС exposes NO₂, CO, O₃, SO₂ datasets on the same portal but the build script today reads only PM10 + PM2.5. To extend:

  1. Open the dataset page for the pollutant in the ИАОС data.egov.bg organisation listing (e.g. data.egov.bg/data?org[0]=76).
  2. Click the latest quarterly resource; copy its UUID from the URL data/resourceView/<uuid>.
  3. Append the entry to POLLUTANTS at the top of scripts/air/build_index.ts:

```ts

const POLLUTANTS = {

pm10: "452acfd4-9fa1-4ab8-9213-f1b2736ce143",

pm25: "0eefa354-495f-4a2d-a40f-846e11dd396a",

no2: "<new-uuid>",

} as const;

```

  1. Add the matching key to the Pollutant type and the pollutants block in data/air/index.json's output (the script already passes these through).

The watcher only tracks PM10 — all ИАОС pollutants publish on the same cadence, so adding NO₂ resources here doesn't need a new watch source.

Step 1 — Build

pnpm exec tsx scripts/air/build_index.ts          # uses cached CSVs when present

Force a fresh fetch (skip the disk cache):

rm -rf raw_data/air/iaos
pnpm exec tsx scripts/air/build_index.ts

Expected output:

pm10: 47 stations
pm25: 10 stations
Wrote .../data/air/index.json — 37 município stations, 14 background stations, asOf 2026-03-31

Step 2 — Spot-check

python3 -c "
import json
d = json.load(open('data/air/index.json'))
print('snapshotAsOf:', d['snapshotAsOf'])
print('stations:', len(d['stations']))
print('background:', len(d['backgroundStations']))
# Sample a Sofia + a non-Sofia station
for s in d['stations']:
    if s['name'].startswith('София'):
        print(f'  {s[\"name\"]} → {s[\"obshtina\"]} · PM10={s[\"latestReadings\"].get(\"pm10\")}')
        break
"

Sanity checks:

If the município-mapped count drops sharply, ИАОС likely renamed a station or added a new town not in the alias map — inspect the script's STATION_PREFIX_ALIASES and BACKGROUND set.

Step 3 — Commit + bucket sync

git add data/air/index.json raw_data/air/iaos/
git commit -m "air: refresh ИАОС stations through <snapshotAsOf>"
pnpm run bucket:sync:dry
pnpm run bucket:sync

Step 4 — Stamp success

pnpm exec tsx scripts/stamp-ingest.ts update-air-quality \
  --summary "37 município stations + 14 background, asOf <snapshotAsOf>"

Append to the public data-changes log only when readings actually changed:

if [ -n "$(git diff --stat data/air/)" ]; then
  pnpm exec tsx scripts/append-data-change.ts update-air-quality \
    --summary "37 município stations + 14 background, asOf <snapshotAsOf>" \
    --source "ИАОС air quality (data.egov.bg)"
fi

Known limitations

What this skill does NOT do

File map

| Path | Purpose |

|---|---|

| scripts/air/build_index.ts | CLI entry — fetch, parse, map → obshtina, write |

| raw_data/air/iaos/{pm10,pm25}.csv | cached source CSVs |

| data/air/index.json | output — stations[] + backgroundStations[] |

| scripts/watch/sources/iaos_air_quality.ts | watcher — fingerprints PM10 dataset resource UUIDs |

| src/data/air/useAirQuality.tsx | React Query hook with Sofia район → SOF00 fallback |

| src/screens/myarea/MyAreaAirTile.tsx | "Качество на въздуха" tile |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.