Challenge

Week 6 - Data analysis

AIM: To create a script for the following operations by parsing a given converge output file using Python.

Data visualizer:

  • The script should take column numbers as the input and plot the respective column as separate images.
  • Each file should be saved by the name of the column.
  • The plot label 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 extracted and must appear as labels in the plots. THIS SHOULD NOT BE DONE MANUALLY.

Objectives of the project:

  1. To calculate the area under the P-V diagram. P is in column 2 and V is in column 8
  2. To calculate the power output of this engine. Assume that RPM is 1500.
  3. To calculate its specific fuel consumption. This engine consumed 20 micro grams of fuel(per 1 stroke cycle)

Introduction:

Parsing in computer languages refers to syntactic analysis of the input code into its components parts in order to facilitate the writing of compilers and interpreters. It can be used to describe split or separation.

Parsing a file means reading in a data stream of some sort and building an in memory model of the semantic content of that data. This aims at facilitating performing some kind of transformation on the data.

In other words, Data parsing is a method where one string of data gets converted into a different type of data. So let’s say you receive your data in raw HTML, a parser will take the said HTML and transform it into a more readable data format that can be easily read and understood.

  • Python is used to display the code of the program:
# A program to create a script for the following operations by parsing a given converge output file using Python.

# Importing math library function

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

# Defining the empty arrays

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=[]

# Enabling the user to enter the required filename

file=input('Enter the name of the input file: ')
print(file)

# Command to open the file

try:
	file_data=open(file,'r')
	print(' Data file is successfully opened')

except filenotfounderror:
	print(' Please enter a valid file name')

# Intializing a counter variable

line_count=1

# Reading the data of each line from the file using for loop command 

for line in file_data:

	if line_count is 3:
		label=line.split()
			
	if line_count is 4:
		Units=line.split()
			
	line_count=line_count+1

	if '#' not in line:

# Appending the data into the empty arrays

		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]))

# Assigning the column no. for each column

print('Please select the engine parameters from below list: ')

for i in range(0,17+1):
	filename=str(label[i])+str(Units[i])
	print(filename)

Parameter=np.array([[0],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])

# Enabling the user to input value of column numbers

print('\nEnter the column no. of an engine parameters to plot graph:  ')

x=input("Column no. of an engine parameter on the x-axis:  ")
x=int(x)

y=input("Column no. of an engine parameter on the y-axis:  ")
y=int(y)

# Plotting the graph with respective to the column no.s entered by user

plt.plot(Parameter[x],Parameter[y])
# Giving the varying title for each plot
plt.title(str(label[x])+'  '+'vs'+'  '+ str(label[y])+'  ' 'plot')
plt.xlabel(str(label[x]) + str(Units[x]))
plt.ylabel(str(label[y]) + str(Units[y]))
plt.show()

# Finding the Area under P-V graph, Power output and Specific fuel consumption 

if x==8 and y==2:

	Area=abs(np.trapz(Parameter[x],Parameter[y]))
	Workdone=Area*1000
	RPM=1500
	Power=2*math.pi*RPM*Workdone/60

	Fuel_per_stroke=20*pow(10,-6)
	Fuel_per_sec=Fuel_per_stroke*RPM*(0.5/60)
	Fuel_per_hour=Fuel_per_sec*3600
	BSFC=Fuel_per_hour/Power

	print('Area under PV-graph: ',Area)
	print('Workdone:',Workdone,'KJ')
	print('Power output of the engine:',Power,'KW')
	print('Specific fuel consumption:',BSFC,'g/KWh')


Procedure of above program:

  • Import the necessary math, matplotlib and numpy modules.
  • Create an empty lists of array for different input parameters to be appended.
  • Use input command to enable the user to input the required file name and with the help of open file command open it for file parsing.
  • Now, use try and except command to check the input file is compatible or not
  • After suitable input of correct file name the file gets opened in reading mode with help of ‘r’ command.We need to use counter variable for line count.
  • Using if statement, we can separate the lines containing # and the lines not containing #.
  • The split command will convert the data into the form of strings.
  • Then, float command is used to convert this strings into floating point numbers and then append them to their respective parameter list.
  • Now, we use the for loop to perform the same task for each line in data file.
  • Create an array of various engine parameters with respective to its column numbers.
  • For loop specify the different filename for each columns specified by the user.
  • Then, an input command for which the user can specify the columns they want to plot for graph.
  • Create the plots for column[x] and column[y] with respective to the data entered by the user.
  • After the plot is done and saved , we can use this data to find the area under the graph with the help of importing trapz from numpy to perform the calculation.
  • From the value of area and the given parameters , we can calculate the power and specific fuel consumption.

Output:

Note:In order to run an input command, we need to use SublimeREPL to get an output because input command doesnot work in sublime BUILD.

  • Compatibility check:

  • Results:

  • Plots:

Calculation Output:

Area under the P-V graph: 0.0002590377238248494

Workdone:0.2590377238248494 KJ

Power Output of the Engine:40.68955050853843 KW

Specific Fuel Consumption: 0.022118700962576152 g/KWh

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.