DATA ANALYSIS USING PYTHON
AIM
To write a program to create a visualizer tool which will read the given data and required information is taken from the data file
PROGRAM :
import matplotlib.pyplot as plt
import math
import numpy as np
import sys
#compatibility check
try :
file = open(\'engine_data.out\',\'r\')
except :
print(\'\"file not recognised.Please provide CONVERGE output file\"\')
sys.exit()
#defining header file
file = \'engine_data.out\'
row_ct = 0
column_ct =0
ct_header =0
column_title =[]
column_unit =[]
for line in open(file,\'r\'):
if \'#\' not in line:
row_ct = row_ct+1
column_ct=len(str.split(line))
else:
ct_header = ct_header+1
if ct_header == 3:
line_content = str.split(line)
for i in range(1,len(line_content)):
column_title.append((line_content[i]))
elif ct_header ==4:
line_content = str.split(line)
for j in range(1,len(line_content)):
column_unit.append((line_content[j]))
c=1
for c in range(1,17):
c=c+1
a=8
b=2
x_axis_data =[]
y_axis_data =[]
for i in open(file,\'r\'):
if \'#\' not in i:
x =i.split()
x_axis_data.append(float(x[a-1]))
for i in open(file,\'r\'):
if \'#\' not in i:
y =i.split()
y_axis_data.append(float(y[b-1]))
plt.plot(x_axis_data,y_axis_data)
plt.xlabel(column_title[a-1]+column_unit[a-1])
plt.ylabel(column_title[b-1]+column_unit[b-1])
plt.show()
#callculating area
area = np.trapz(y_axis_data,x_axis_data)
print(\"the area under p-v is\",area)
workdone = area*pow(10,6)
n=1500
power = 2*math.pi*n*workdone/(60)
print(\"the power is\",power)
fuel =20*pow(10,-6)
sfc = 3600*fuel*n/(60*power)
print(\"specific fuel consumption is\",sfc)
EXPLANATION
- We have to write a code which takes as input a particular data and read the data.
- Our code should take the column numbers has input as input and should give the values as output
- The code which we write should not take other data as input. It should print such as file not recognized
- First, we should take header files such as title and the units of the column. \'if\' is used for reading the title and units
- \'for loop\' is used for opening the file which we are going to open
- A for loop is created for the range of 17 columns and counter is coded for the increment
- Our main aim is to plot the two columns with giving the column nos as input So that variable a and b are assigned
- Two empty arrays are created for the x-axis data and y-axis data and using the append command the values are stored in the respective arrays
- Plot the x-axis data and the y-axis data and label are given as we already found the column titles and column units
- Then using the np.trapz command find the area and print it.
- Then find the power and the specific fuel consumption using the formulae and print them
PLOT FOR THE PRESSURE AND THE VOLUME
PLOT FOR CRANK AND MIN TEMPERATURE
COMPATIBILITY CHECK
Only the particular data file should open, other than this file should not be opened. It should print something like the file is not recognized
CALCULATED VALUES:
RESULT
Thus the given data was read and the required information from the file was calculated.