Challenge

Week 6 - Data analysis

DATA ANALYSIS USING PYTHON

AIM

In this challenge, we have to do data analysis for a given data using python and extract the data and graph that user want by REPL method.

REPL

REPL stands for read, Evaluate, Print, Loop. The REPL is how you interact with the Python Interpreter. Unlike running a file containing Python code, in the REPL you can type commands and instantly see the output printed out.

PROCEDURE

  • Import the import matplotlib.pyplot and import numpy module.

import matplotlib.pyplot as plt

import numpy as np

  • Then for the compatibility check we use try and except loop.

try:

open('engine_data.out')

print ('File open Successfully')

except:

print('File not recognized please provide a valid files')

  • Then open command is used to open the file that provided.

for line in open('engine_data.out'):

  • To read the column number, column name, column units I have used for and if else loop.
  • For plotting graph I have used repl by input command to get the data he wants and convert the values into an integer by int command.

First_column = input('Please enter the value for X-axis = ')

x_axis = (int(First_column)-1)

  • The x-axis and y-axis are stored in an array using append and by converting the string into float.

value_of_x_axis.append(float(line.split()[x_axis]))

  • Then plot the graph using plt.plot.
  • The area of p-v curve is defined by storing the values in null array by append command.
  • The area is calculated by using np.trapz command.
  • Then the power, number of cycles per second, and fuel consumption are calculated using the formulas given in the program.

No.of cycles = RPM/(2*60)

Power = No.of cycles * area

Specific fuel consumptions = fuel consumption per hour /power

PROGRAM

# File parsing in python

import math
import matplotlib.pyplot as plt 
import numpy as np 

# compability check
try:
	open('engine_data.out')
	print ('File open Successfully')
except:
	print('File not recognized please provide a valid files')

# Reading file data
line_count = 1
headline_count=1
#File Parsing
for line in open('engine_data.out'):
	if '#' not in line:
		line_count=+1
		column_count = len(line.split())
	else:
		headline_count=headline_count+1
		if headline_count==3:
			column_number=(line.split())
		if headline_count==4:
			column_name=(line.split())
		if headline_count==5:
			column_units=(line.split())

#print(column_count)
#print(column_number)
#print(column_name)
#print(column_units)

print('column parameters(1-17)','1:Crank,2:Pressure,3:Max_Pres,4:Min_Pres,5:Mean_Temp,6:Max_Temp,7:Min_Temp,8:Volume,9:Mass,10:Density,11:Integrated_HR,12:HR_Rate,13:C_p,14:C_v,15:Gamma,16:Kin_Visc,17:Dyn_Visc')

First_column = input('Please enter the value for X-axis = ')
x_axis = (int(First_column)-1)
Second_column = input('Please enter the value for y-axis = ')
y_axis = (int(Second_column)-1)

print('You are requested the graph between',column_name[(x_axis)+1],'vs',column_name[(y_axis)+1])

value_of_x_axis = []
value_of_y_axis = []

for line in open('engine_data.out'):
	if '#' not in line:
		value_of_x_axis.append(float(line.split()[x_axis]))
for line in open('engine_data.out'):
	if '#' not in line:
		value_of_y_axis.append(float(line.split()[y_axis]))

plt.plot(value_of_x_axis,value_of_y_axis)
plt.savefig(column_name[((x_axis)+1)]+'vs'+column_name[((y_axis)+1)]+'.png')
plt.xlabel(column_name[((x_axis)+1)]+column_units[((x_axis)+1)])
plt.ylabel(column_name[((y_axis)+1)]+column_units[((y_axis)+1)])
plt.show()


# For P-V diagram take volume and pressure null array

volume = []
pressure = []
for line in open('engine_data.out'):
	if '#' not in line:
		volume.append(float(line.split()[7]))
		pressure.append(float(line.split()[1]))

#area under the P-V  curve 
area = np.trapz(pressure,volume)
# Work done per cycle
work_done = area
print('Total work done per cycle =', work_done,'MJ')

#RPM = 1500
RPM = input(' enter the RPM = ')
RPM = int(RPM)

# NUMBER OF CYCLES PER SECOND FOR 4 STROKE ENGINE

n = RPM/(2*60)

#Power of the engine
Power = area*n
print('The power of the engine =',Power,'MW')

# Calculating fuel consumption 
fuel_consumption = input('Enter the fuel_consumption in micro gram = ')
fuel_consumption = int(fuel_consumption)
fuel_consumption_perhour = fuel_consumption*pow(10,-6)*RPM*60

specific_fuel_consumption = fuel_consumption_perhour/Power
print('specific_fuel_consumption = ',specific_fuel_consumption,'gram/MW.hr')

OUTPUT

CONCLUSION

Learned file parsing and data analysis using python and repl method.

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.