Challenge

Week 6 - Data analysis

Aim :To parse the given data file using python and hence to plot respective graphs.

Theory: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 perfoming some kind of transformation on the data. The main aim in this project is to capture all the coefficients provided in the file and to calulate the enthapy , entropy and the specific heat of the particular species.

Main code:

import math                                 #importing libraries
import matplotlib.pyplot as plt 
import numpy as np
line_count=1                       #creating empty arrays with partivular name
Crank=[]
Pressure=[]
Max_pressure=[]
Min_pressure=[]
Mean_temperature=[]
Max_temperature=[]
Min_temperature=[]
Volume=[]
Mass=[]
Density=[]
Integrated_HR=[]
HR_rate=[]
C_p=[]
C_v=[]
Gamma=[]
Kin_Visc=[]
Dyn_Visc=[]

	
#asking for the input from the user

file=(input('Input the file name that has to be parsed: '))

if file.endswith('out') is True:                                 #if the file contains'out' then the further staements will be executed
	for line in open('engine_data.out','r'):

		
	
		if '#' not in line:                                       #appending all the parameters with the respective columns values
			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_temperature.append(float(line.split()[4]))
			Max_temperature.append(float(line.split()[5]))
			Min_temperature.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]))


	#Declaring an array p for storing all the parameters
	p=[Crank,Pressure,Max_pressure,Min_pressure,Mean_temperature,Max_temperature,Min_temperature,Volume,Mass,Density,Integrated_HR,HR_rate,C_p,C_v,Gamma,Kin_Visc,Dyn_Visc]
	print('Please input the columns between 1 and 17')
	q=int(input('Enter the column that has to be in x-axis: '))     #asking the user to input the desired column
	r=int(input('Enter the column that has to be in y-axis: '))

	
	

	

	#Declaring an array for splitting the line for the parameters
	sp=[]
	line_count=1

	for line in open(file,'r'):
		if "#" in line:           
			if line_count is 3: #splitting the third line since all the parameters are present
				l=(line.split()[1:18])
				
				sp.append(l)

		line_count = line_count+1 #incrementing line_count

	

	plt.plot(p[q-1],p[r-1])        # plotting the required graph
	plt.xlabel(sp[0][q-1])
	plt.ylabel(sp[0][r-1])
	name=l[q-1]+'vs'+ l[r-1]
	plt.title(name)	
	plt.show()


#for work done of  the graph
	work=np.trapz(Pressure,Volume)*100           #Calculating the work done , sfc and power 
	print("Work Done ={:f} KJ".format(work))
	rpm=1500/2
	rps=rpm/60
	power=work*rps
	print("Power={:f} KW".format(power))
	fuel=20*pow(10,-6)*rps*3600
	sfc=fuel/power
	print("SFC={:f} g/KWh".format(sfc))
else:
	print("File not recognised. Please provide a valid CONVERGE output file")

Code Explanation:

  • The required libraries are imported
  • Empty arrrays are created for the respective parameters which are present in the data file.
  • The user is asked to input the file name to be parsed,and the program executes further only if the file name ends with 'out'
  • Now using if statement , a condition is applied and further , each array is appended with the resective column values
  • Now the user is asked to input his desired columns for which he wishes to visualize the graph
  • Once the input is done, the plots are generated and the xlabel and ylabel are created on its own.
  • The title is also created on its own.
  • After that, the neccesary calculations are made to calculate the values of
  1. Area under the Pressure -Volume diagram which is nothing but the work done
  2. The power transmitted by the engine
  3. And the value of SFC

Results and Plots:

Here, when the user is asked to input the file name,and if the file name does not end with 'out',an error message as shown above will be generated

Once after the user inputs the proper filename, the user is asked to input colums between 1 and 17

When the user inputs the columns of volume and pressure, the above graph is generated and the title and x and y labels gets generated on its own.

Once after the graph gets generated , the values of work done, power and SFC will be calculated and shown as the output.

Note: Command prompt was used to visualise the output of the above program.

Conclusion:File parsing is a very important concept which helps us to read and write data into a file and make further use of it. Python is a powerful software which can conduct these opertions very effectievely.

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.