flash-flood-benchmark-data / tools /get_watershed.py
skyan1002's picture
v1.0 paper-matched: 806 L3 / 21 primary, agent flags, master_metadata, AGENTS.md + tool schema, annual L3-expansion pipeline + tools
7260f53 verified
Raw
History Blame Contribute Delete
821 Bytes
#!/usr/bin/env python3
"""
get_watershed.py — retrieve the NLDI upstream contributing watershed for a USGS
gauge and write watershed.geojson. Retries transient NLDI failures (the service
throttles at roughly 800 requests/hour; most failures are transient).
Example
-------
python get_watershed.py --gauge 08165500 --out watershed.geojson
"""
import argparse
import json
from torrent_tools import fetch_basin
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--gauge", required=True)
ap.add_argument("--out", default="watershed.geojson")
args = ap.parse_args()
gj = fetch_basin(args.gauge.zfill(8))
with open(args.out, "w") as f:
json.dump(gj, f)
n = len(gj.get("features", []))
print(f"wrote {args.out} ({n} feature(s))")
if __name__ == "__main__":
main()