Challenge

Parsing of Data and File handling using Python Programming.

Problem Statement:-

The python program should ask for the inputs and provide the graph between the inserted input- commands i.e. the column numbers and also do the following

  • Calculate the area under the P-V diagram. If 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.

Note:- This program is executed under tools è SublimeREPL è Python è Python- RunCurrent

Objective:

The python should to the following tasks:

  • Data visualize: 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. If, I request 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.
  • Compatibility check: 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"
  • Basic engine performance calculations: calculate the power, specific fuel consumption.

Solution Procedure:

since the sublime compiling interface do not take the inputs from the user it will be difficult to solve the problem hence we use sublime REPL interface in tools to execute this python code.

Note:- This program is executed under tools è SublimeREPL è Python è Python- Runcurrentfile.

  • The executed file ask for the name of the file which is saved in the same directory that the program is saved.
  • By entering the correct file name the rest of the file gets executed otherwise the program suspends by providing the information
  • After successful entry of file name here the file name is “engine_data.out”. The program reads the each and every line in the file.
  • With the help of for loop and if command we can pull out any data from the file.
  • With the help of user given inputs the program will display the graph between the entered inputs and perform the Basic engine performance calculations.
  • n = N/60 converting the RPM into Number of cycles per second(RPS) (angular velocity).

Power = Work*n/1000 where the work is in joules hence to covert in to kJ/s divide by 1000 (Kw)

Mass of fuel consumed per hour = mass of fuel consumed per cycle * speed in rpm *60. (Kg/hr)

Specific fuel consumed = mass of fuel consumed per hour/power (kg/kw.hr)

"""
A python code to demonstrate the big data handling and file parsing
by Rupesh
"""
import math
import matplotlib.pyplot as plt 
import numpy as np 
from tabulate import tabulate

filename = input('Enter the file name with extension(.out): ') 

# Compatibility check
try:
	open(filename)

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


file = open(filename, 'r')	#reading the file and assaigning the name to file

# Extracting the data from the file
a=[] 		# storing the properties
k=[]		# storing the variables
unit =[]	#storing the units

line_counter = 1
for line in file:
		for i in range(1,18):
			if line_counter == 3:
				a.append(line.split()[i]) 
				k.append(i)
			if line_counter == 4:
				unit.append(line.split()[i])
		line_counter = line_counter + 1

# Printing the data in a table format
headers = ('column no','property')
table = tuple(zip( k, a ))
print(tabulate(table,headers=headers,tablefmt='grid')) 

# Extracting the data according the input parameters from file
file = open(filename, 'r')
option1 = [] 	#storing the data according the user input of x-axis
option2 = []	#storing the data according the user input of y-axis

print('Enter the column nos from above property table')
x = int(input ('enter required columns no1 as x_axis: '))
y = int(input ('enter required columns no2 as y_axis: '))

line_count = 1
for line in file:
	if '#' not in line:
		option1.append(float(line.split()[x-1]))
		option2.append(float(line.split()[y-1]))
	line_count = line_count + 1

xlabel = a[x-1] 	                # assigning the X-label
ylabel = a[y-1]	                        # assigning the Y-label
title = (ylabel+' '+'vs'+' '+xlabel) 	# assigning the title to the plot

#plots
plt.plot(option1,option2, lw=3)
plt.xlabel(xlabel+unit[x-1])
plt.ylabel(ylabel+unit[y-1])
plt.title(title)
plt.savefig(str(title)+'.png')
plt.show()

# calculating the area under the graph
area = np.trapz(option2,option1)
Work = area*pow(10,6)	# area in j or watt

# Given engine data 
N = 1500 			# Engine velocity RPM
m_f = 20 * pow(10,-6)		# mass of fuel consumed per stroke in Kg/stroke
n = N/60			# Number of cycles per second (angular velocity)
Power = Work*n/1000	        # Power output (kW) at speed n = rad/s
fc_hr = m_f*N*60		# fuel consumtion kg per hr
sfc = (fc_hr)/Power 		# Specific fuel consumtion (kg per kW.hr)

if x == 8:
	if y == 2:
		print('Workdone is the area under P_V diagram =', Work,'joules')
		print('power output =', Power, 'kW')
		print('Specific fuel consumption =', sfc,'kg/kW.hr')

Results:

If we enter the wrong file i.e., name of the file does not existed in the directory then the following message will be displayed(Compatabilty check).

If we provide with the required file name correctly we can able the see the following screen.

By entering the x_axis and y_axis correctly we will get the required out put and the output file i.e the graph is stored in the same directory.

suppose the user enterd x_axis as 1 and y_axis as 2 the following graph will pop up and is stored in directory as png file.

If we insert the X_axis as 8(volume) and y_axis as 2 (pressure) we will see the following results.

Basic engine performance

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.