Project

File Parsing in Python

Aim:

For this challenge, you will be create a data visualizer tool. Your task is to write a script that does the following.

Data visualizer:

Your 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. THIS SHOULD NOT BE DONE MANUALLY.

Compatability check:

You 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 performance calculation:

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

CODE:

"""
File parsing in python
"""

import matplotlib.pyplot as plt
import numpy as np 
from numpy import trapz as trp

#Compatability check
try:
  f=open('engine_data.out','r')

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

#starting file parsing
file = 'engine_data.out'
count_rows = 0
count_columns = 0
count_header=0
column_title = []
column_units = []

for line in open(file,'r'):
	if '#' not in line:
		count_rows = count_rows + 1
		count_columns = len(str.split(line))
	else:
		count_header = count_header +1

		if count_header == 3:
			for i in range(0,len(str.split(line))):
				column_title.append(str.split(line)[i])

		elif count_header == 4:
			line_content = str.split(line)
			for i in range(0,len(str.split(line))):
				column_units.append(str.split(line)[i])

#Compatability check

if (count_header < 4 or count_rows < 1 or count_columns < 8):
	print('File not recognized. Please provide a valid CONVERGE output file')

else:
	#Plotting of all columns

 	for i in range(0,count_columns):
 		data_array=[]
 		for j in open(file,'r'):
 			if '#' not in j:
 				data_array.append(float(j.split()[i]))

 		plt.plot(data_array)
 		plt.ylabel(column_title[i+1] + column_units[i+1])
 		plt.savefig(column_title[i+1]+'.png')
 		plt.figure()

 		del data_array

 	#Plotting of given data columns

 	x_axis_column_num=8
 	y_axis_column_num=2
 	x_array=[]
 	y_array=[]


 	for k in open(file,'r'):
 		if '#' not in k:
 			y_array.append(float(k.split()[y_axis_column_num-1]))
 			x_array.append(float(k.split()[x_axis_column_num-1]))

 	plt.plot(x_array,y_array)
 	plt.xlabel(column_title[x_axis_column_num]+column_units[x_axis_column_num])
 	plt.ylabel(column_title[y_axis_column_num]+column_units[y_axis_column_num])
 	plt.savefig(column_title[x_axis_column_num] +' VS '+ column_title[y_axis_column_num]+'.png')
 	plt.figure()

    #Calculating area under the pv curve
 	d = x_array[1] - x_array[0]
 	work = []  
 	for i in range(0,len(x_array), 2):
 		work.append((y_array[i]*2*d))
 	Area = 0
 	for w in work:
 	    Area = Area + w 
 	print('Area under PV diagram:', Area, 'MJ')


    #Calculating power output of the engine
 	RPM = 1500
 	power = Area * 1e+06*RPM/(9550)
 	print('Power Output of the Engine:',power,'kw')

 	#Calculatig specific fuel consumption
 	fuel_per_stroke = 20*1e-06
 	fuel_per_sec = fuel_per_stroke*RPM*60
 	SFC = fuel_per_sec/ (power * 1e+03)
 	print('Specific Fuel Consumption:',SFC,'g/J')

Output:

PV Diagram:

Other Graphs:

Explanation:

Parse means to ''resolve (a sentence) into its component parts and describe their syntactic roles''. In computing, parsing is 'an act of parsing a string or a text'. [Google Dictionary]File parsing in computer language means to give a meaning to the characters of a text file as per the formal grammar. A parser is a program that parses the text files.

In this challenge, we first open the output data file then we calculate the total numbers of columns and rows.

In that file first 5 rows is the header of the file. Row 3 and 4 having engine parameters title and its units.

Remaining rows contains values of engine parameters. First we read the row 3 and split the titles separately and stored in column_titles.

Similarly, read the row 4 and split the units and stored in column_units.

Here, we checking the compatability of file, if the file is not compatable it shows like this "File not recognized. Please provide a valid CONVERGE output file".

Now, we read the egine parameters values and separate each columns and stored in data_array. Then plot the data_array and label them from column_titles(Not using manually).

Similarly delete the data_array and stored the second column and plot the data_array, this is repeated for all the columns.

The plotting of all the columns is stored in file as shown in the output.

Next, we plot the graph between column 8 and column 2. So, split the each lines and stored only column 8 and column 2 values in two separate arrays.

Now, plot the two arrays and label from column_titles and column_units instead of labelling manually.

Finally calculate the area under pv diagram, power output and specific fuel consumption of the engine as shown in the coding.

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.