Project

Data Analysis and File Parsing using Python

Objective- Write a code for checking file compatibility and error should come if not compatible file is provided. Also write a code to generate plot for any two columns in the given data file, and find area under P-V diagram engine power, specific fuel consumption from provided data values.

Summary- By using file parsing in python we are able to read, extract and create required output from the available data in the file. Parsing in general means analysis available data i.e. a string or text in syntactic components. Here, we will extract data from a provided text file which contains data about performance of an engine and write code to visualize the data.

#importing libraries
import math
import matplotlib.pyplot as plt 
import numpy as np
from scipy.integrate import simps


#file comptability check
try: 
	open(\'engine_data.out\', \'r\')

except:
	print(\'file not found, please provide a valid Converge file\')

data = \'engine_data.out\'


#File Parsing sta
#PLotting columns
column_count = 0 
row_count = 0
headers = 0
column_number = []
column_title= []
data_units = []

for line in open(data, \'r\'):
	if \'#\' not in line:
		row_count = row_count + 1
	
	else:
		headers = headers + 1
		if headers == 3:
			column_title.append(line.split())

		if headers == 4:
			data_units.append(line.split())
	

#Enter respective column number in order to get desired plot
X = (int(\'Enter column number for x-axis\')-1) #Enter column number for x-axis
Y = (int(\'Enter column number for y-axis\')-1) #Enter column number for y-axis

line_count = 1
X_column = []
Y_column = []

for line in open(data, \'r\'):
	if \'#\' not in line:
		X_column.append(float(line.split()[X]))
		Y_column.append(float(line.split()[Y]))

xlabel = (column_title[0][X+1] + data_units[0][X+1])
ylabel = (column_title[0][Y+1] + data_units[0][Y+1])

plt.plot(X_column, Y_column)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.savefig(column_title[0][X+1] + \'VS\' + column_title[0][Y+1] + \'.png\')
plt.show()


#Finding area under PV diagram
Volume = []
Pressure = []

for i in open(data, \'r\'):
	if \'#\' not in i:
		Pressure.append(float(i.split()[1]))
		Volume.append(float(i.split()[7]))

#Area under P-V diagram = Work Done

Pressure = np.array(Pressure)*1e6 # Converting MPa to Pa

Work_done = simps(np.array(Pressure), np.array(Volume))
print(\'Area under P-V diagram = Work Done =\', Work_done, \'Joules\')


# Calculating engine power by using Power - 2pi*N*Tq
N = 1500 #RPM
Tq = Work_done

Power = 2*math.pi*N*Tq/(60*1000)
print(\'Power =\', Power, \'kW\')

#Specific fuel Consumption
TFC_per_stroke = 20e-9 #in kg 
TFC = TFC_per_stroke*N*60/2 # in kg/hr
SFC = TFC/Power #in kg/kWh

print(\'Specific Fuel Consumption =\', SFC, \'Kg/kWh\')

#end

Code Explanation:

  • Imported required modules for the code, like matplotlib, numpy and scipy.integrate from numpy.
  • In order to work with the file it should be in the same folder as the code.
  • To check file compatibility used try and except function, which means if a non-compatible file is provided it will show an error saying \'file not found, please provide a valid Converge file\'.
  • Renamed file name as ‘data’ in order to make it easy to call to when required.
  • For parsing the file and data visualization we used for loop function, we used ‘open’ command to open the file and ‘r’ mode to read the lines in data.
  • Using if and else function we read lines which do not contain ‘#’
  • Created empty arrays for storing the data for values of column names and their units, now with respect which line they are stored in we appended the them to get an array of column title and units which can be used in order to plot values of any two columns.
  • Then we created function for X and Y, we can enter required column number to create plot for the same, we ‘int’ function as it return integer function and later we subsctracte the input from ‘1’ to read the exact column we need to create plot for.
  • Created empty arrays names as X_column and Y_column for X and Y axis of the plot respectively, later again using ‘open’ function and ‘r’ mode we read the file and appended the values X_column and Y_column with line function that we have created with respective column number, we will be able to read the same.
  • In order to get the desired plot with exact labels and units we used the array created for column title and units. We used [0] and [‘respective column number] as values of column titles and units is stored inside a list so we have to have to open that list first and then read the required data.
  • Once the plot is created we can use the same command as above to save the file with respective column name.
  • In order to find area under the P-V diagram we first used same function as above to ead the file and create array for values of Volume and pressure, once done, we multiplied value of Pressure with to convert it into “Mpa” from “Pascale”.
  • Using formula “∫pdV” and “simps” module we found the value of Work Done, which is same as area under the p-V diagram.
  • After that using formula for power i.e. we found Power. With N given as 1500RPM and T= Torque = Work Done.
  • With provided values of fuel consumption per stroke we could find the “Total Fuel consumption(TFC)” and with formula “TFC/Power” we got the required value of” Specific fuel Consumption in Kg/kWh”.

OOUTPUT: Output from the code, and values of Area under P-V Diagram, Power and Specific Fuel Consumption are shown below.

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.