Challenge

Week 6 - Data analysis

Objective: To write a Python program that imports a data file entered by the user and perform the following operations:

  1. Plot a graph between the column number entered by the user.
  2. Check if the file is compatible with the code or not
  3. Perform the basic performance calculations such as work output, power output and specific fuel consumption

Solution procedure:

  1. Imported matplotlib and Numpy modules.
  2. The user is asked to enter the filename which I want to import using the input function. If the filename matches engine_data.out then the program will execute otherwise it will stop and give an error message.
  3. The file is read with the help of read_file() function and the values it contains is stored into a list data.This function read each and every line of the file and depends it into the list data
  4. The compatibility of the file is checked by finding whether CONVERGE is present in the first line of the file or not. If it is present then the file is compatible otherwise it will show an error message and the code exit.
  5. After that some lists are defined for storing the column number, column heading and column units. For storing the values a for loop is used which adds all other values except “#” sign.
  6. For taking the input from the user the available number of columns are displayed on the screen and the user is asked to input the column numbers of which he wants to plot the graph.1 is subtracted from the input to convert it into the index value.
  7. After that the values under the column number input by the user are stored in the list X and Y and a graph is plotted between them using the plot() command and saved the plot using savefig() by the name of the column.The respective X and Y labels are also added using xlabel() and ylabel() command.
  8. For calculating the area under PV diagram the values under the pressure and volume columns are stored into their respective lists.The units of pressure values are converted from MPa to Pa using np.array()
  9. The area under PV diagram all the work done is calculated using a function trapz() under the numpy module which integrates along a given axis using composite trapezoidal rule. Here, pressure values are integrated with respect to the x axis representing volume.
  10. The power developed by the engine is equal to the product of the work done and the speed with which the shaft is rotating.In the problem the speed is given as 1500 RPM for 1500 / 60 RPS.
  11. The specific fuel consumption is equal to the mass of the fuel divided by power output. it is represented in the units gram/kWh.The mass of the fuel is given to be 20 micrograms fore stroke. therefore for a two stroke engine the total mass of the fuel consumed would be 40 micrograms.
import matplotlib.pyplot as plt
import numpy as np

# Checking and inporting the file
name = str(input("Enter the file name WITH extension that has to be opened: "))
if name == "engine_data.out":
    print("File Opened")
else:
    print("File not found. Enter valid file name")
    exit()

data = []                                 # to store rows of file
# function for reading the file
def read_file():
    file = []
    for line in open(name, 'r'):
        values = line.split()
        file.append(values)
    return file
data = read_file()

# Checking Compatibility
if "CONVERGE" in data[0]:
    print("File is compatible.You can proceed")
else:
    print("File is not compatible.Enter compatible file name")
    exit()
    
# defining arrays for storing column number,heading and units
no_cols = []
col_heading = []
col_unit = []
data[1].remove("column")

# for loop for appending column number and ignoring "#" 
for i in data[1]:
    if i is not "#":
        no_cols.append(i)
# for loop for appending column heading and ignoring "#" 
for i in data[2]:
    if "#" not in i:
        col_heading.append(i)
# for loop for appending column unit and ignoring "#" 
for i in data[3]:
    if "#"  not in i:
        col_unit.append(i)
        
# printing the available columns
print("Available columns")
for i in range(len(no_cols)):
    print(no_cols[i],col_heading[i],col_unit[i])

# Taking user input 
x = int(input("Enter first column no.(for X-axis): "))-1
y = int(input("Enter second column no.(for Y-axis): "))-1

X = []
Y = []
for i in range(5,len(data)):    # range starts from 5 because the values of different parameters start from row 6
    X.append(float(data[i][x]))
    Y.append(float(data[i][y]))

# plotting the respective column graph
filename = str(no_cols[x]) + "&" + str(no_cols[y]) + ".png"
plt.plot(X,Y,linewidth = 2)
plt.xlabel(col_heading[x] + col_unit[x])
plt.ylabel(col_heading[y] + col_unit[y])
plt.savefig(filename)        # to save the figure by the column name
plt.show()

# calculating area under pv diagram
p = []
v = []
rps = 1500/60                      # engine speed in revolution per secont
for i in range(5,len(data)):
    p.append(float(data[i][1]))
    v.append(float(data[i][7]))

volume = np.array(v)               # converting list to array so that any operation can be performed on each element
pressure = np.array(p)*pow(10,6)   # for converting MPa to Pa; 1MPa = 10^6 Pa
area = np.trapz(pressure,volume)   # numpy.trapx() is a function which integrates along the given axis
# calculating power
power = area*rps/1000
print("Work Output = ", area,"Joules")
print("Power Output of an engine running at a speed of 1500rpm = ",power,"kW")

m = 20* pow(10,-6)     # mass of fuel in grams consumed per stroke
M = m*2                # mass of fuel in grams consumed in 2 stokes(assuming 2 stroke engine)
sp_fuel = M*3600/power
print("Specific Fuel Consumption= ", sp_fuel,"g/kWh")

Results and Conclusions:

  1. From the first picture it is clear that if the file name is wrong then the program will not execute.
  2. The second picture proves the compatibility test of the file i.e. the program will execute only when the file would be compatible.
  3. Third picture and the graph shows that we get a plot for the column number that the user enters. It also displays the basic performance calculations of the engine such as work output power, output and specific fuel consumption.

Hence, the program is ready for data analysis purposes.

1st picture

2nd pictures

when CONVERGE is removed from the first line, an error message is displayed.

3rd pictures

Plot

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.