| #!/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() | |