material.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. class MaterialLoader:
  12. def __init__(self, base_path, lazy_load=True):
  13. self.base_path = Path(base_path)
  14. self.lazy_load = lazy_load
  15. self._data_cache = {}
  16. self.opt = args.parse_args_and_yaml()
  17. def _load_material(self, station_path):
  18. """核心数据加载方法"""
  19. input_file = self.base_path / station_path
  20. # 根据您的原始代码逻辑简化的加载流程
  21. try:
  22. basic = pd.read_csv(input_file / self.opt.doc_mapping['basic'], sep=r'\s+', header=0)
  23. power = pd.read_csv(input_file / self.opt.doc_mapping['power'], sep=r'\s+', header=0)
  24. plant_type = int(basic.loc[basic['PropertyID'].tolist().index('PlantType'), 'Value'])
  25. assert plant_type == 0 or plant_type == 1
  26. # 根据电站类型加载数据
  27. nwp_v = pd.read_csv(input_file / '0' / self.opt.doc_mapping['nwp_v'], sep=r'\s+', header=0)
  28. nwp_v_h = pd.read_csv(input_file / '0' / self.opt.doc_mapping['nwp_v_h'], sep=r'\s+', header=0)
  29. nwp_own = pd.read_csv(input_file / '1' / self.opt.doc_mapping['nwp_own'], sep=r'\s+', header=0)
  30. nwp_own_h = pd.read_csv(input_file / '1' / self.opt.doc_mapping['nwp_own_h'], sep=r'\s+', header=0)
  31. if self.opt.switch_nwp_owner:
  32. nwp_v, nwp_v_h = nwp_own, nwp_own_h
  33. # 如果是风电
  34. if plant_type == 0:
  35. station_info = pd.read_csv(input_file / self.opt.doc_mapping['station_info_w'], sep=r'\s+', header=0)
  36. station_info_d = pd.read_csv(input_file / self.opt.doc_mapping['station_info_d_w'], sep=r'\s+', header=0)
  37. nwp = pd.read_csv(input_file / self.opt.doc_mapping['nwp_w'], sep=r'\s+', header=0)
  38. nwp_h = pd.read_csv(input_file / self.opt.doc_mapping['nwp_w_h'], sep=r'\s+', header=0)
  39. cap = float(station_info.loc[0, 'PlantCap'])
  40. if (input_file / self.opt.doc_mapping['env_wf']).exists():
  41. env = pd.read_csv(input_file / self.opt.doc_mapping['env_wf'], sep=r'\s+', header=0)
  42. else:
  43. env = None
  44. # 如果是光伏
  45. else:
  46. station_info = pd.read_csv(input_file / self.opt.doc_mapping['station_info_s'], sep=r'\s+', header=0)
  47. station_info_d = pd.read_csv(input_file / self.opt.doc_mapping['station_info_d_s'], sep=r'\s+', header=0)
  48. nwp = pd.read_csv(input_file / self.opt.doc_mapping['nwp_s'], sep=r'\s+', header=0)
  49. nwp_h = pd.read_csv(input_file / self.opt.doc_mapping['nwp_s_h'], sep=r'\s+', header=0)
  50. cap = float(station_info.loc[0, 'PlantCap'])
  51. if (input_file / self.opt.doc_mapping['env_sf']).exists():
  52. env = pd.read_csv(input_file / self.opt.doc_mapping['env_sf'], sep=r'\s+', header=0)
  53. else:
  54. env = None
  55. return {
  56. 'station_info': station_info,
  57. 'station_info_d': station_info_d,
  58. 'nwp': nwp,
  59. 'nwp_h': nwp_h,
  60. 'power': power,
  61. 'nwp_v': nwp_v,
  62. 'nwp_v_h': nwp_v_h,
  63. 'env': env,
  64. 'cap': cap
  65. }
  66. except Exception as e:
  67. print(f"Error loading {station_path}: {str(e)}")
  68. return None
  69. def get_material(self, station_path):
  70. if self.lazy_load:
  71. if station_path not in self._data_cache:
  72. self._data_cache[station_path] = self._load_material(station_path)
  73. return self._data_cache[station_path]
  74. else:
  75. return self._load_material(station_path)
  76. if __name__ == "__main__":
  77. run_code = 0