name: dds-bangkok-water-level description: Fetch historical water level time series (canal/river monitoring stations) from weather.bangkok.go.th, the Bangkok Drainage and Sewerage Department's telemetry system covering ~312 stations at 5-minute resolution. Use this whenever the user asks to download, pull, scrape, or update Bangkok water level / canal level / flood monitoring data, mentions weather.bangkok.go.th or "ระดับน้ำ กทม.", or wants historical water-level data for one or more Bangkok stations over a date range -- even if they just say "get me the water level data" without naming the site. Also covers de-noising the result (single-sample sensor glitches). Not for rainfall data (see dds-bangkok-rain) or for the smaller/older flood.bangkok.go.th API.


Bangkok water level data (weather.bangkok.go.th)

Adapted from gain9999/thaiwater.

Fetches historical water level readings for Bangkok's canal/river monitoring network from weather.bangkok.go.th, which is the Telemetry system run by the Bangkok Metropolitan Administration's Drainage and Sewerage Department. It covers roughly 312 stations, most updated within the last day, at true 5-minute resolution.

This is a different, larger, and more current system than flood.bangkok.go.th's public JSON API (~34 stations, station codes like WL.BCN.01, often stale by weeks). If the user wants broad citywide coverage or recent data, this skill's system is almost always the right one. If they specifically ask for flood.bangkok.go.th, or want the documented 30-min-resolution JSON API, that's a separate, simpler task -- just call its endpoints directly (GET https://flood.bangkok.go.th/api/water/history?period=30&st_code=<CODE>&stdate=<ISO>&enddate=<ISO>) rather than using this skill.

Quick start

python scripts/fetch_water_level.py --start 2026-09-23 --end 2026-09-29 --out water_level.csv

That fetches every station. To scope to specific stations (faster, useful for testing), first list them:

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

then pass their water_ids:

python scripts/fetch_water_level.py --start 2026-09-23 --end 2026-09-29 \
  --stations 1,74,119 --out water_level.csv

Add --despike to null out single-sample sensor glitches (see below). Heavy stations that time out are retried automatically as 1-day requests; --timeout, --chunk-days and --fallback-timeout tune that (see "How this actually works"). Run python scripts/fetch_water_level.py --help for all flags.

Output is a long-format CSV: one row per station per timestamp, columns water_id, water_name, river_name, site_time, water_level_msl, warning_level, critical_level. water_level_msl is metres relative to mean sea level (ม.รทก.). warning_level/critical_level are that station's own alert thresholds, if the site publishes them for it.

For a full run (all ~312 stations, a week of data), expect roughly 10-40 minutes depending on --workers -- see "Timing and etiquette" below.

How this actually works (read before changing the script)

weather.bangkok.go.th has no documented API. /water/WaterHistory is an ordinary server-rendered page: you POST a station id + date range to it and the same URL re-renders with the result embedded in a <script> tag as a Highcharts config, in a page that's otherwise 1-3 MB of HTML. The script scrapes that. Two things about the form will burn you if you rebuild this by hand from the page source instead of using the script as-is:

  1. Only send the fields a real, unfiltered search would send. The form has rain_data and txtFilter inputs that exist in the HTML but are disabled whenever rain_field_selected=0 (the "no filter" default) -- and browsers never submit disabled fields. If you copy every input you see in the page source and POST all of them, the server applies a filter against a field that isn't meant to apply, and you get a perfectly-formed response with an empty series, no error. This was the single biggest time-sink in figuring this system out -- if a station you know has data comes back empty, this is almost certainly why.
  2. One request only covers up to about 3 days. Ask for a longer span and, again, you silently get an empty series back -- no error, no truncation warning. fetch_water_level.py chunks any longer range into <=3-day windows and merges them (de-duplicating on timestamp at the seams), via CHUNK_DAYS in the script.

A third thing looks like missing data but isn't: some stations are heavy and simply time out. Pump stations and sluice gates (names starting สถานีสูบน้ำ / ประตูระบายน้ำ) have pages so large that a 3-day request can take minutes, longer than a normal request timeout. The chunk then "fails", and a station whose every chunk failed is indistinguishable, in the output, from a station that has no data. In the first full pull of this project, 84 stations came back empty this way; re-fetching them one day at a time with a 5-7 minute timeout recovered 46 of them (a 1-day request answers in about 35 s). The script now does this itself: if a chunk fails, it retries it as 1-day pieces with --fallback-timeout (default 420 s) and keeps using 1-day pieces for the rest of that station. The remaining empty stations really were empty -- the site answered every day with an empty series.

If a station you expect is missing from the output, don't conclude it has no data: check the stderr log for chunk(s) failed, then retry just those ids with --chunk-days 1 --fallback-timeout 600.

--stations also accepts ids that are no longer in the site's station list. The site drops some ids from the list but they can still answer when asked directly (water_id 35 returned a complete week); the script fetches them with blank name/threshold columns and says so in the log.

The station list itself (all ~312 stations, current metadata, most recent reading) is embedded the same way on page load, as a const allData = [...] JS array -- fetch_station_list() scrapes that instead of walking the dropdown. No station on this system has latitude/longitude (the field exists in the data and is always null); if the user needs a map or coordinates, you'll need to source those separately and join on station name/river, since there's no shared ID with any other Bangkok GIS system.

Despiking

--despike runs a small Hampel-style filter: each point is compared to the median of its 3 nearest valid neighbours on each side, and nulled out if it's more than --despike-threshold metres (default 0.3) away from that local median. It's aimed at a specific, very recognizable failure mode in this data: a single 5-minute sample reads several metres off and then the very next sample lands right back where it was, e.g. 1.60, -2.00, 1.60. That shape is a telemetry dropout, not real water movement -- canal levels just don't move and un-move that fast. The 0.3 m default comes from looking at the actual distribution of 5-minute deltas across the whole station network: 99.5% of genuine changes are under ~0.19 m, then there's a clean gap before the glitch population starts above ~1 m, so 0.3 m sits safely in that gap without touching real signal.

What it deliberately does not do: some stations sit in or right next to a pump sump, and their level legitimately swings a metre or more for tens of minutes at a time as the pump cycles on and off. That's a real, sustained physical pattern, not a glitch, and this filter correctly leaves it alone (a local 7-point median can't distinguish a sustained plateau from real data). If the user asks about that pattern specifically -- a station that alternates between a smooth high level and a lower level for extended stretches -- that needs a different, longer-window detector, not this one; say so rather than trying to stretch --despike to cover it.

Timing and etiquette

Each 3-day chunk of an ordinary station takes roughly 20-40 seconds to fetch (heavy stations, see above, take several minutes per 3 days or about 35 s per single day) -- the government server is just slow, not rate-limiting you. --workers defaults to 8 concurrent requests, which has been tested up to 10 without issues, but resist the urge to push much higher just because it's a for loop you can parallelize infinitely -- it's still someone else's server. For a full 312-station x 1-week pull, budget on the order of 20-40 minutes; run it in the background and check in periodically rather than blocking on it.

If a station comes back with a failed chunk after retries, that's reported per-station in the script's stderr log (N chunk(s) failed) -- it's not necessarily a bug, some stations' sensors are simply offline for stretches of real time.