material.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # @FileName :material.py
  4. # @Time :2025/4/29 11:07
  5. # @Author :David
  6. # @Company: shenyang JY
  7. import pandas as pd
  8. from pathlib import Path
  9. from concurrent.futures import ThreadPoolExecutor
  10. from functools import partial
  11. from app.model.config import myargparse
  12. args = myargparse(discription="算法批量修模", add_help=False)
  13. class MaterialLoader:
  14. def __init__(self, base_path, lazy_load=True):
  15. self.base_path = Path(base_path)
  16. self.lazy_load = lazy_load
  17. self._data_cache = {}
  18. self.opt = args.parse_args_and_yaml()
  19. def _load_material(self, station_path):
  20. """核心数据加载方法"""
  21. input_file = self.base_path / station_path
  22. # 根据您的原始代码逻辑简化的加载流程
  23. try:
  24. basic = pd.read_csv(input_file / self.opt.doc_mapping['basic'], sep=r'\s+', header=0)
  25. power = pd.read_csv(input_file / self.opt.doc_mapping['power'], sep=r'\s+', header=0)
  26. plant_type = int(basic.loc[basic['PropertyID'].tolist().index('PlantType'), 'Value'])
  27. assert plant_type == 0 or plant_type == 1
  28. # 根据电站类型加载数据
  29. nwp_v = pd.read_csv(input_file / '0' / self.opt.doc_mapping['nwp_v'], sep=r'\s+', header=0)
  30. nwp_v_h = pd.read_csv(input_file / '0' / self.opt.doc_mapping['nwp_v_h'], sep=r'\s+', header=0)
  31. nwp_own = pd.read_csv(input_file / '1' / self.opt.doc_mapping['nwp_own'], sep=r'\s+', header=0)
  32. nwp_own_h = pd.read_csv(input_file / '1' / self.opt.doc_mapping['nwp_own_h'], sep=r'\s+', header=0)
  33. if self.opt.switch_nwp_owner:
  34. nwp_v, nwp_v_h = nwp_own, nwp_own_h
  35. # 如果是风电
  36. if plant_type == 0:
  37. station_info = pd.read_csv(input_file / self.opt.doc_mapping['station_info_w'], sep=r'\s+', header=0)
  38. station_info_d = pd.read_csv(input_file / self.opt.doc_mapping['station_info_d_w'], sep=r'\s+', header=0)
  39. nwp = pd.read_csv(input_file / self.opt.doc_mapping['nwp_w'], sep=r'\s+', header=0)
  40. nwp_h = pd.read_csv(input_file / self.opt.doc_mapping['nwp_w_h'], sep=r'\s+', header=0)
  41. cap = float(station_info.loc[0, 'PlantCap'])
  42. if (input_file / self.opt.doc_mapping['env_wf']).exists():
  43. env = pd.read_csv(input_file / self.opt.doc_mapping['env_wf'], sep=r'\s+', header=0)
  44. else:
  45. env = None
  46. # 如果是光伏
  47. else:
  48. station_info = pd.read_csv(input_file / self.opt.doc_mapping['station_info_s'], sep=r'\s+', header=0)
  49. station_info_d = pd.read_csv(input_file / self.opt.doc_mapping['station_info_d_s'], sep=r'\s+', header=0)
  50. nwp = pd.read_csv(input_file / self.opt.doc_mapping['nwp_s'], sep=r'\s+', header=0)
  51. nwp_h = pd.read_csv(input_file / self.opt.doc_mapping['nwp_s_h'], sep=r'\s+', header=0)
  52. cap = float(station_info.loc[0, 'PlantCap'])
  53. if (input_file / self.opt.doc_mapping['env_sf']).exists():
  54. env = pd.read_csv(input_file / self.opt.doc_mapping['env_sf'], sep=r'\s+', header=0)
  55. else:
  56. env = None
  57. return {
  58. 'station_info': station_info,
  59. 'station_info_d': station_info_d,
  60. 'nwp': nwp,
  61. 'nwp_h': nwp_h,
  62. 'power': power,
  63. 'nwp_v': nwp_v,
  64. 'nwp_v_h': nwp_v_h,
  65. 'env': env,
  66. 'cap': cap
  67. }
  68. except Exception as e:
  69. print(f"Error loading {station_path}: {str(e)}")
  70. return None
  71. def get_material(self, station_path):
  72. if self.lazy_load:
  73. if station_path not in self._data_cache:
  74. self._data_cache[station_path] = self._load_material(station_path)
  75. return self._data_cache[station_path]
  76. else:
  77. return self._load_material(station_path)
  78. if __name__ == "__main__":
  79. run_code = 0