Project

Reading and Interpreting Data from File Using Python

Reading and Interpreting Data from File Using Python

Objective:

  • Read the data from given file and interpret the results in plots using python
  • Creata a code which couldn't accept files not having specified extenion and it pops out the error in manual string format written by developer.
  • Calculate the engine parameters using data from file using python.

Description:

The given data file is in '.out' format and some values are given for calculating engine parameters

    • RPM=1500
    • Fuel consumption=20micro gram for 1 stroke cycle
from numpy import *
import os
import math
from prettytable import PrettyTable
import matplotlib.pyplot as plt 


try:
	y=input("specify the path of file to extract info-")
	z=input('enter the file name-')
	os.chdir(y)
	if ".out" in z:
		lin=0
		labels,units=[],[]
		for line in open(z):
			if lin==2:
				labels.append(line.split())
			elif lin==3:
				units.append(line.split())
			lin=lin+1
		table=PrettyTable(["columns","name"])
		for i in range(1,len(labels[0])):
			table.add_row([i,labels[0][i]])
		print("The file consists of following columns")
		print(table)
		c=int(input("enter the no of column to plot for x-axis-"))
		d=int(input("enter the no of column to plot for y-axis-"))
		k=(c,d)
		for i in range(1,len(labels[0])):
			command=''
			command='column'+str(i)+'=[]'
			exec(command)
			for line in open(z):
					if '#' not in line:
						commandd=''
						commandd='column'+str(i)+'.append(float(line.split()[i-1]))'
						exec(commandd)
		comm='plt.plot(column'+str(k[0])+',column'+str(k[1])+')'
		plt.xlabel(str(labels[0][k[0]])+ str(units[0][k[0]]))
		plt.ylabel(str(labels[0][k[1]])+str(units[0][k[1]]))
		exec(comm)
		plt.savefig(str(labels[0][k[0]])+"Vs"+str(labels[0][k[1]])+".png")
		print("your plot has been saved")
		plt.show()
		area=trapz(column2,column8)
		print("The area under the P-V diagram is",area*(10**3),"KN-m")
		f=20
		rpm=1500
		power=(area*(10**3)*rpm*2*math.pi)/60
		print("The power of engine is",power, "KW")
		s=power/0.746
		print("The BHP of the engine is",s,"hp")
		sfc=(20*pow(10,-6)*3600)/(power*0.08)
		print('specific fuel consumption is',sfc,'g/KWh')

	else:
		print("File not recognized. Please provide a valid CONVERGE output file")		
except FileNotFoundError as e:
	print("File is not Valid. please check the file")
except ValueError as e :
	print("Invalid inputs")
finally:
	print("program closed")
  • Importing of required modules are necessary for using specific module related functions.
  • The whole code is in try block.. if any errors pop it will pop out the statements there in except blocks.
  • Code ask the user for following inputs and the flow will be like this
  • Code changes the working directory to specified path and read the given file
  • It reads the code line by line and takes the titles of columns in lists and output as shown by using for-loop and table creation from PrettyTable module.
  • Then it ask user for following inputs:
  • Program asks the user for -select the no of column for plotting in x,y axis.
  • Then program read the all the data and create lists and stores values for respective columns using for-loop and exec function
  • And outputs the plot on the screen as well as the same has been saved in first specified path using commands from matplotlib.pyplot module
  • And when you close the shown plot on screen.
  • The code outputs the following results.
  • Area under P-V diagram has been calculated with trapz which is a function of numpy module.
  • Power, BHP and Specific fuel Consumption are calculated by simple formulas shown in code.
  • And finally block has been excecuted as program closed.

Commands used in code:

Results:

  • The following are the outputs of the code :

Conclusion:

  • Data from the given file is extracted and interpreted as plots and tables using Python.
  • Using Try -Except and If blocks file compatibility is checked using python.
  • Given engine parameters are successfully calculated and outputs the same using Python.
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.