Challenge

Data analysis using Python

Aim – Data analysis and File parsing using Python

Objective –

  1. To write a code for parsing the data file for data visualization.
  2. To perform compatibility check.
  3. To solve basic performance calculations.

Theory –

File Parsing –Parsing means to convert data in a certain format into a more usable format. Parsing refers to the way that human beings analyze a sentence or phrase (in spoken language or text) "in terms of grammatical constituents, identifying the parts of speech, syntactic relations, etc. Why do we even need to parse files? In an imaginary world where all data existed in the same format, one could expect all programs to input and output that data. There would be no need to parse files. However, we live in a world where there is a wide variety of data formats. Some data formats are better suited to different applications. An individual program can only be expected to cater for a selection of these data formats. So, inevitably there is a need to convert data from one format to another for consumption by different programs. Sometimes data is not even in a standard format which makes things a little harder.

Problem statement –

To write a python script which takes column number as input and plot the respective column as separate images. The plot labels should be extracted from the file automatically.

To perform compatibility check on the written code.

To calculate the area under the P-V diagram, power output assuming engine speed as 1500 rpm and to calculate the specific fuel consumption when engine is consuming 20 micro-grams of fuel per stroke-cycle.

Code -

import numpy as np
import matplotlib.pyplot as plt 

# Initialising variables
crank = []
Pressure = []
P_max = []
P_min = []
T_mean = []
T_max = []
T_min = []   
volume = []
mass = []
density = []
integrated_hr = []
hr_rate = []
cp = []
cv = []
gamma = []
kin_vis = []
dyn_vis = []


# To inout the file name
file = input('enter file name: ')

try:
 	data_file = open(file,'r')
 	
 	ct=1
 	for Line in data_file:
		if ct == 3:
 			Columns=Line.split()
		if ct == 4:
			Units=Line.split()
		ct = ct+1
 	
		if '#' not in Line:
 			crank.append(float(Line.split()[0]))     
 			Pressure.append(float(Line.split()[1]))
 			P_max.append(float(Line.split()[2]))
 			P_min.append(float(Line.split()[3]))
 			T_mean.append(float(Line.split()[4]))
 			T_max.append(float(Line.split()[5]))
 			T_min.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]))
 			cp.append(float(Line.split()[12]))
 			cv.append(float(Line.split()[13]))
 			gamma.append(float(Line.split()[14]))
 			kin_vis.append(float(Line.split()[15]))
 			dyn_vis.append(float(Line.split()[16]))

 	prop = np.array([[0],crank,Pressure,P_max,P_min,T_mean,T_max,T_min,volume,mass,density,integrated_hr,hr_rate,cp,cv,gamma,kin_vis,dyn_vis])


	# Printing the different columns 
	for i in range(1,17):
  		filename=str(Columns[i])+str(Units[i])
  		print(filename)

	# Input the value of column numbers
	x = int(input('Enter the column number for x axis: '))
	y = int(input('Enter the column mumber for y-axis: '))

	# Plotting
	plt.plot(prop[x],prop[y])
	plt.title(str(Columns[x])+  ' ' + 'vs' + ' '  + str(prop[y]))
	plt.xlabel(str(Columns[x]) + ' ' + str(Units[x]))
	plt.ylabel(str(Columns[y]) + ' ' + str(Units[y]))
	plt.savefig(str(Columns[x]) + ' ' + 'vs' + ' ' + str(Columns[y]))
	plt.show()
	
	# Area under P-V diagram
	area = np.trapz(prop[2],prop[8])
	work_done = area*1000
	print('Area of the PV diagram : ',work_done,'KJ')
	
	# Power developed by the engine
	rpm = 1500
	power = (rpm/2)*work_done/60
	print('Power : ',power,'kW')
	
	# Specific fuel consumption
	SFC=20*pow(10,-6)*3600*(rpm/60)/power
	print('SFC : ',SFC,'g/kWh')
except:
 	print('File not recognized. Please provide a valid CONVERGE output file')

Explanation -

  1. First of all, we import the necessary modules for the program.
  2. Then we initiate the different columns as empty arrays which will get filled late using the ‘append’ command.
  3. Now we use the try and except commands to check the compatibility of the file. If the file is compatible, our program will run and end smoothly and if the file is not compatible, then it will show that the file not recognized.
  4. Now we open the file in reading mode. Our basic motive is to get all the data of each column separately. For this, we need to use a counter variable (ct) and if statement “If '#' not in line”, we need to split each line using the delimiter ' '. Now this split command will convert this data in the form of strings which we can later use for naming and plotting purposes.
  5. We can use the float command to convert these strings into floating-point numbers and also we need to use append command so that every time the loop iterates the data gets stored
    in the arrays.
  6. Now to plot the graphs, we first need to create an array of the various columns and then just provide the column number's of the data that we need to plot. Then we can use the ‘savefig’ to save this plot
  7. Now to calculate the area under the PV diagram only, we need to import trapz from numpy to calculate the area under the graph by giving it inputs. This way we can get the area for only the p-v diagram.
  8. After that, we can calculate the power and Specific Fuel Consumption using the formulas.

Result -

1. Engine performance calculations -

Work done by the engine is 0.500587 KJ.

Power developed by the engine is 6.25733 KW @ 1500 rpm.

Specific fuel consumption is 0.143831 g/kWh.

2. PV Diagram -

3. Crank vs PV diagram -

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.