Week 6 - Data analysis
AIM: To write a program to do data analysis and data visualization using python.
Objective:
To write a program that can open and read the data.
To perform some calculation based on the data available.
To write a program to enter the input from the user to plot some graphs accordingly.
To plot graphs between Pressure Vs volume, Crank angle Vs Pressure and Crank Angle Vs Temperature.
Explanation/steps to follow:
Initially, you have to import some modules to write function and perform calculations accordingly.
After that we want to open the data file into our program.
Before opening the data, we will check for the compatibility of the data file. So we are using try and except function to check for the data file.
After that we have removing the lines starting with '#' so that it would not be considered in the calculation part.
Then we are using split command to split the data and also trying to append in the value.
Then we are using astype command to cast the data value into float
After that, i am performing some calculations such as calculation of area under curve, integration using trapezoidal rule, also perform work, power and specific fuel consumption calculation.
After that I have written some codes to visualize data in a better way.
And then ask the user to enter the value from the above mentioned parameters, to use them in the plots accordingly.
Then I have written the codes for the plotting the graphs.
After completing the code and running the codes.
Then i open up the command prompt and started the program there and get the outputs.
Program:
#Program to do data analysis from the provided data
#Importing the modules required for various function
import math
import numpy as np
import matplotlib.pyplot as plt
#Compability Check
try:
data = open('engine_data.out', 'r')
except FileNOTFound Error:
print('File Not Recognized. Please provide a valid Converge output file')
#Opening the data file into the program
line_count = 1
value = []
for line in data:
if '#' not in line:
value.append(line.split())
column = np.transpose(value)
column = column.astype(np.float)
#Area Under P-V Curve using Trapezoidal Rule
xvar =column[1]
yvar =column[7]
dx = []
k=0
while k < 8664:
dif=xvar[k+1]-xvar[k]
dx.append(dif)
k = k+1
W = []
Work = 0
t = 0
while t < 8664:
W.append(((dx[t]/2)*(yvar[0]+2*(yvar[t+1])+yvar[8664])))
t = t + 1
for n in range(0,len(W)):
Work = Work + W[n]
#Work Calculation
Work = Work*1000000
print('Work Calculated:')
print(abs(Work),'Joules')
print('\n')
#Power Calculation
power = (Work*2*1500*(math.pi)*0.001)/60
print("Power Calculated:")
print(abs(power), 'Watts')
print('\n')
#SpecificFuel Consumption Calculation
sfc =0.02/(abs(power))
print('Specific fuel Consumed is:' )
print(sfc, 'gram/joules')
print('\n')
#Data Visulaization:
parameter = ['Crank Angle', 'Pressure', 'Maximum Pressure', 'Minimum Pressure', 'Mean Temperature', 'Maximum Temperature', 'Minimum Temperature', 'Volume', 'Mass', 'Density', 'Heat Rejection','Heat Rejection Rate', 'C_P','C_V', 'gamma', 'Kinematic Viscosity', 'Dynamic Viscosity']
parameter_num = ['Crank Angle =0', 'Pressure=1', 'Maximum Pressure=2', 'Minimum Pressure=3', 'Mean Temperature=4', 'Maximum Temperature=5', 'Minimum Temperature=6', 'Volume=7', 'Mass=8', 'Density=9', 'Heat Rejection=10','Heat Rejection Rate=11', 'C_P=12','C_V=13', 'gamma=14', 'Kinematic Viscosity=15', 'Dynamic Viscosity=16']
print('Press \n 1 for Crank-Angle \n 2 for Pressure \n 3 for Maximum Pressure \n 4 for Minimum Pressure \n 5 for Mean Temperature \n 6 for Maximum Temperature \n 7 for Minimum Temperature \n 8 for Volume \n 9 for Mass \n 10 for Density \n 11 for Heat Rejection \n 12 for Heat Rejection Rate \n 13 for C_P \n 14 for C_V \n 15 for gamma \n 16 for Kinematic Viscosity \n 17 for Dynamic Viscosity')
#Asking user to enter the Component number to plot graphs
print("Please Enter X-component to plot graph from above numbers:")
x=int(input())
print("Please Enter Y-component to plot graph from above numbers:")
y=int(input())
#Plotting the curve
plt.plot(column[x-1], column[y-1], linewidth =3 , color ='red')
plt.xlabel(parameter[x-1], color ='green', fontsize=12)
plt.ylabel(parameter[y-1], color ='green', fontsize=12)
plt.show()
Output:
Results:
Crank Angle Vs Mean Temperature
Crank Angle Vs Pressure
Pressure Vs Volume