from pathlib import Path import json ROOT = Path(__file__).resolve().parent def read_json(path): return json.loads(Path(path).read_text(encoding='utf-8-sig')) def station_info(station, root=ROOT): mapping = read_json(Path(root) / 'input_schema.json')['station_mapping'] for code, info in mapping.items(): if station in (code, info['site']): return dict(info, station_code=code) raise ValueError(f'unknown station: {station}; use YUWEI, JMJD01 or ZHONGXIN') def parse_issue_time(value): import pandas as pd ts = pd.Timestamp(value) if pd.isna(ts) or ts.tzinfo is None: raise ValueError('issue_time requires an explicit timezone, e.g. +08:00') if ts != ts.floor('15min'): raise ValueError('issue_time must be on a 15-minute boundary') return ts.tz_convert('UTC') def validate_arrays(data): import numpy as np import pandas as pd for key in ('power_history', 'future_raw', 'elevation', 'issue_time_ns'): if key not in data: raise ValueError(f'missing input field: {key}') h, f, e, t = (np.asarray(data[k]) for k in ('power_history', 'future_raw', 'elevation', 'issue_time_ns')) n = len(h) if not n or h.shape != (n, 48, 7) or f.shape != (n, 16, 20) or e.shape != (n, 16) or t.shape != (n,): raise ValueError('input shapes must be [N,48,7], [N,16,20], [N,16], [N]; N > 0') if any(not np.isfinite(a).all() for a in (h, f, e)): raise ValueError('NaN/Inf in required input; skip this issue instead of filling future values') if not np.issubdtype(t.dtype, np.integer): raise ValueError('issue_time_ns must be integer UTC epoch nanoseconds') times = pd.to_datetime(t, unit='ns', utc=True) if times.isna().any() or times.duplicated().any() or not times.is_monotonic_increasing: raise ValueError('issue times must be valid, unique and increasing') if (times != times.floor('15min')).any(): raise ValueError('issue times must be on 15-minute boundaries') if not np.allclose(e, f[:, :, 13], rtol=0, atol=1e-4): raise ValueError('elevation differs from future_raw column 13') return data def write_json(value, path): path = Path(path) path.parent.mkdir(parents=True, exist_ok=True) path.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding='utf-8')