tsrWindSpeedAnalyst.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import os
  2. import pandas as pd
  3. import numpy as np
  4. import pandas as pd
  5. import matplotlib.pyplot as plt
  6. import seaborn as sns
  7. from .analyst import Analyst
  8. from .utils.directoryUtil import DirectoryUtil as dir
  9. from confBusiness import ConfBusiness
  10. class TSRWindSpeedAnalyst(Analyst):
  11. """
  12. 风电机组叶尖速比分析
  13. """
  14. def typeAnalyst(self):
  15. return "tsr_windspeed"
  16. def turbineAnalysis(self,
  17. dataFrame,
  18. outputAnalysisDir,
  19. outputFilePath,
  20. confData: ConfBusiness,
  21. turbineName):
  22. self.tsr(dataFrame, outputFilePath,
  23. confData.field_wind_speed, confData.field_rotor_speed, confData.field_power, confData.field_pitch_angle1, confData.rotor_diameter)
  24. def tsr(self, dataFrame, output_path, field_wind_speed, field_rotort_speed, field_power_active, field_angle_pitch, rotor_diameter):
  25. dataFrame['power'] = dataFrame[field_power_active] # Alias the power column
  26. # Calculate 'wind_speed_floor'
  27. dataFrame['wind_speed_floor'] = (dataFrame[field_wind_speed] / 1).astype(int) + 0.5
  28. # Ensure the necessary columns are of float type
  29. dataFrame['wind_speed'] = dataFrame[field_wind_speed].astype(float)
  30. dataFrame['rotor_speed'] = dataFrame[field_rotort_speed].astype(float)
  31. rotor_diameter = pd.to_numeric(rotor_diameter, errors='coerce')
  32. # Calculate TSR
  33. dataFrame['tsr'] = (dataFrame['rotor_speed'] * 0.104667 *
  34. (rotor_diameter / 2)) / dataFrame['wind_speed']
  35. # Group by 'wind_speed_floor' and calculate mean, max, and min of TSR
  36. grouped = dataFrame.groupby('wind_speed_floor').agg({
  37. 'power': 'mean',
  38. 'rotor_speed': 'mean',
  39. 'tsr': ['mean', 'max', 'min']
  40. }).reset_index()
  41. # Rename columns for clarity post aggregation
  42. grouped.columns = ['wind_speed_floor', 'power',
  43. 'rotor_speed', 'tsr', 'tsr_max', 'tsr_min']
  44. # Sort by 'wind_speed_floor'
  45. grouped = grouped.sort_values('wind_speed_floor')
  46. # Write the aggregated dataFrame to a new CSV file
  47. grouped.to_csv(output_path, index=False)
  48. def turbinesAnalysis(self, dataFrameMerge, outputAnalysisDir, confData: ConfBusiness):
  49. self.plot_tsr_distribution(outputAnalysisDir, confData.farm_name)
  50. def plot_tsr_distribution(self, csvFileDirOfCp, farm_name, encoding='utf-8'):
  51. """
  52. Generates Cp distribution plots for turbines in a wind farm.
  53. Parameters:
  54. - csvFileDirOfCp: str, path to the directory containing input CSV files.
  55. - farm_name: str, name of the wind farm.
  56. - encoding: str, encoding of the input CSV files. Defaults to 'utf-8'.
  57. """
  58. field_Name_Turbine = "turbine_name"
  59. x_name = 'wind_speed_floor'
  60. y_name = 'tsr'
  61. split_way = '_tsr_windspeed.csv'
  62. # 设置绘图样式
  63. sns.set_palette('deep')
  64. # 初始化结果DataFrame
  65. res = pd.DataFrame()
  66. # 遍历输入目录中的所有文件
  67. for root, dir_names, file_names in dir.list_directory(csvFileDirOfCp):
  68. for file_name in file_names:
  69. if not file_name.endswith(".csv"):
  70. continue
  71. file_path = os.path.join(root, file_name)
  72. # 读取CSV文件
  73. frame = pd.read_csv(file_path, encoding=encoding)
  74. # 提取设备名
  75. turbine_name = file_name.split(split_way)[0]
  76. # 添加设备名作为新列
  77. frame[field_Name_Turbine] = turbine_name
  78. # 选择需要的列并合并到结果DataFrame中
  79. res = pd.concat(
  80. [res, frame.loc[:, [field_Name_Turbine, x_name, y_name]]], axis=0)
  81. # 重置索引
  82. ress = res.reset_index()
  83. # 绘制全场TSR分布图
  84. fig, ax = plt.subplots(figsize=(16, 8))
  85. ax = sns.lineplot(x=x_name, y=y_name, data=ress,
  86. hue=field_Name_Turbine)
  87. ax.set_title('TSR-Distibute')
  88. plt.legend(ncol=4)
  89. plt.savefig(csvFileDirOfCp + r"/{}-TSR-Distibute.png".format(farm_name),
  90. bbox_inches='tight', dpi=300)
  91. plt.close(fig)
  92. # 绘制每个设备的TSR分布图
  93. grouped = ress.groupby(field_Name_Turbine)
  94. for name, group in grouped:
  95. color = ["lightgrey"] * len(ress[field_Name_Turbine].unique())
  96. fig, ax = plt.subplots(figsize=(8, 8))
  97. ax = sns.lineplot(x=x_name, y=y_name, data=ress, hue=field_Name_Turbine,
  98. palette=sns.set_palette(color), legend=False)
  99. ax = sns.lineplot(x=x_name, y=y_name, data=group,
  100. color='darkblue', legend=False)
  101. ax.set_title('turbine name={}'.format(name))
  102. plt.savefig(csvFileDirOfCp + r"/{}.png".format(name),
  103. bbox_inches='tight', dpi=120)
  104. plt.close(fig)