name: dds-bangkok-rain description: Fetch historical rainfall data from flood.bangkok.go.th's Drainage Information System API, covering ~132 rain gauge stations across Bangkok. Use this whenever the user asks to download, pull, or update Bangkok rainfall / rain gauge / ฝน กทม. data, mentions flood.bangkok.go.th, or wants historical rain data for one or more Bangkok stations over a date range -- even if they just say "get me the rain data" without naming the site. Also covers reconstructing finer time resolution from the API's nested accumulation windows. Not for water level / canal level data (see dds-bangkok-water-level).


Bangkok rainfall data (flood.bangkok.go.th)

Adapted from gain9999/thaiwater.

Fetches historical rainfall readings from flood.bangkok.go.th's API (the Drainage Information System), covering roughly 132 rain gauge stations across Bangkok (station codes look like RF.KTY.01).

This is a genuine JSON API (unlike the water-level Telemetry site -- see the sibling dds-bangkok-water-level skill, which has to scrape HTML instead), but it has two rough edges worth knowing about before you touch it: there's no endpoint that just lists every station, and the API itself is intermittently flaky.

Quick start

python scripts/fetch_rain.py --start 2026-09-23 --end 2026-09-29 --out rain.csv

That's every station at raw 30-minute resolution (one row per station per 30 minutes, value = rainfall in that 30-minute window). To scope to specific stations, first list them:

python scripts/fetch_rain.py --list-stations > stations.json

then pass their codes:

python scripts/fetch_rain.py --start 2026-09-23 --end 2026-09-29 \
  --stations RF.KTY.01,RF.BBN.01 --out rain.csv

Add --resolution derived to get finer time buckets instead of flat 30-minute rows -- see "Getting better than 30-minute resolution" below, it's a real technique this API supports, not just an interpolation guess.

Output (raw30, the default): st_code, name_th, name_en, site_time, rainfall_30min_mm. Output (derived): st_code, name_th, name_en, interval_start, interval_end, resolution_min, rainfall_mm -- more rows, mixed 5/15-minute buckets, resolution_min tells you which.

How this actually works

There's no "list all rain stations" endpoint

flood.bangkok.go.th has a per-station-type "list" convention for most things (district/all, water/riverlist, ...) but rain stations are only enumerable by district: GET /api/rain/infoByDistrict/{district_id}. So the station list comes from fetching district/all (50 districts), hitting rain/infoByDistrict/{id} for each one, and de-duplicating the results by st_code -- a station can be returned under more than one district. fetch_all_rain_stations() in the script does this already; don't try to find a shortcut endpoint, there isn't one.

The API is flaky -- always retry on the response shape, not the status code

Every endpoint on this API, not just the rain ones, will intermittently return its own frontend's index.html instead of the JSON you asked for, on the exact same URL with the exact same request -- HTTP 200 both times. It looks like a reverse proxy occasionally routing to the wrong backend. The fix is boringly simple: treat any response that doesn't start with [ as a miss and retry (a handful of attempts with a short sleep is enough -- see api_get() in the script). Route every API call through that helper rather than calling requests/curl directly, or you'll spend a long time debugging what looks like missing data but is actually just this.

Getting better than 30-minute resolution

The raw rows come back every 30 minutes no matter what period query param you pass -- that parameter does not change the sampling rate. But each row already reports several trailing accumulation windows ending at that same timestamp: rf5min, rf10min, rf15min, rf30min (plus longer ones out to rf24hr, not used here). These are nested and monotonic (rf5min <= rf10min <= rf15min <= rf30min -- checked against real rain events, not assumed), which means you can subtract consecutive ones to recover the rainfall in each actual sub-window:

Sub-window (minutes before T) How to get it
0–5 rf5min
5–10 rf10min − rf5min
10–15 rf15min − rf10min
15–30 rf30min − rf15min (one 15-min lump -- no field exists to split this half further)

So the second half of every 30-minute row resolves to true 5-minute bins, and the first half stays one 15-minute bin. --resolution derived does exactly this (see derive_resolution() in the script). This is exact subtraction of numbers the API actually reported, not an estimate -- but it's genuinely mixed-resolution (5 min for some sub-windows, 15 for others), so if the user needs uniform 5-minute data across the whole timeline, tell them that's not something this API supports and --resolution derived is the closest honest approximation, not the real thing.

Timing

This API is fast (a few seconds per station, not the 20-40s of the water-level Telemetry site), so the default 10 workers comfortably fetches all ~132 stations for a week in well under a minute. The bottleneck here is the retry loop on flaky responses, not raw request time.