File size: 2,831 Bytes
7260f53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
"""
build_testbed.py — assemble a candidate episode–gauge–watershed testbed package
from public services, in the released package layout:

    <outdir>/<EPISODE>/<GAUGE>/
        watershed.geojson     (NLDI contributing watershed)
        streamflow.csv        (NWIS IV discharge, begin-24 h .. end+72 h)
        testbed_summary.json  (window, gauge, provenance)

Forcing can then be added with download_forcing.py. A package built this way is
a CANDIDATE: it becomes comparable to the released benchmark testbeds only after
the screening and quality-control criteria of the paper (Sect. 3.4) are applied
— use code/pipeline for that.

Example
-------
  python build_testbed.py --episode FF_2025_07_TX_ep002 --gauge 08165500 \
      --outdir ./my_testbeds
"""
import argparse
import json
from datetime import timedelta
from pathlib import Path

from torrent_tools import episode_lookup, fetch_basin, fetch_iv, parse_utc

HERE = Path(__file__).resolve().parent
DATA = HERE.parent.parent / "data"


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--episode", required=True)
    ap.add_argument("--gauge", required=True)
    ap.add_argument("--begin", help="override episode begin (UTC)")
    ap.add_argument("--end", help="override episode end (UTC)")
    ap.add_argument("--outdir", default="./testbeds")
    args = ap.parse_args()

    gauge = args.gauge.zfill(8)
    if args.begin and args.end:
        b_ep, e_ep = args.begin, args.end
    else:
        ep = episode_lookup(args.episode, DATA)
        b_ep, e_ep = ep["begin"], ep["end"]
    b = parse_utc(b_ep)
    e = parse_utc(e_ep)

    out = Path(args.outdir) / args.episode / gauge
    out.mkdir(parents=True, exist_ok=True)

    print(f"[1/3] NLDI watershed for {gauge} ...")
    gj = fetch_basin(gauge)
    with open(out / "watershed.geojson", "w") as f:
        json.dump(gj, f)

    print(f"[2/3] NWIS IV discharge {b - timedelta(hours=24)} -> "
          f"{e + timedelta(hours=72)} ...")
    q = fetch_iv(gauge, b - timedelta(hours=24), e + timedelta(hours=72), "00060")
    if q.empty:
        print("  WARNING: no discharge data returned")
    q.to_csv(out / "streamflow.csv", index=False)

    print("[3/3] summary ...")
    with open(out / "testbed_summary.json", "w") as f:
        json.dump({
            "ff_episode_id": args.episode, "gage_id": gauge,
            "begin_dt": str(b), "end_dt": str(e),
            "n_discharge_samples": int(len(q)),
            "package_class": "user-built candidate (unscreened)",
            "built_with": "code/tools/build_testbed.py",
        }, f, indent=2)
    print(f"package at {out}")
    print("next: add forcing with download_forcing.py --watershed "
          f"{out/'watershed.geojson'} --begin '{b_ep}' --end '{e_ep}'")


if __name__ == "__main__":
    main()