Data Analysis in PYTHON
AIM : Write a program in PYTHON to create a data visualizer tool.
Compatability check
- The code should exit gracefully, if a non-compatible file is provided as an input. It should say something like "File not recognized. Please provide a valid CONVERGE output file"
Data visualizer
- The script should take column numbers as the input and plot the respective columns as separate images
- Each file should be saved by the name of the column
- The plot labels should be extracted from the file.
Basic performance calculation
- Calculate the area under the P-V diagram. P is in column 2 and V is in column 8.
- Calculate the power output of this engine. Assume that RPM is 1500
- This engine consumed 20 micro grams of fuel (per 1 stroke cycle). Calculate its specific fuel consumption.
EXPLANATION OF CODE :
1) Firstly we import all the necessacry libraries.
2) We then check if the data file is compatable or not and we open and read it . Here we use a keyword called enumerate as it assigns the values for each line and are stored in the variable ' i '. To read and append the column names we used the ' if '#' in l ' statement .
3) To read and append the column values we used the ' if '#' not in line ' statement and proceeded for the further calculations .
4) Plotting the graphs and saving the names of given axis by splitting the names into different columns and labelling the axis by them .
eg : c = l.split()
xname = c[x]
plt.xlabel(c[x])
5) To calculate area under the PV curve we use :np.trapz(pressure,volume) where trapz is a definite integral as approximated by trapezodial rule , which is used to calculate the area under given x & y locations .
Power = `(2*pi*area*10^6 *1500)/(60/1000)`
Specific fuel consumption = `(3600*20e-6*1500)/(60*power)`
PYTHON CODE :
""" PROGRAM TO PERFORM DATA ANALYSIS """
import math
import matplotlib.pyplot as plt
import numpy as np
# COMPATABILITY CHECK
try :
open('engine_data.out','r')
except : # if the output displays FileNotFoundError then we print the below
print('File not recognized. Please provide a valid CONVERGE output file.')
file_name = open('engine_data.out','r') # opening and reading the file
for i,l in enumerate(file_name): # enumerate keyword stores assigned values in the 'i' variable
if '#' in l :
if i == 2:
c = l.split()
# DATA VISUALIZER
x = 1 # crank angle
y = 8 # volume
xname = c[x]
yname = c[y]
xdata = []
ydata = []
for line in open('engine_data.out','r'): # feeding data into the file name arrays
if '#' not in line:
xdata.append(float(line.split()[x-1])) # appending the data into the specific file name
ydata.append(float(line.split()[y-1]))
# GRAPH PLOTS FOR CRANK ANGLE AND VOLUME
plt.plot(xdata,ydata)
plt.xlabel(c[x])
plt.ylabel(c[y])
plt.savefig('volume&crank.png')
plt.show()
# INPUTS FOR PV GRAPH
x = 2 # pressure
y = 8 # volume
pressure = []
volume = []
for line in open('engine_data.out','r'):
if '#' not in line:
pressure.append(float(line.split()[x]))
volume.append(float(line.split()[y]))
# GRAPH PLOTS FOR PRESSURE AND VOLUME
plt.plot(volume,pressure)
plt.xlabel('volume')
plt.ylabel('pressure')
plt.show()
# BASIC PERFORMANCE CALCULATION
area = np.trapz(pressure,volume)
print('Area under the PV curve = ',area)
power = 2*math.pi*area*pow(10,6)*1500/(60/1000)
print('Power = ',power,'Watt')
sfc = (3600*20e-6*1500)/(60*power)
print('Specific fuel consumption = ',sfc,'gram/KW*hr')
GRAPHS & OUTPUTS :
1) Crank angle V/S Volume :
2) Pressure V/S Volume :
3) Performance calculation output :
4) Compatibility check with wrong file name :
ERRORS :
CORRECTIONS : for i,l in enumerate(f):