File size: 2,177 Bytes
6848dc2 | 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 | """Sun*+ 推論例 (ローカル/オフライン実行用)。
python infer_example.py --station 0658 --start 2026-10-01 --end 2026-10-03 # 観測なしダウンスケーリング (ARCO-ERA5 を自動取得)
python infer_example.py --station 0658 --start 2026-10-01 --end 2026-10-03 --nc era5_dir --static static.nc # 取得済み NetCDF を使う
予測対象時刻の前後24時間の余白を自動的に含めて取得します。出力は CSV (時刻 UTC)。"""
import argparse, pandas as pd, numpy as np
from sunstar_plus import SunstarPlus, era5io
ap = argparse.ArgumentParser()
ap.add_argument("--station", required=True, help="stations_nationwide.csv の block_no (例 0658=新見)。カンマ区切り可")
ap.add_argument("--start", required=True); ap.add_argument("--end", required=True)
ap.add_argument("--nc"); ap.add_argument("--static"); ap.add_argument("--out", default="sunstar_plus_pred.csv")
ap.add_argument("--known", action="store_true", help="学習に使った地点なら指定 (σ の未知地点拡大を外す)")
a = ap.parse_args()
st = pd.read_csv("data/stations_nationwide.csv", dtype={"block_no": str}); ids = a.station.split(",")
sel = st[st.block_no.isin(ids)].reset_index(drop=True); assert len(sel), "block_no が見つかりません"
t0, t1 = pd.Timestamp(a.start) - pd.Timedelta(hours=24), pd.Timestamp(a.end) + pd.Timedelta(hours=24)
if a.nc:
import glob; times, fields, grid = era5io.load_nc(glob.glob(a.nc + "/20??-??.nc"), a.static); keep = (times >= t0) & (times <= t1)
times = times[keep]; fields = {k: v[keep] for k, v in fields.items()}
else: times, fields, grid = era5io.fetch_arco(t0, t1)
m = SunstarPlus(".")
out = m.downscale(times, fields, grid, sel, sigma=True, heldout=not a.known)
rows = []
for j, r in sel.iterrows():
d = pd.DataFrame({"time_utc": times, "block_no": r.block_no})
for v, (mean, sg) in out.items():
d[v] = mean[:, j]
if sg is not None: d[v + "_sigma"] = sg[:, j]
rows.append(d)
res = pd.concat(rows); res = res[(res.time_utc >= pd.Timestamp(a.start)) & (res.time_utc <= pd.Timestamp(a.end))]
res.to_csv(a.out, index=False); print(res.head(), "->", a.out)
|