Data Analysis using Python
Objective: To create a data visualizer tool using python.
Program:
#data analysis
import math
import matplotlib.pyplot as plt
import numpy as np
filename = input(\'Enter file name \')
if filename.endswith(\'.out\'):
crank=[]
pressure=[]
max_pressure=[]
min_pressure=[]
mean_temp=[]
min_temp=[]
max_temp=[]
volume=[]
mass=[]
density=[]
int_hr=[]
hr_rate=[]
Cp=[]
Cv=[]
gamma=[]
kin_vis=[]
dyn_vis=[]
for line in open(\'engine_data.out\'): #input data values in arrays
if \'#\' not in line:
crank.append(float(line.split()[0]))
pressure.append(float(line.split()[1]))
max_pressure.append(float(line.split()[2]))
min_pressure.append(float(line.split()[3]))
mean_temp.append(float(line.split()[4]))
min_temp.append(float(line.split()[5]))
max_temp.append(float(line.split()[6]))
volume.append(float(line.split()[7]))
mass.append(float(line.split()[8]))
density.append(float(line.split()[9]))
int_hr.append(float(line.split()[10]))
hr_rate.append(float(line.split()[11]))
Cp.append(float(line.split()[12]))
Cv.append(float(line.split()[13]))
gamma.append(float(line.split()[14]))
kin_vis.append(float(line.split()[15]))
dyn_vis.append(float(line.split()[16]))
X = int(input(\"Input first column number: \")) #input from user
Y = int(input(\"Input second column number: \"))
#--------------------------------------------------------------- #labels code
C1=[]
ct1=1
for line in open(\'engine_data.out\'):
if ct1 is 3:
for i in range (0,17):
C1.append(line.split()[i])
ct1=ct1+1
C2=[]
ct2=1
for line in open(\'engine_data.out\'):
if ct2 is 4:
for j in range (0,17):
C2.append(line.split()[j])
ct2=ct2+1
plt.xlabel(C1[X]+C2[X])
plt.ylabel(C1[Y]+C2[Y])
#-----------------------------------------------------------
W=np.array([crank,pressure,max_pressure,min_pressure,mean_temp,min_temp,max_temp,volume,mass,density,int_hr,hr_rate,Cp,Cv,gamma,kin_vis,dyn_vis]) #plot code
plt.plot(W[X-1] ,W[Y-1] )
plt.savefig(str(C1[X])+\' vs \' +str(C1[Y])+\'.png\')
plt.show()
A=np.trapz(pressure,volume) #PV area
print (\'Area under PV diagram is....\',A)
P=A*1500/60 #Power output
print (\'power output of engine is...\',P,\'MW\')
SFC=(20*12.5*3.6*pow(10,-6))/P #specific fuel consumption
print (\'specific fuel consumption is...\',SFC,\'g/kWh\')
else:
print (\'File not recognized. Please provide a valid CONVERGE output file\')
Procedure:
- \'math\' and \'matplotlib.pyplot\' were imported for using mathematical functions and plotting.\'numpy\' was imported for using \'trapz\' command.
- Empty arrays for all the parameters given in the datasheet was created.
- A \'for loop\' was created to read all the lines(rows) of the datasheet and an \'if-condition\' was given within the \'for loop\' to read only the numerical data of the file from these rows. In order to store the values of a column to the specified array, indexing was used and the values were appended to the previously created empty arrays.
- \'input\' function was used to obtain the two column inputs (X and Y) from the user.
- Another array (W) was created which stored all the parameter arrays. The column number inputs from the user were used as the index values of this new array (W) to plot the graph.
- In order to show the correct labels on the graph, an array (C1) was formed. An \'if-condition\' was specified for reading only the 3rd line of the data set and a \'for loop\' was formed within it to append all the parameters of the row to the (C1) array. The same process was repeated for forming array C2 which contains the units of all the parameters.\'xlabel\' and \'ylabel\' commands were used with inputs as the index value of the C1 and C2 arrays (which is given by the user) to show the appropriate labels on the graph.\'savefig\' command was used with inputs as index value of C1 array to save the plot generated with appropriate file names.
- To check the data file compatibility, an input function was created to take the file name from the user and an \'if-else\' condition was created which checks the extension \'.out\' of the file name(given by the user) using \'endswith\' command. If the extension matches then the program runs or else it prints a scripted message.
- The area under the PV curve is obtained by using the \'trapz\' command and giving the pressure and volume arrays as input. This area also represents the work done.
- The power output was calculated using the formula `P=W.rps`
- The specific fuel consumption was calculated using the formula `SFC=(r/P)` where r is the fuel consumption rate in grams per second (g/s) and P is the power produced in (watts).
Results:
- The program initially asks for the file name and only proceeds when a \'.out\' file is provided. If a \'.out\' file is not provided the message \"File not recognized. Please provide a valid CONVERGE output file\" is given by the program.
- The program asks the user for two input columns and then the program plots the graph between the columns provided as shown in the figure.
- The area under the PV diagram was found to be 0.0005
- The value of the power output of the engine was 0.01251 MW.
- The value of specific fuel consumption was 0.07191 g/kWh