Challenge

Data analysis using Python

Aim: Write script to do the data analysis using python

objectives:

  • Write the script for Data Visualiser ,Compatibility check , and basic performance calculation

procedure:

  • First we will ask user to input file that want to analyze.
  • Once user inputed filename we will check format of file if it is correct then program will proceed otherwise it will throw error.
  • Then we will check for data in file if data exist in file then program will proceed otherwise it will give you instuction that there is no data in file.
  • Next we will extract data column wise and stored it to array.
  • Then we will provide user the input map and ask user about do you want plot between two variable? if answer is yes then we will ask for no of variable to be plotted.then we will ask user do you want to save the plot and this loop will continue untill user input No.
  • This task is scripted with help while and for loop .
  • Next we will extract data of pressure and volume from array where we stored and intergrateb by using smips function which we import from scipy module.
  • We will ask user to input RPM and Fuel consumption and with the help of that we will calculation for power .

Main program:

"""
data analysis 
"""
import math
import matplotlib.pyplot as plt 
from scipy.integrate import simps
import numpy as np 

# input from user as filename

filename = input("nEnter the name of Converge output filename with extension(.out):")

# =================================================================================================
# compatibility check

try:

	f= open(filename)
	f.close()
except File_not_found_error:
	print('nThis is not a valid Converge output file.n')
	exit()

# checking if data is present in the converge output file
i=0

for line in open(filename):
	if '#' not in line:
		break
	i=i+1

j=0
for line in open(filename):
	j=j+1
	if  j>i:
		break
if j==i:
	print('n No data present in the file.n')
	exit()

#==================================================================================
# Data extraction
# Getting number of columb i.e number of variables 
i=0
for line in open(filename):
	if i ==2:
	# sorting unit of variables 	
		c= line.split() 
		i=0
		break
	if i==1:
		# sorting name of variables 
		b=line.split()
		i=i+1
	# sorting column numbers	
	if 'column' in line.split():
		a=line.split()
		i=1

a.remove("column")                 #removing column from list a
a.remove("#")                      # removing # from list a
a=np.array(a, dtype = np.int)      # Converting data to integer 
b.remove("#")                      # removing # from list b
c.remove("#")                      # removing # from list c
num_col = len(a)                   # Number of columns

#===============================================================================

# Getting Data 
num_data = 0
data = np.arrange(0,num_col)
for line in open(filename):
 	if '#' not in line:
 		d= line.split()                         #sorting current data line as list in d 
 		d= np.array(d, dtype = np.float32)      # Converting list data to float array
 		data= np.vstack([data,d])               # stacking the float array d in data matrix
 		num_data = num_data +1
 	data = np.delete(data,0,0)                  # Deleting the first row used to initilize the array


#==============================================================================================================
# Data visualizer

print('n================================================================')
print ('nnInput map')
for i in a:
	print ('n{} for {}'.format(i,b[i-1]))

x='n'

print('n=================================================================')
x=input("nnDo you want a plot between any two variables? (y/n):")
while(x.lower()=='y'):
	cn1 = input("nEnter the column number of variable along x- axis :")
	cn2 = input("nEnter the column number of variable along y- axis :")

	cn1 = int(cn1)
	cn2= int(cn2)

	plt.figure()
	plt.plot(data[:,cn1-1],data[:,cn2-1])
	plt.xlabel('{} {}'. format(b[cn1-1],c[cn1-1]))
	plt.ylabel('{} {}'. format(b[cn2-1],c[cn2-1]))

	z= "n"
	z= input ("nDO you want to save the plot? (y/n):")
	if (z.lower()=='y'):
		plt.savefig("{}-{}.png".format(b[cn2-1],b[cn1-1]))
	plt.show()
print('n================================================================')

x='n'
x = input("nnDo you want a single variabl plot? (y/n):")
while(x.lower()=='y'):
		cn = input("nEnter the column number of variable :")
		cn = int(cn)
		plt.figure()
		plt.plot(np.arrange(0,num_data),data[:,cn-1])
		plt.xlabel('Data point number')
		plt.ylabel('{} {} '.format (b[cn-1],c[cn-1]))

		z= "n"
		z= input("nDo you want to save the plot? (y/n):")
		if(z.lower()=='y'):
			plt.savefig("{}.png".format(b[cn-1]))
		plt.show()
		print('n=====================================================')
		x= input("nnDo you want any other single variable plot? (y/n):")
print('n====================================================================')



#  basic performance calculation

print('nPerformance of Engine')
b1 = [i.lower() for i in b]
presssure_column=b1.index('pressure')
volumn_column=b1.index('volume')
pressure = data[:,presssure_column]
volume = data [:,volume_column]

area=0  # araa under curve
area=simps(pressure,volume,even='first')*np.power(10,6)
RPM = input('nenter the RPM of Engine')
f = input ('nEnter the value of fuel consumption per cycle in micro-grams')
RPM=float(RPM)
f= float(f)
n= RPM/(2*60)
p=araa*n
f=(f*n*3600)/np.power(10,9)  # fuel consumption kg per hr
sfc = (f*1000)/p              # specific fuel consumption
print('nArea under the P-V curve i.e work done per cycle = {} J'.format(area))
print('npower output at {} RPM = {} W'.format(RPM,p))
print('nspecific fuel consumption = {} kg/kw.hr' .format(sfc))

Output:

Result: script to analyze the data file has been executed successfully.

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.