Data Parsing and Analysis using Python
AIM: To create a scipt for the following operations
- Data visualizer
The 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
- 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.
PROCEDURE:
- Import the neccesay modules.
- Create empty list for arrays to be appended.
- use open file and input command to enable the suer to input the required file name and open it for parsing
- Use try and except command to check if the file is compatible
- Afte the file is opened in reading mode, We need to use a counter variable for line count.
- using if statement we can seperate the lines containing # and lines not containing # and using ' ' as delimiter we can seperate the words on each line
- The split command command will convert data into the form of strings.
- Use float command to convert these strings into floating-point numbers and then append them to the respective list.
- Use this command in a for loop to perform the same task for each line.
- Create an aray of various columns with respective to its columns numbers.
- Using for loop specify different filename for each columns specified by the user.
- Again use input comand such that the user can specify the columns they want to use for plotting.
- Create the plots for Columns[x] and Columns[y] with respective to the user's desire.
- After the plotting is done and saved, We can use these data to find the area under the graph.
- For this, we need to import trapz from numpy to perform the calculation.
- From the value of area and the given parameters we can calculate the power and specific fuel consumption.
- NOTE: We need to use SublimeREPL to run the command because input command does not work in sublime BUILD.
CODE:
import math
import numpy as np
import matplotlib.pyplot as plt
#Defining the empty arrays
Crank=[]
Pressure=[]
Max_Pressure=[]
Min_Pressure=[]
Mean_Temp=[]
Max_Temp=[]
Min_Temp=[]
Volume=[]
Mass=[]
Density=[]
Integrated_HR= []
HR_Rate=[]
C_p=[]
C_v=[]
Gamma=[]
Kin_Visc=[]
Dyn_Visc=[]
#Enabling the user to enter the required filename
file=input('Enter the name of the input file: \n')
print(file)
#Command to open the file
try:
file_data=open(file,'r')
print('\n Data file is successfully opened')
except filenotfounderror:
print('\n Please enter a valid file name')
#Reading the data from the data file by each line using the for loop
#Initialising a counter variable
line_count=1
for line in file_data:
if line_count==3:
columns=line.split()
if line_count==4:
units=line.split()
line_count=line_count+1
if '#' not in line:
#Appending the data into the empty arrays
Crank.append(float(line.split()[0]))
Pressure.append(float(line.split()[1]))
Max_Pressure.append(float(line.split()[2]))
Min_Pressure.append(float(line.split()[3]))
Mean_Temp.append(float(line.split()[4]))
Max_Temp.append(float(line.split()[5]))
Min_Temp.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]))
C_p.append(float(line.split()[12]))
C_v.append(float(line.split()[13]))
Gamma.append(float(line.split()[14]))
Kin_Visc.append(float(line.split()[15]))
Dyn_Visc.append(float(line.split()[16]))
#Assigning the column numbers for each column
print("Please select the columns no. from the list below:")
for i in range(1,17+1):
filename=str(columns[i])+str(units[i])
print(filename)
Columns=np.array([[0],Crank,Pressure,Max_Pressure,Min_Pressure,Mean_Temp,Max_Temp,Min_Temp,Volume,Mass,Density,Integrated_HR,HR_Rate,C_p,C_v,Gamma,Kin_Visc,Dyn_Visc])
#Enabling the user to input value fo column numbers
x=int(input('Enter the column number you want on x-axis: '))
y=int(input('Enter the column number you want on y-axis: '))
#Plotting the graph with respective to the columns values
plt.plot(Columns[x],Columns[y])
#Specifying varying title for each plot
plt.title(str(columns[x])+' '+'VS'+' '+str(columns[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()
#For finding the Area under the graph, Power output and Specific Fuel Consumption
if x==8 and y==2:
Area=np.trapz(Columns[x],Columns[y])
Workdone= Area*1000
RPM=1500
Power= 2*math.pi*RPM*Workdone/60
#To calcualte Specific Fuel Consumption use the formula BSFC= Fuel_Consumption/power
Fuel_per_stroke=20*pow(10,-6)
Fuel_per_sec=Fuel_per_stroke*RPM*(0.5/60)
Fuel_per_hour=Fuel_per_sec*3600
BSFC=Fuel_per_hour/Power
print('Area under the PV-graph: ',Area)
print('Workdone: ',Workdone,'KJ')
print('Power Output of the Engine: ',Power,'KW')
print('Specific Fuel Consumption: ',BSFC,'g/KWh')
OUTPUT:
#Compatibility Check
RESULT
#IDLE
#SublimeREPL
PLOTS:
CALCULATION OUTPUT:
Area under the PV-graph: -0.0002590377238248494
Workdone: -0.2590377238248494 KJ
Power Output of the Engine: -40.68955050853843 KW
Specific Fuel Consumption: -0.022118700962576152 g/KWh