python pandas数据结构转化
背景介绍
windsim已经导出所有机位点的风速风向,每个机组都有16个扇区的风速和风向信息(轮毂高度处)
同时你还有fluent或者AIWIND一个扇区的CFD结果, 一个excel表包含所有机组的信息,每个机组的速度廓线放在一个sheet表中, 为了提取对应轮毂高度处的信息,
AIWIND或者fluent计算得到的一个方向的速度廓线计算结果:
前面我们已经使用 python对比作图 ,可在此基础上 进行技术分解优化!
技术分解
- 访问windsim的speed table
- 遍历AIWIND或者fluent速度廓线得到的所有机位点的sheet,并选取90m高度处,存放入新的表中
- 转化pandas的Series和list,方便实用index函数
技术实现
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
## 注意需要有windsim各个扇区的数据
#fileReadyAnalysis=r"C:\Users\yezhaoliang\Desktop\NewSimulation\Results-newSimulation45.xlsx"
#fileReadyAnalysis=r"C:\Users\yezhaoliang\Desktop\NewSimulation\Results-newSimulation0.xlsx"
## Sector: 0 22 45 67 90 112 135 157 180 202 247 270 292 315 337
plotSector='0'
## AIWIND导出的数据需要汇总
#fileReadyAnalysis=r"C:\Users\yezhaoliang\Desktop\NewSimulation\Results-newSimulation"+plotSector+".xlsx"
aiwindfileReadyAnalysis=r"C:\Users\yezhaoliang\Desktop\work\AIWind\DongPing\Results-newSimulation"+plotSector+".xlsx"
if __name__ == "__main__":
accordingTable = [
"C1566","C2738","C2742","C3284","C4179","C4182","K01", "K02", "K03", "K04", "K05", "K06", "K07", "K08", "K09", "K10", "X1", "X2", "X3", "X4", "X5", "X6", "X7",
"X8", "X9", "X10", "X11", "X12", "X13", "X14", "X15", "X16", "X17", "X18", "X19", "X20", "X21", "X22", "X23", "X24",
"X25", "X26", "X27", "X28", "X29", "X30", "X31", "X32", "X33", "X34", "X35", "X36", "X37", "X38", "X40", "X41",
"X42", "X43", "X44", "X45", "X46", "X47", "X48", "X49", "X50", "X51", "X52", "X53", "X54", "X55", "X56", "X57",
"X58", "X59", "X60", "X61", "X62", "X63", "X64", "X65", "X66", "X67", "X68", "X69", "X70", "X71", "X72", "X73",
"X74", "X75", "X76", "X77", "X78", "X79", "X80", "X81", "X82", "X83", "X84", "X85", "X86", "X87", "X88", "X89",
"X90", "X91", "X92", "X93", "X94", "X95", "X96", "X97", "X98", "X99", "X100"]
x_axisChange = [
"C1566","C2738","C2742","C3284","C4179","C4182","K01", "K02", "K03", "K04", "K05", "K06", "K07", "K08", "K09", "K10", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
"29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "40", "41", "42", "43", "44", "45", "46", "47", "48",
"49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67",
"68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86",
"87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100"];
windSimspeedTable=r'C:\Users\yezhaoliang\Desktop\work\AIWind\DongPing\ResultsDongPingSpeedTable.csv'
speedTableData=pd.read_csv(windSimspeedTable)
indexMe=[x for x in speedTableData.iloc[:,0]]
# b = pd.read_excel(fileReadyAnalysis,None)
# sheetnames=b.keys()
# sheetvalues=b.values()
#hubVelocities=np.zeros(sheetvalues.__len__())
xlsxAiwind =pd.ExcelFile(aiwindfileReadyAnalysis)
turbines=pd.Series([0]*accordingTable.__len__())
turbineVelocity=pd.Series([0]*accordingTable.__len__(),dtype='float')
windSimVelocity=pd.Series([0]*accordingTable.__len__(),dtype='float')
count=0
## 读取通过程序汇总的各个扇区的数据(各个机位点存储在相应sheet表中
for sheetname in xlsxAiwind.sheet_names:
pdtemp=xlsxAiwind.parse(sheetname)
#print(pdtemp.iloc[5:20,2])
#linreg = LinearRegression()
newCount=accordingTable.index(sheetname[1:])
newCountST=indexMe.index(sheetname[1:]+"_Velocity")
turbines[newCount]=sheetname[1:]
windSimVelocity[newCount]= speedTableData.iloc[newCountST,1]
## 机位点名字
print(sheetname[-7:-4])
## 数据提取 AIWIND 单个扇区下的处理
text1=pdtemp.iloc[0:100,2]
text2=pdtemp.iloc[0:100,1]
iMax=[i for i in range(len(text1)) if text1[i]>90]
neededIndex=iMax[0]
neededIndexSmaller=iMax[0]-1
smallZ=text1[neededIndexSmaller]
smallV=text2[neededIndexSmaller]
bigZ = text1[neededIndex]
bigV = text2[neededIndex]
# yi=[y for y in pdtemp.iloc[0:100,1]]
# linreg.fit(pdtemp.iloc[5:20,2], pdtemp.iloc[5:20,1])
hub_height=90
velocityPredict=(bigV-smallV)/(bigZ-smallZ)*(hub_height-smallZ)+smallV
# velocityPredict= linreg.predict(hub_height)
print(velocityPredict)
turbineVelocity[newCount]=velocityPredict
count=count+1
df=pd.concat([turbines,turbineVelocity,windSimVelocity],axis=1)
#df.to_excel('hello45.xlsx')
df.to_excel(r'C:\Users\yezhaoliang\Desktop\work\AIWind\DongPing\\'+plotSector+'.xlsx')
# speeTableSeries=pd.Series(speedTableData.values())
# tempArray=pd.concat(turbineVelocity[0:23],speeTableSeries)
#temp1=[x for x in speedTableData]
#temp2=[y for y in turbineVelocity]
#tempArray=[temp1,temp2]
aiwindYaxis= turbineVelocity
windsimYaxis=windSimVelocity
#ylimMax=max(tempArray[1])
#ylimMin=min(tempArray[1])
ylimMax=10
ylimMin=2
# 开始画图
plt.figure(figsize = (25,8))
plt.title('Sector-'+plotSector+' Analysis',fontsize=32 )
#plt.plot(x_axix, aiwindYaxis, marker='o', color='black', label='AIWIND')
plt.plot(x_axisChange, aiwindYaxis, marker='o',markersize='9', color='red',markerfacecolor='grey',markeredgecolor='black',linestyle='solid', label='AIWIND',linewidth=3)
#plt.plot(sub_axix, test_acys,marker='*', color='red', label='testing accuracy')
#plt.plot(x_axix, windsimYaxis,marker='*', color='blue', label='WindSiM')
plt.plot(x_axisChange, windsimYaxis,marker='*',markersize='9', color='black',markerfacecolor='grey',markeredgecolor='black',label='WindSiM',linewidth=3)
#plt.plot(x_axix, thresholds, color='blue', label='threshold')
plt.legend() # 显示图例
plt.margins(0)
plt.grid()
plt.ylim(ylimMin-1,ylimMax+1)
plt.xlabel('Turbine Names',fontsize=24 )
plt.ylabel('Velocity',fontsize=24 )
plt.tick_params(axis='both',which='major',labelsize=24)
plt.savefig(r"C:\Users\yezhaoliang\Desktop\work\AIWind\DongPing\Velocity-test"+plotSector+".jpg",dpi = 900)
Related