| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- import os
- import pandas as pd
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- from matplotlib.ticker import MultipleLocator
- import seaborn as sns
- from behavior.analystExcludeRatedPower import AnalystExcludeRatedPower
- from utils.directoryUtil import DirectoryUtil as dir
- from algorithmContract.confBusiness import *
- class TSRWindSpeedAnalyst(AnalystExcludeRatedPower):
- """
- 风电机组叶尖速比分析
- """
- def typeAnalyst(self):
- return "tsr_windspeed"
- def turbineAnalysis(self,
- dataFrame,
- outputAnalysisDir,
- outputFilePath,
- confData: ConfBusiness,
- turbineName):
- self.tsr(dataFrame, outputFilePath,
- confData.field_wind_speed, confData.field_rotor_speed, confData.field_power, confData.field_pitch_angle1, confData.rotor_diameter)
- def tsr(self, dataFrame, output_path, field_wind_speed, field_rotort_speed, field_power_active, field_angle_pitch, rotor_diameter):
- dataFrame['power'] = dataFrame[field_power_active] # Alias the power column
- # Calculate 'wind_speed_floor'
- dataFrame['wind_speed_floor'] = (dataFrame[field_wind_speed] / 1).astype(int) + 0.5
- # Ensure the necessary columns are of float type
- dataFrame['wind_speed'] = dataFrame[field_wind_speed].astype(float)
- dataFrame['rotor_speed'] = dataFrame[field_rotort_speed].astype(float)
- # rotor_diameter = pd.to_numeric(rotor_diameter, errors='coerce')
- # # Calculate TSR
- # dataFrame['tsr'] = (dataFrame['rotor_speed'] * 0.104667 *
- # (rotor_diameter / 2)) / dataFrame['wind_speed']
- # Group by 'wind_speed_floor' and calculate mean, max, and min of TSR
- grouped = dataFrame.groupby('wind_speed_floor').agg({
- 'power': 'mean',
- 'rotor_speed': 'mean',
- 'tsr': ['mean', 'max', 'min']
- }).reset_index()
- # Rename columns for clarity post aggregation
- grouped.columns = ['wind_speed_floor', 'power',
- 'rotor_speed', 'tsr', 'tsr_max', 'tsr_min']
- # Sort by 'wind_speed_floor'
- grouped = grouped.sort_values('wind_speed_floor')
- # Write the aggregated dataFrame to a new CSV file
- grouped.to_csv(output_path, index=False)
- def turbinesAnalysis(self, dataFrameMerge, outputAnalysisDir, confData: ConfBusiness):
- self.plot_tsr_distribution(outputAnalysisDir, confData)
- def plot_tsr_distribution(self, csvFileDirOfCp, confData: ConfBusiness, encoding='utf-8'):
- """
- Generates tsr distribution plots for turbines in a wind farm.
- Parameters:
- - csvFileDirOfCp: str, path to the directory containing input CSV files.
- - farm_name: str, name of the wind farm.
- - encoding: str, encoding of the input CSV files. Defaults to 'utf-8'.
- """
- field_Name_Turbine = "turbine_name"
- x_name = 'wind_speed_floor'
- y_name = 'tsr'
- upLimitOfTSR=20
- # 设置绘图样式
- sns.set_palette('deep')
- # 初始化结果DataFrame
- res = pd.DataFrame()
- # 遍历输入目录中的所有文件
- for root, dir_names, file_names in dir.list_directory(csvFileDirOfCp):
- for file_name in file_names:
- if not file_name.endswith(CSVSuffix):
- continue
- file_path = os.path.join(root, file_name)
- # 读取CSV文件
- frame = pd.read_csv(file_path, encoding=encoding)
- # 提取设备名
- turbine_name = file_name.split(CSVSuffix)[0]
- # 添加设备名作为新列
- frame[field_Name_Turbine] = turbine_name
- # 选择需要的列并合并到结果DataFrame中
- res = pd.concat(
- [res, frame.loc[:, [field_Name_Turbine, x_name, y_name]]], axis=0)
- # 重置索引
- ress = res.reset_index()
- # 绘制全场TSR分布图
- fig, ax = plt.subplots(figsize=(16, 8))
- ax = sns.lineplot(x=x_name, y=y_name, data=ress,
- hue=field_Name_Turbine)
-
- ax.xaxis.set_major_locator(MultipleLocator(1)) # 创建一个刻度 ,将定位器应用到y轴上
- ax.set_xlim(0,26)
- ax.yaxis.set_major_locator(MultipleLocator(
- confData.graphSets["tsr"]["step"] if not self.common.isNone(confData.graphSets["tsr"]["step"]) else 5)) # 创建一个刻度 ,将定位器应用到y轴上
- ax.set_ylim(confData.graphSets["tsr"]["min"] if not self.common.isNone(confData.graphSets["tsr"]["min"]) else 0,
- confData.graphSets["tsr"]["max"] if not self.common.isNone(confData.graphSets["tsr"]["max"]) else upLimitOfTSR)
-
- ax.set_title('TSR-Distibute')
- plt.legend(ncol=4)
- plt.xticks(rotation=45) # 旋转45度
- plt.savefig(csvFileDirOfCp + r"/{}-TSR-Distibute.png".format(confData.farm_name),
- bbox_inches='tight', dpi=300)
- plt.close(fig)
- # 绘制每个设备的TSR分布图
- grouped = ress.groupby(field_Name_Turbine)
- for name, group in grouped:
- color = ["lightgrey"] * len(ress[field_Name_Turbine].unique())
- fig, ax = plt.subplots(figsize=(8, 8))
- ax = sns.lineplot(x=x_name, y=y_name, data=ress, hue=field_Name_Turbine,
- palette=sns.set_palette(color), legend=False)
- ax = sns.lineplot(x=x_name, y=y_name, data=group,
- color='darkblue', legend=False)
-
- ax.xaxis.set_major_locator(MultipleLocator(1)) # 创建一个刻度 ,将定位器应用到y轴上
- ax.set_xlim(0,26)
-
- ax.yaxis.set_major_locator(MultipleLocator(
- confData.graphSets["tsr"]["step"] if not self.common.isNone(confData.graphSets["tsr"]["step"]) else 2)) # 创建一个刻度 ,将定位器应用到y轴上
- ax.set_ylim(confData.graphSets["tsr"]["min"] if not self.common.isNone(confData.graphSets["tsr"]["min"]) else 0,
- confData.graphSets["tsr"]["max"] if not self.common.isNone(confData.graphSets["tsr"]["max"]) else upLimitOfTSR)
- ax.set_title('turbine name={}'.format(name))
- plt.xticks(rotation=45) # 旋转45度
- plt.savefig(csvFileDirOfCp + r"/{}.png".format(name),
- bbox_inches='tight', dpi=120)
- plt.close(fig)
|