| |
| """ |
| download_streamflow.py — fetch the NWIS instantaneous discharge record for a |
| gauge over an event window (same convention as the released packages: |
| 24 h before begin to 72 h after end) and write streamflow.csv |
| (columns: datetime_utc, value [cfs]). |
| |
| Example |
| ------- |
| python download_streamflow.py --gauge 08165500 \ |
| --begin 2025-07-04T00:00 --end 2025-07-05T00:00 --out streamflow.csv |
| """ |
| import argparse |
| from datetime import timedelta |
|
|
| from torrent_tools import fetch_iv, parse_utc |
|
|
|
|
| def main(): |
| ap = argparse.ArgumentParser() |
| ap.add_argument("--gauge", required=True) |
| ap.add_argument("--begin", required=True, help="episode begin (UTC)") |
| ap.add_argument("--end", required=True, help="episode end (UTC)") |
| ap.add_argument("--pre-hours", type=float, default=24.0) |
| ap.add_argument("--post-hours", type=float, default=72.0) |
| ap.add_argument("--parameter", default="00060", help="00060 discharge / 00065 stage") |
| ap.add_argument("--out", default="streamflow.csv") |
| args = ap.parse_args() |
|
|
| b = parse_utc(args.begin) - timedelta(hours=args.pre_hours) |
| e = parse_utc(args.end) + timedelta(hours=args.post_hours) |
| df = fetch_iv(args.gauge.zfill(8), b, e, args.parameter) |
| if df.empty: |
| raise SystemExit(f"no {args.parameter} IV data for {args.gauge} in window") |
| df.to_csv(args.out, index=False) |
| print(f"{args.gauge}: {len(df)} samples " |
| f"{df.datetime_utc.iloc[0]} -> {df.datetime_utc.iloc[-1]}") |
| print(f"wrote {args.out}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|