run.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """One CSV-based forecast, used by run.sh for integration debugging."""
  2. from pathlib import Path
  3. import json
  4. import os
  5. import sys
  6. import tempfile
  7. import pandas as pd
  8. from ultra_short_pv import PVForecaster
  9. from pv_io import ROOT, read_json, write_json
  10. from pv_inputs import prepare_from_csv
  11. def run(config_path):
  12. config_path = Path(config_path).resolve()
  13. cfg = read_json(config_path)
  14. def path(key):
  15. p = Path(cfg[key]).expanduser()
  16. return p if p.is_absolute() else config_path.parent / p
  17. code, issue = cfg['station_code'], cfg['issue_time']
  18. data, audit = prepare_from_csv(code, issue, path('power_csv'), path('ec_dir'),
  19. receipts=read_json(path('ec_receipts')))
  20. model = PVForecaster(code)
  21. result = model.predict(issue, data['power_history'][0], data['future_raw'][0])
  22. rows = [dict(station_code=result['station_code'], issue_time=result['issue_time'],
  23. capacity_kw=result['capacity_kw'], **point) for point in result['forecasts']]
  24. output = path('output_dir')
  25. output.mkdir(parents=True, exist_ok=True)
  26. name = result['station_code'] + '_' + pd.Timestamp(result['issue_time']).strftime('%Y%m%d%H%M')
  27. target = output / f'{name}.csv'
  28. write_json(audit, output / f'{name}.audit.json')
  29. fd, temp = tempfile.mkstemp(dir=output, suffix='.tmp')
  30. os.close(fd)
  31. try:
  32. pd.DataFrame(rows).to_csv(temp, index=False, encoding='utf-8-sig', float_format='%.3f')
  33. os.replace(temp, target)
  34. finally:
  35. if os.path.exists(temp):
  36. os.unlink(temp)
  37. return target
  38. if __name__ == '__main__':
  39. try:
  40. print('预测完成:', run(sys.argv[1] if len(sys.argv) > 1 else ROOT / 'config.json'))
  41. except Exception as exc:
  42. print(f'预测失败:{type(exc).__name__}: {exc}', file=sys.stderr)
  43. raise SystemExit(1)