Python-Data Analysis
Please find the Code,Code Explanation, Results and Errors Below.
Code
#This is a program to make it easier for data analysis using python
import matplotlib.pyplot as plt
import numpy as np
from numpy import trapz
import time
lc=1
row_count=0
heading=0
crank=[]
pressure=[]
coloumn_name=[]
coloumn_units=[]
#First Compaitablity Test
try:
open('engine_data.out')
except:
print("File not recognized or present. Please provide a valid converge output file")
sys.exit()
f='engine_data.out'
for line in open(f,'r'):
if '#' not in line:
row_count=row_count+1
coloumn_count=len((line.split()))
else:
heading=heading+1
if heading==3:
coloumn_names=(line.split())
print(coloumn_names)
elif heading==4:
coloumn_units=(line.split())
x= input("Please give required x axis data coloumn number (1-17) 1-Crank to 17-Dynamic Viscosity ")
nx=int(x)
print (nx-1)
y=input("Please give required y axis data coloumn number (1-17) 1-Crank to 17-Dynamic Viscosity ")
ny=int(y)
print(ny-1)
print("The Graph you requested is ",coloumn_names[ny],"vs",coloumn_names[nx])
data_array_x=[]
data_array_y=[]
for i in open(f,'r'):
if '#' not in i:
data_array_x.append(float(i.split()[nx-1]))
#print(data_array_x)
for j in open(f,'r'):
if '#' not in j:
data_array_y.append(float(j.split()[ny-1]))
#print(data_array_y)
plt.plot(data_array_x,data_array_y)
plt.xlabel(coloumn_names[nx]+coloumn_units[nx])
plt.ylabel(coloumn_names[ny]+coloumn_units[ny])
plt.savefig(coloumn_names[nx] +' VS '+ coloumn_names[ny]+'.png')
plt.show()
#Area under PV diagram
volume=[]
pressure=[]
for k in open(f,'r'):
if '#' not in k:
pressure.append(float(k.split()[1]))
volume.append(float(k.split()[7]))
#Calculating area under the pv curve
area = np.trapz(pressure,volume)
print('The total area under the graph is ',area)
#Calculating power output of the engine
rpm = 1500
power = area * 1e+06* rpm/(9550)
print('The power output of the engine is ',power,'KW')
#Calculating specific fuel consumption
fuel_per_stroke = 20*1e-06
mass_flow_rate=2.99*1e-10 #mass flow rate in grams per second
Specific_Fuel_Consumption = mass_flow_rate/ (power)
print('The Specific Fuel Consumption is',Specific_Fuel_Consumption,'g/KW')
plt.plot(volume,pressure)
plt.savefig(coloumn_names[8] +' VS '+ coloumn_names[2]+'.png')
plt.xlabel("Volume")
plt.ylabel("Pressure")
plt.show()
Code Explanation
The required libraries including matplotlib.pyplot and numpy is imported
The first compaitablity test is done using try and except function
After this using for loop the the names of the coloumns as well as their units are stored in arrays.
Using input function the x and y axis of the required graph is taken
using for loop the datat in the required coloumns are stored in respective arrays.
Following this The graph is plotted,displayed and saved.
After this the PV graph is plotted. The data is taken from the respective coloumns using for loop as before
The area under the PV graph is calculated using special function in Numpy called trapz.
After this the require values of power and Specific fuel consumptios in found out
Results
Errors Faced
No Major errors except some syntax mistakes