Project

Data Analysis using Python

In this project, thermodynamic data collected from simulation of a Port Fuel Injected engine is extracted and analysed from a data file using a Python program. The data file is available for downloads in this link: https://drive.google.com/file/d/1L8GY56d-M8mB1KfceM-xVhGNvnCIqxjm/view?usp=sharing.

The data file consists of 17 columns of different thermodynamic parameters for which more than 8000 values have been recorded. The objective is to open the file in the directory, read and extract desired data from the file. The program is further designed to be interactive with the console to seek input from users on opening desired file name, extracting columns to generate plots and save the same in the current directory. The final part of the program allows the user to calculate the Work done per cycle, Power output for specified rpm and Specific Fuel Consumption.

The program is also designed to exit gracefully in case an invalid file name is entered for file parsing.

Below is the program:

"""Parsing a data file"""


import math
import matplotlib.pyplot as plt
import numpy as np
import sys




def extract(file):

	try:
		
		line_no = 1
		lines = []

		#Open file, read and assign index to each line
		for line in open(file):
			line_no = line_no + 1
			lines.append(line.split())


		#Extract Column no., Parameters and units	
		for i in range(0, 4):
			
			if i == 1:
				column_no = lines[i]

			elif i == 2:
				parameters = lines[i]
			elif i == 3:
				units = lines[i]


		
		#Function to extract values of each column and store it in an array "val"		
		def func(n, parameters, lines):
			

			nm_p = parameters[n]
			nm_un = units[n]

			val = []
			f = n - 1
			val.append(nm_p)
			val.append(nm_un)
			for i in range(5, len(lines)):
			
				val.append(lines[i][f])
	
			return val					
		

		#Array conataining parameters, units and respective values column-wise
		col = []
		sz = []
		for a in range(0,17):
			col.append(func((a+1), parameters, lines))
			sz.append(a+1)


		
		print ('\n')	

		for u in range(0, 17):
			print ('Column ' + str(u+1) + ': ' + col[u][0] )

		print ('\n')

		ex = 'ra'
		while ex != 'e':
				
			#Input column nos for plots
			y = int(input('Enter column on y: '))
			x = int(input('Enter column on x: '))

			

			if x not in sz:
				print ('Enter valid Column no for x and y!\n')
				continue
			elif y not in sz:
				print ('Enter valid Column no for x and y!\n')
				continue	


			

			x_name = str(col[x-1][0] + ' ' + col[x-1][1])
			y_name = str(col[y-1][0] + ' ' + col[y-1][1])
			ttl = str(col[y-1][0] + ' ' + 'vs' + ' ' + col[x-1][0])


			x_val = []
			y_val = []

			p_val = []
			v_val = []

			#X and Y values to plot specified columns
			for l in range(2, (len(lines)-4)):
				x_val.append(float(col[x-1][l]))
				y_val.append(float(col[y-1][l]))

				p_val.append(float(col[1][l]))
				v_val.append(float(col[7][l]))


			
			#Function to calculate area under the curve using trapezium formula	
			def area(y, x, m, n, v):
				sm_i = 0

				for l in range(m, n):

					a = abs(x[v+l] - x[v+l-1])
					b = y[v+l] + y[v+l-1]
					sm_i = sm_i + .5*a*b

				return sm_i	


			min_v = min(v_val)
			max_v = max(v_val)

			s = v_val.index(min_v)
			t = v_val.index(max_v)
			z = (len(v_val)-1)

			

			

			w_1 = area(p_val, v_val, 1, (t+1), 0)*1000000

			w_2 = area(p_val, v_val, 1, (s-t+1), t)*1000000

			w_3 = area(p_val, v_val, 1, (z-s+1), s)*1000000


			#Area under the curve = w_1 - w_2 + w_3

			area = w_1 - w_2 + w_3

			


			


			

			#Plot for the desired column no.s
			plt.plot(x_val, y_val)
			plt.xlabel(x_name)
			plt.ylabel(y_name)
			plt.title(ttl)
			plt.savefig(ttl)

			print ('\n')

			print ('Plot for ' + ttl + ' has been saved to current directory \n\n\n')

			
			while ex != 'c' or 'e':
				ex = input('Press "c" to continue or "e" to exit: ')
				if ex == 'c':
					break
				elif ex == 'e':
					break
				else:
					print ('Invalid entry!')			


					
			print ('\n\n\n\n\n\n\n\n')


		



		print ('-------------------------------------------------------------------------------------------------')	
		#Calculaion of Area, Power and Spcific Fuel Consumption	

		#Area under the curve	
		print ('Area under PV curve = ' + str(area) + ' sq.m\n')

		rpm = int(input('Enter Revolutions per minute: '))

		#Power output of Engine | p = w * rpm | rpm = 1500
		pwr = area*rpm/60

		#Power in kw
		pwr_kw = pwr/1000

		print ('Power output of the Engine = ' + str(pwr_kw) + ' kW\n')




		#Specific Fuel Consumption | spc = mass/pwr_kw | mass = 20*10**(-6)
		mg = int(input('Enter mass of fuel consumed by Engine per cycle(in micrograms): '))
		spc = mg*10**(-6)/pwr_kw


		print ('Specific Fuel Consumption = ' + str(spc) + ' g/kW')


		





			


	except IndexError:
		print ('File not recognized. Please provide a valid CONVERGE output file')

	except FileNotFoundError:
		print ('File not recognized. Please provide a valid CONVERGE output file')	



		

#Input file name
extract(input('Enter file name with extension: '))









	





Below is the plot for Pressure vs Volume:

Area under the curve (work done per cycle) = 500.3546 sq.m
Power Output (for 1500 rpm) = 12.5087 kW
Specific Fuel Consumption (for 20 micrograms fuel consumed per cycle) = 1.5987 mg/kW

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.