DATA PARSING (using PYTHON)
AIM:
To create a data visualizer by parsing a given CONVERGE output file using PYTHON
OBJECTIVES:
- To understand the concept of data parsing.
- To practice data parsing and doing all stuffs of calculations, plotting and other operations on the given data.
INTRODUCTION:
The process of "parsing" can be described as reading in a data stream of some sort and building an in-memory model or representation of the semantic content of that data, in order to facilitate performing some kind of transformation on the data.
‘File parsing’ is very important as we do not always get sets of data in computational language. So, to convert the set of data into some format that the computer can understand, it is highly essential.
Calculation of area under P-V curve:
The area under P-V curve was calculated using the Trapezoidal rule. The area under the curve was broken into small elemental trapeziums and the area of all the trapeziums are calculated for getting the area under the curve. This method more or less gives the accurate results.
Power output calculation:
Power = (Work Done).(Work-Stroke)/(second)
where (Work-Stroke)/(second) = RPM/120
Specific Fuel Consumption calculation:
Brake-specific fuel consumption (BSFC) is a measure of the fuel efficiency of any prime mover that burns fuel and produces rotational or shaft power. It is typically used for comparing the efficiency of internal combustion engines with a shaft output.
`F_(sp)` = (Fuel per second)/(Power)
Fuel per second = (Fuel)/(stroke)
PROCEDURE OF COMPLETION:
- The necessary modules i.e. numpy and matplotlib were imported.
- Using the 'try: ' and 'except: ' block, data compatability condition was initialized.
- Then, a loop was run for storing the column headers in a list and thereafter, another loop was run for storing all the values under their columns respectively.
- Then, plotting and saving of each column was done respectively.
- Then, user-input for desired column no. and plotting and calculation was done.
- The area under the P-V graph was calculated using the trapz(x, y) function from the numpy module.
The following code can be seen for completing the task:
# DATA PARSING
# importing modules
import numpy as np
import matplotlib.pyplot as plt
# checking data compatibility
try:
with open('engine_data.out') as file:
read_data = file.read()
except:
print('File not recognized. Please provide a valid CONVERGE output file.')
exit()
# initialization of parameters
head_count = 0
line_count = 0
col_name = []
# naming the columns
for line in open('engine_data.out', 'r'):
if '#' in line:
head_count = head_count+1
if head_count == 3:
col = line.split()
for i in range(0, len(col)):
col_name.append(col[i])
col_num = i
else:
line_count = line_count + 1
columns = len(line.split())
# entering the values into the columns
values = np.zeros((line_count, columns))
row = 0
for line in open('engine_data.out', 'r'):
if '#' not in line:
column = 0
value = line.split()
while column < columns:
values[row, column] = value[column]
column = column + 1
row = row + 1
# plotting each and every column and saving separately
col_num = 1
while col_num <= columns:
plt.plot(values[:, col_num-1])
plt.ylabel(col_name[col_num])
plt.title(col_name[col_num])
plt.savefig(col_name[col_num] + '.png')
plt.figure()
plt.grid()
col_num = col_num + 1
# user input for specific graph between two parameters
x_value = int(input('Enter the column no. you want in x-axis: '))
y_value = int(input('Enter the column no. you want in y-axis: '))
print('The following parameters are calculated from the above input: ')
print('The parameters chosen are: 1. ' + col_name[x_value] + ' 2. ' + col_name[y_value])
# plotting
plt.grid()
x_values = values[:, x_value-1]
y_values = values[:, y_value-1]
plt.plot(x_values, y_values)
plt.xlabel(col_name[x_value])
plt.ylabel(col_name[y_value])
plt.grid()
plt.title(col_name[x_value] + 'Vs' + col_name[y_value])
plt.savefig(col_name[x_value] + ' Vs ' + col_name[y_value])
# calculation of parameters if user chooses volume and pressure
if x_value == 8 and y_value == 2:
work_done = abs(np.trapz(x_values, y_values))
rpm = 1500
work_stroke_sec = rpm / 120
power = work_done*work_stroke_sec
fuel_stroke = 20*1e-6
f_sec = fuel_stroke*rpm*60
sfc = f_sec/(power*1e+03)
print('Area under P-V diagram: ', work_done, 'MJ')
print('Power output of the figure: ', power, 'kW')
print('Specific Fuel Consumption for 20 micro grams of fuel: ', sfc, 'gm/kW.hr')
else:
pass
OBSERVATIONS:
The folder containing all the files with the respective curves is shown below:
The output for the P-V graph is as follows:
The graph under P-V is as follows:
Some additional graphs that were plotted according to their datas and their numbers are as follows:
CONCLUSION:
File parsing is an important tool of every programming language as it enables us to convert a human readable language to a computer readable language which helps us in computations and other functionings.