"""
demonstrating file parsing in python
"""
import matplotlib.pyplot as plt
import numpy as np
# input
line_count = 1
data1 = []
data2 = []
no = []
cool = []
units = []
pressure = []
volume = []
#checking file name
filename = input("enter file name:")
if filename =='engine_data.out':
print('file found successfully')
else:
print('file not recognized')
exit()
print('--------------------------------')
d = []
for line in open(filename):
if '#' in line:
d.append(line.split())
nc = len(d[1])
b = d[1].index('column')
for i in range(b,nc-1):
no.append(i)
for i in d[2]:
if i is not '#':
cool.append(i)
for i in d[3]:
if i is not '#':
units.append(i)
print('showing data present in file')
print( 'column',' ','parameter')
for i in range(0,len(no)):
print(' ',no[i],' - ',cool[i],units[i])
print('--------------------------------')
line_count = line_count + 1
x = input('input_data1')
y = input('input_data2')
x = int(x)
y = int(y)
for line in open('engine_data.out'):
if '#' not in line:
a = line.split()
data1.append(float(a[x-1]))
data2.append(float(a[y-1]))
pressure.append(float(line.split()[1]))
volume.append(float(line.split()[7]))
plt.plot(data1,data2,linewidth = 3)
plt.xlabel(cool[x-1] + units[x-1])
plt.ylabel(cool[y-1] + units[y-1])
plt.title(cool[y-1]+ '-' +cool[x-1] +' diagram')
plt.show()
print('--------------------------------')
#calculate area under(P-V) diagram
P = np.array(pressure)
V = np.array(volume)
area = np.trapz(P,V)
workdone = 1000000*area
print('area under (P-V) diagram =',area)
print('workdone by engine = ',workdone)
#calculate power output of engine
P = (workdone*(1500/(2*60)))/1000
print('power output of engine = ',P,'Kw')
#calculate specific fuel consumption
sfc = 20*pow(10,-6)*3600/P
print('specific fuel consumption = ',sfc,'g/Kw.h')
explanation =
1. Compatability check is done with help of the 'if and else' command.
2. using commands like 'int', 'input', 'float', 'plot' we are able to find out data of
parameters and also plot the graph too.
3. we calculate power, specific fuel consumption, area under the (P-V)diagram by using
their respective formula.
"""
demonstrating file parsing in python
"""
import matplotlib.pyplot as plt
import numpy as np
# input
line_count = 1
data1 = []
data2 = []
no = []
cool = []
units = []
pressure = []
volume = []
#checking file name
filename = input("enter file name:")
if filename =='engine_data.out':
print('file found successfully')
else:
print('file not recognized')
exit()
print('--------------------------------')
d = []
for line in open(filename):
if '#' in line:
d.append(line.split())
nc = len(d[1])
b = d[1].index('column')
for i in range(b,nc-1):
no.append(i)
for i in d[2]:
if i is not '#':
cool.append(i)
for i in d[3]:
if i is not '#':
units.append(i)
print('showing data present in file')
print( 'column',' ','parameter')
for i in range(0,len(no)):
print(' ',no[i],' - ',cool[i],units[i])
print('--------------------------------')
line_count = line_count + 1
x = input('input_data1')
y = input('input_data2')
x = int(x)
y = int(y)
for line in open('engine_data.out'):
if '#' not in line:
a = line.split()
data1.append(float(a[x-1]))
data2.append(float(a[y-1]))
pressure.append(float(line.split()[1]))
volume.append(float(line.split()[7]))
plt.plot(data1,data2,linewidth = 3)
plt.xlabel(cool[x-1] + units[x-1])
plt.ylabel(cool[y-1] + units[y-1])
plt.title(cool[y-1]+ '-' +cool[x-1] +' diagram')
plt.show()
print('--------------------------------')
#calculate area under(P-V) diagram
P = np.array(pressure)
V = np.array(volume)
area = np.trapz(P,V)
workdone = 1000000*area
print('area under (P-V) diagram =',area)
print('workdone by engine = ',workdone)
#calculate power output of engine
P = (workdone*(1500/(2*60)))/1000
print('power output of engine = ',P,'Kw')
#calculate specific fuel consumption
sfc = 20*pow(10,-6)*3600/P
print('specific fuel consumption = ',sfc,'g/Kw.h')
explanation =
1. compatibility check is done with help of the 'if and else' command.
2. using commands like 'int', 'input', 'float', 'plot' we are able to find out data of
parameters and also plot the graph too.
3. we calculate power, specific fuel consumption, area under the (P-V)diagram by using
their respective formula.