engine data analysis in python
Script for the data visulizer is given below and discussed next with images
#importing lobraries
import matplotlib.pyplot as plt
import numpy as np
import math
#variables for the engine data
crank = []
pressure = []
max_pressure = []
min_pressure = []
mean_temp = []
max_temp = []
min_temp = []
volume = []
mass = []
density = []
integrated_hr = []
hr_rate = []
cp = []
cv = []
gamma = []
kin_visc = []
dyn_visc = []
cname = []
#keep track of line no.
linecount = 1
#taking file name from user
filename = input('Enter the converge output filename -')
print('\n')
try:
#opening the file and take reading permossion
file = open(filename, 'r')
#line by line execution
for line in file:
value=line.split()
if linecount is 3:
#extractiong the column headings
cname = value
if linecount is 4:
#extracting the column heading units
uname = value
if '#' not in value:
#assigning all the variables from file
crank.append(float(value[0]))
pressure.append(float(value[1]))
max_pressure.append(float(value[2]))
min_pressure.append(float(value[3]))
mean_temp.append(float(value[4]))
max_temp.append(float(value[5]))
min_temp.append(float(value[6]))
volume.append(float(value[7]))
mass.append(float(value[8]))
density.append(float(value[9]))
integrated_hr.append(float(value[10]))
hr_rate.append(float(value[11]))
cp.append(float(value[12]))
cv.append(float(value[13]))
gamma.append(float(value[14]))
kin_visc.append(float(value[15]))
dyn_visc.append(float(value[16]))
linecount = linecount + 1
#making array of all variables in the file
allvalues = [crank,pressure,max_pressure,min_pressure,mean_temp,max_temp,min_temp,volume,mass,density,integrated_hr,hr_rate,cp,cv,gamma,kin_visc,dyn_visc]
print('choose two column no. from below list for which you want graph-')
#displaying the list of the column headings
for i in range(1,18):
print(str(i) + ' ' + cname[i])
print('\n')
#taking the column no. as input from user
a = int(input('enter the first column no. '))
b = int(input('enter the second column no. '))
print('\n')
#special case for p-v diagram
if a==2 and b==8:
#calculating the required quantites
area = np.trapz(pressure,volume)
output_power = 1000*area*1500/60
sfc = 20*3600*pow(10,-6)/(0.08*output_power)
print('Area under the curve is :' + str(area))
print('output power of this engine is :' + str(output_power) + ' in [kw]')
print('specific fuel consumption for this engine is :' + str(sfc) + ' in [Kg/kw-h]')
#plotting part here
plt.figure()
plt.plot(allvalues[b-1],allvalues[a-1])
plt.xlabel(cname[b] + uname[b])
plt.ylabel(cname[a] + uname[a])
plt.savefig(cname[a] + 'VS' + cname[b] + '.png')
plt.show()
#execution if filename is not found in current directory
except:
print('enter valid filename or column no')
This script takes the converge file as a input from the user which is engine data file from the converge output file which containes the engine data.
data has 17 column in the file which has many rows for the differents data for the engine.
Now we are going to make a data visuliser tool for this type of file so here user gives the column no. as iput to take a look at graph for column no. which he/she has entered as input. and graph is shown on the windows and also the graph is saved as name of the column no automatically not manully and figure is saved in the current working directory of the python program
- It takes the column no. as input and shows the graph
the picture showing that it takes column no as input is given below
It shows the graph on the seperate windows.picture is given below
- Each file is saved as its name of the column
the file name is automatically retrived from the data file and name is given automatically and saved as the name of the columns as shown in the below picture
- The plot labels are extracted from the file. If, user request for a plot between column 1 (crank angle) and column 8(volume), then the label informations are automatically be extracted and appear as labels in the plots.This all is done automatically
- The code exits gracefully, if a non-compatible file is provided as an input. It says "enter valid filename or column no"
Here text file is provided instead of the out file so it displayes this message as shown in the figure
- Code explaination
-First we import all the necessary library
-declare all the variable required to save the engine data with 17 columns and so there is 17 variables to save. here the line 3 is column heading so to save the column heading we declare the variable called cname also
-to keep track of the line count we declare linecount variable which is initialised as 1
-we take file name as a input from the user displaying this message "Enter the converge output filename -"
-here if the valid file name is provided then it will try next code if the valid filename is not provided then it displayes this "enter valid filename or column no"
-Now if the valid filename is provided then we have to open the file and read it. in this file we have to read it line by line so we have to make for loop and next we have to split the line and save it in the variable name value
-Now first 3 lines are introduction line so if the 3rd line is reached we have to extract the column name from this line if the 4th line is reached than we have to extract the unit from this line so we have inserted the if condition in here and if there is # mark in starting of the line it does nothing to next line and increase the linecount variavle by 1.
-Now if there is no # mark in the starting of the line we have to extract variable from this line like first is crank , second is pressure and so on.all these values are in text format so we have to have to convert them to the float values and save them in corresponding value.after assigning all the value we increase line count by one.
-now access all these variables we make new array named allvalues so that we can access all these from that array
-Now we have to show user the list of the column and its no. so that he/she can choose the column no correctly we print messeage for that and make for loop to display all the column heading and sr no.
- Now we take two column no. as input from the user saved in a and b this input is integer as we know it and from this we make plot of the variables retrived form the variables now in plotting we know that index in python starts from the 0 and user enters from the 1 so we have to subtract one from the indexing of the plot figure than we give them a label which retrived from the cname and uname varibles and save figure as a name of the column name
-If user enter the pressure and volume as input then we have to perform the calculation for the area under the curve , output power and specific fuel consumption(sfc).to calculate the area use trapz commad from the python numpy library now to calculate the output power in kw we have formula '1000*area*rotation in second' means "1000*area*rpm/60" Here rpm is given as 1500 rpm. To calculate the sfc we have formula "fuel consumed in gram per stroke *3600*/(0.08*output_power)" means "20*3600*10^(-6)/(0.08*output_power)" and we print all these data with converting this values to the string variable.