| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- """One CSV-based forecast, used by run.sh for integration debugging."""
- from pathlib import Path
- import json
- import os
- import sys
- import tempfile
- import pandas as pd
- from ultra_short_pv import PVForecaster
- from pv_io import ROOT, read_json, write_json
- from pv_inputs import prepare_from_csv
- def run(config_path):
- config_path = Path(config_path).resolve()
- cfg = read_json(config_path)
- def path(key):
- p = Path(cfg[key]).expanduser()
- return p if p.is_absolute() else config_path.parent / p
- code, issue = cfg['station_code'], cfg['issue_time']
- data, audit = prepare_from_csv(code, issue, path('power_csv'), path('ec_dir'),
- receipts=read_json(path('ec_receipts')))
- model = PVForecaster(code)
- result = model.predict(issue, data['power_history'][0], data['future_raw'][0])
- rows = [dict(station_code=result['station_code'], issue_time=result['issue_time'],
- capacity_kw=result['capacity_kw'], **point) for point in result['forecasts']]
- output = path('output_dir')
- output.mkdir(parents=True, exist_ok=True)
- name = result['station_code'] + '_' + pd.Timestamp(result['issue_time']).strftime('%Y%m%d%H%M')
- target = output / f'{name}.csv'
- write_json(audit, output / f'{name}.audit.json')
- fd, temp = tempfile.mkstemp(dir=output, suffix='.tmp')
- os.close(fd)
- try:
- pd.DataFrame(rows).to_csv(temp, index=False, encoding='utf-8-sig', float_format='%.3f')
- os.replace(temp, target)
- finally:
- if os.path.exists(temp):
- os.unlink(temp)
- return target
- if __name__ == '__main__':
- try:
- print('预测完成:', run(sys.argv[1] if len(sys.argv) > 1 else ROOT / 'config.json'))
- except Exception as exc:
- print(f'预测失败:{type(exc).__name__}: {exc}', file=sys.stderr)
- raise SystemExit(1)
|