Challenge

Data Parsing using Python

Objective:

  • Write a script which 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.
  • 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.

Given Data File: https://drive.google.com/file/d/1L8GY56d-M8mB1KfceM-xVhGNvnCIqxjm/view

Procedure:

1. Open the sublime text file and first save the file with the extension .py.

2. Import some modules into the command window like:

  • import matplotlib.pyplot as plt

  • This module helps us to plot the graph of the given result.
  • import math

  • This module helps us to use mathematical functions like pie, sine, cos, etc.
  • import numpy

  • This module helps us to do mathematical operations on an element of an array.

  • Whenever we want to define more than one value in a single variable we have to use an array and numpy.linspace is also the same thing it stores value in a range like this;

  • numpy.linspace (lower limit, upper limit, no. of division)

  • This works acc. to a formula:
    Size of division = (upper limit-lower limit)/(no. of division-1)

3. Use an empty array for storing a value which we get from FOR loop, theta1 & theta2 will store 10 values.

4. Append is a command that will continuously append the value of a variable. It keeps on adding the values into an existing array.

so for that, we have to define an empty array for that variable which we are going to use append command. Temp is a variable in which we store values from append command.

5. use plt.plot() command to plot the graph of the given result and use plt.figure() command.

6. Import prettytable: PrettyTable is a simple Python library designed to make it quick and easy to represent tabular data in visually appealing ASCII table.

7. Empty array was created to store values of all the terms like crank, pressure temp etc.

8. Each line was splited using split() by , and empty space.

9. All values of engine data was stored in a column.

10. Area under curve is calculated using np.trapz(pressure, volume)

11. Power calculated using (area under PV/ time for one cycle)

12. BSFC calculated using formula (fuel consumption per unit time/ power output)

Coding:

#data_analysis_Ashwani

import math
import matplotlib.pyplot as plt 
from prettytable import PrettyTable
import numpy as np 

#datainputs 
crank = []
pressure = []
max_pressure = []
min_pressure = []
mean_temp =[]
max_temp = []
min_temp= []
volume = []
mass= []
density = []
integrated_HR = []
HR_rate = []
C_p = []
C_v = []
gamma = []
kin_visc = []
Dyn_visc = []

#file parsing
for line in open('engine_data.out'):
	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]))
		max_temp.append(float(line.split()[5]))
		min_temp.append(float(line.split()[6]))
		volume.append(float(line.split()[7]))
		mass.append(float(line.split()[8]))
		density.append(float(line.split()[9]))
		integrated_HR.append(float(line.split()[10]))
		HR_rate.append(float(line.split()[11]))
		C_p.append(float(line.split()[12]))
		C_v.append(float(line.split()[13]))
		gamma.append(float(line.split()[14]))
		kin_visc.append(float(line.split()[15]))
		Dyn_visc.append(float(line.split()[16]))


title= []
units= 	[]

#taking title from data

linecount = 1
for line in open('engine_data.out'):
	if linecount is 3:
		title.append(line.split())
	if linecount is 4:
		units.append(line.split())
	linecount=linecount +1

t=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]

print('All properties in given data')
table = PrettyTable(['serial no.','Terms'])
for i in range(0,len(t)):
	table.add_row([t[i],title[0][i+1]])
	
print(table)


a =[crank,pressure, max_pressure, min_pressure, mean_temp, max_temp, min_temp, volume, mass, density, integrated_HR, HR_rate, C_p, C_v, gamma, kin_visc, Dyn_visc]

x = int(input("input the 1st serial number which is x axis:"))
y = int(input("input the 2nd serial number which is y axis:"))

plt.plot(a[x],a[y], 'r--')
plt.xlabel(title[0][x+1]+units[0][x+1])
plt.ylabel(title[0][y+1]+units[0][y+1])
plt.show()

else:
	print('error, enter relevant value')

pressure = np.array(pressure)*1e6
volume = np.array(volume)
area_underPV = np.trapz(pressure,volume) #numpy.trapz(x.y) will integrate along the given axis using trapezoidal rule and calculate area under curve on points x,y
print('work done =', area_underPV,"joules")

#power output
rpm = 1500
one_rev_time = rpm/60 # time for one revolution in sec
	

onecycle= 2*one_rev_time    #one rev = 2strokes     #one cycle = 4strokes      #one cycle= 2revolution
power = area_underPV/onecycle
print('Power of engine =', power,'watt' '(when rpm is 1500)')
print('Power of engine =', power/0.746,'HP')

	
#calculating specific fuel consumption (brakespecificfuelconsumption)

m = 20*pow(10,-6)      #Fuel consumption per unit time in gram
bsfc= (3600*m)/power   #BSFC = Fuel consumption per unit time / Brake Horse Powe
print('BSFC = ',bsfc,'g/kwH') 

Sublime doesn't take input so using tool option package control was installed and and then from package control "sublimeREPL".

Program run:

Output:

P-V Diagram plot:

Conclusion:

1. Got the understanding how to manage big chunk of data using python scripting.

2. We can take output in any form like we have plotted PV diagram usng the data given.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.