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 39 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| from asammdf import MDF import os import pandas as pd import numpy as np from numpy import trapz from matplotlib import pyplot as plt import datetime as dt import docx from docx.shared import Inches
def check_MDF(Document_Adress): print('文件名: '+Adress_str+',相关统计信息如下:')
new_Doc = document.add_paragraph\ ('文件名: '+Adress_str+',相关统计信息如下:')
MDF_File=MDF(Document_Adress) TCOil= MDF_File.get("TCOil") TOil=MDF_File.get("TOil") VehSpd=MDF_File.get("VehSpd")
VehSpdCheck(VehSpd) Signal_Data_Max(TCOil) T_Subtract_Peak(TCOil, TOil, 20)
document.save(Veh_VIN+'.docx')
def timeconvert(second): Timestr =str(int(second/3600)) + 'h ' + \ str(int((second%3600)/ 60)) + 'min ' + \ str(int((second%3600) % 60)) + 's' return Timestr
def Signal_Data_Max(Signal): x=Signal.timestamps y=Signal.samples T_C_Max=np.max(y) Positon=np.where(y==np.max(y)) Time=(x[Positon[0][0]])
print('零件处油温最大值%d' %T_C_Max+\ '时间是'+timeconvert(Time))
new_Doc = document.add_paragraph\ ('零件处油温最大值为 '+str(T_C_Max)+' ℃,'\ '时间是'+timeconvert(Time),style='List Bullet')
def VehSpdCheck(Singal): x=Singal.timestamps y=Singal.samples Average_Vehspd=np.sum(y)/len(y) Drive_Distance=Average_Vehspd*x[-1]/3600 print('该文件平均车速 %.2f km/h,' %Average_Vehspd+\ '行驶里程 %.2f km' %Drive_Distance)
Average_Vehspd_str=str(round(Average_Vehspd, 2)) Drive_Distance_str =str(round( Drive_Distance, 2)) new_Doc = document.add_paragraph\ ('该文件平均车速 '+ Average_Vehspd_str+' km/h,'+\ '行驶里程= '+Drive_Distance_str+' km'+',见下图:',style='List Bullet')
plt.figure(Adress_str+'--'+Singal.display_name) plt.axis([np.min(x),np.max(x),0,np.max(y)]) plt.plot(x,y,label='$VehSpd$') plt.xlabel('t (s) ') plt.ylabel('VehicleSpeed (km/h) ')
plt.fill_between(x,y1=y,y2=0,facecolor='purple',\ alpha=0.2)
plt.text(x[-1]/3,np.max(y)-3, 'DriveDistance:%.2f km' \ %Drive_Distance, \ fontdict={'size': 10, 'color': 'red'})
plt.title(Adress_str) plt.legend(loc='upper right') picture_name=Adress_str +'-'+ \ Singal.display_name+'.png' plt.savefig(picture_name, dpi=200)
new_Doc = document.add_picture(picture_name, width=Inches(5.0)) os.remove(picture_name)
def T_Subtract_Peak(T1,T2,threshold): x = T1.timestamps y1 = T1.samples y2 = T2.samples k = len(x)
a = 0 i = 0 m = 0
T_Up = [] for a in range(0, k): T_Up.append(y1[a] - y2[a]) y3 = T_Up
while i < k - 1: if int(T1.samples[i] - T2.samples[i]) >= \ threshold: j = i while j < k - 1: j += 1 if int(T1.samples[j] - \ T2.samples[j]) < threshold - 3: m = m + 1 time = int(j / k * T1.timestamps[-1] / 60) print('该文件零件处油温与油底壳温差大于' + str(threshold) + '的波峰第 %d 处,' % m + \ '时间是 %d s,' % time + '零件处油温 %d ℃' % T1.samples[j])
new_Doc = document.add_paragraph \ ('该文件零件处油温与油底壳温差大于'\ + str(threshold) + '℃的波峰第' +\ str(m) + '处,时间是' + timeconvert(time))
i = j
break i = i + 1
plt.figure(Adress_str + '—' + T1.display_name + '-' + T1.display_name)
fig = plt.figure(num=1, figsize=(15, 8), dpi=80) fig, ax1 = plt.subplots() ax2 = ax1.twinx() ax1.plot(x, y1, '-', color='g', label='TCOil') ax1.plot(x, y2, '-', color='b', label='TOil') ax2.plot(x, y3, '-', color='r', label='T_Up') ax1.legend(loc='upper left') ax2.legend(loc='upper right')
ax1.set_ylim(np.min(y2) - 5, np.max(y1) + 5) ax2.set_ylim(np.min(y3) - 5, np.max(y3) + 5)
ax1.set_title(Adress_str)
ax1.set_xlabel('time(s)') ax1.set_ylabel("TC_Oil & T_Oil (℃)", color='b') ax2.set_ylabel("T_Up(℃)", color='r')
picture_name = Adress_str + '-' + T1.display_name + '-' + T2.display_name +'.png' plt.savefig(picture_name, dpi=100)
new_Doc = document.add_picture(picture_name, width=Inches(6.0)) os.remove(picture_name)
file_path = 'D:\SoftApp\Python\HardWay2StudyPython\MDFData' now_time=dt.datetime.now().strftime('%F %T') Veh_VIN=input("请在下方输入故障排查车辆VIN数字编号后,回车:") print(Veh_VIN+"车辆"+'数据分析时间:'+now_time)
document = docx.Document() document.add_heading(Veh_VIN+'车排查信息', 0) new_Doc= document.add_paragraph \ ('主要排查行驶里程、最高油温、\ 零件处与变速箱油温差值出现的峰值数。 ') new_Doc=document.add_paragraph\ (Veh_VIN+"车辆"+'数据分析时间:'+ now_time)
for root, dirs, files in os.walk(file_path): Documents=[os.path.join(root, name) for name in files]
for Document_Path in Documents: print('-'*60)
new_Doc = document.add_paragraph('-'*100) new_Doc = document.add_paragraph('-' * 100)
Adress=Document_Path.split('\\')[-1] Adress_str=Adress.split('.')[0]
check_MDF(Document_Path)
|