DATA ANALYSIS using Python
DATA ANALYSIS using Python
Aim :
To create a program in Python to create a data visualizer tool by parsing a given CONVERGE output file named 'engine_data.out'.
Objectives :
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. For example, If it is requested for a plot between column 1 (crank angle) and column 8(volume), then the label information should automatically be extracted and must appear as labels in the plots and should be done in a manual way.
Compatability check
The code should exit gracefully, if a non-compatible file is provided as an input. It should display error message like "File not recognized. Please provide a valid CONVERGE output file"
Basic performance calculation
Calculate the area under the P-V diagram. P is in column 2 and V is in column 8 in the data file.
Calculate the power output of this engine by assuming RPM = 1500.
For a scenario, the engine consumed 20 micro grams of fuel (per 1 stroke cycle). Calculate its specific fuel consumption.
Description of Code :
1. In the beginning of the program, all the necessary modules are imported.
2. The compatability check process is done through try & except conditions, a message is printed when the file opening is successful or not with relevant information.
3. Enumerate keyword is used in the for loop such that each time a line is read, a value is assigned to the variable i and after the conditions are satisfied, the column names are obtained by the split() function.
4. THe inputs for the column numbers are obtained and the data in those columns are collected separately into other variables and the graph is plotted with those values.
5. Similarly, the data in the pressure and volume columns are obtained separately into other variables and the Pv graph is plotted with those values.
6. The area under the PV diagram are calculated by using the trapz function and displayed.
7. The power is calculated by the below mentioned formula along with parameter values of area and the rpm is considered as 1500.
`Power = (2pi*area*10^6*1500)/(60/1000)`
8. The Specific fuel consumption value is calculated by the formula mentioned below along with the value of power obtained in the power formula and the value for fuel rate is considered as 20 micro grams of fuel (per 1 stroke cycle).
Specific fuel consumption = `(3600*fuelrate*1500)/(60*power)`
Program Code :
"""
Program to perform Data Analysis in Python
"""
#importing modules
import math
import matplotlib.pyplot as plt
import numpy as np
#Checking Compatibility
try :
open('engine_data.out','r')
print('File opening successful\n\n')
except :
print('File not recognized. Please provide a valid CONVERGE output file.')
#This message is printed if any error is found for opening the file
#Opening file in read mode
filename = open('engine_data.out','r')
"""
enumerate keyword assigns value for each line and
the value assgined is stored in i variable
"""
for i,ln in enumerate(filename):
if '#' in ln:
if(i == 2):
p = ln.split()
#Data Visualizer
column1 = int(input("Enter the column number for x-axis in plot : "))
column2 = int(input("Enter the column number for y-axis in plot : "))
#assigning the name of the column
column1_name = str(p[column1])
column2_name = str(p[column2])
#initialising empty arrays for appending
column1_data = []
column2_data = []
#appending data into the arrays
for line in open('engine_data.out','r'): # feeding data into the file name arrays
if '#' not in line:
pn=line.split()
column1_data.append(float(pn[column1-1]))
column2_data.append(float(pn[column2-1]))
#plotting
file = column1_name + ' vs ' + column2_name + '.png'
plt.plot(column1_data,column2_data)
plt.xlabel(column1_name)
plt.ylabel(column2_name)
plt.savefig(file)
plt.show()
#Plotting graphs for PV diagram
column3 = 2 #pressure
column4 = 8 #volume
pressure = []
volume = []
for line in open('engine_data.out','r'):
if '#' not in line:
a = line.split()
pressure.append(float(a[column3-1]))
volume.append(float(a[column4-1]))
plt.plot(volume,pressure)
plt.xlabel('Volume')
plt.ylabel('Pressure')
plt.savefig('PV.png')
plt.show()
#Basic performance calculation
#trapz function is used to calculate area under curve
area = np.trapz(pressure,volume)
print('\n\nArea under the PV graph = ',area,' sq.units')
#Power calculations
rpm = 1500
power = 2*math.pi*area*pow(10,6)*rpm/(60/1000)
print('\nPower = ',power,'Watts')
#Specific fuel consumption calculation
fuel_rate = 20e-6
specific_fuel = (3600*fuel_rate*rpm)/(60*power)
print('\nSpecific fuel consumption = ',specific_fuel,' gram/KW*hr')
Output :
This graph is obtained as the input value for the columns were 1 and 2 which represents Crank and Pressure respectively.
Plot for the Pressure and Volume data - PV Diagram
Values obtained for Area, Power, Specific fuel consumption are: