Challenge

Week 6 - Data analysis

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import math

#Compatibility Check
filename=input("Enter filename: ")
if filename[filename.find("."):]!=".out":
    print("File not recognized. Please provide a valid CONVERGE output file.")
    exit(0) #This line means, 'stop further execution of the program'.
# A try and except block is used when any statement within the 'try' block might throw an error. If it does, then instead of the execution halting, the control goes to the except block and executes the statements within it.
try:
    fr=open(filename,"r")
except:
    # The statements here will be execute if 'fr=open(filename,"r")' throws an error. This will mostly happen if the users enters the name of a file that doesn't exist.
    print(filename,"does not exist")
    exit(0)
    
#Data Analysis
data=fr.readlines()
columns=data[2].split()
units=data[3].split()
columns.pop(0)  #removing the '#' element from each list
units.pop(0)
print(columns)
print(units)
engine_data=[]  #Initially engine_data is an empty list
for i in range(5,len(data)):
    data[i]=data[i].lstrip()
    t=list(map(float, data[i].split())) #the map(func, list) takes every element of the list argument and applies the function func to it. Here, we're applying the float function to each line of data in the engine_data.out file
    # Therefore, we convert all the rows of data in the engine_data.out file into floating point numbers
    engine_data.append(t)   #After converting each line of data in the engine_data.out file to floating point numbres, we append them to the engine_data list.
engine_data=np.array(engine_data)   #engine_data is a standard 2D python list. Accessing each column in a python list is messy. Therefore, we convert hte list into a 2D numpy array. This makes accessing each column very easy.

col1=int(input("Enter column 1: ")) #same as test.py, we take input from the user.
col2=int(input("Enter column 2: "))
# In a numpy array, one can access each column using the square brackets notation.
# For example, if a is a numpy array, a[:,i] will give all rows of the ith column. a[i,:] will give all columns of the ith row.
plt.plot(engine_data[:,col2-1],engine_data[:,col1-1])
plt.xlabel(columns[col2-1]+" "+units[col2-1])
plt.ylabel(columns[col1-1]+" "+units[col1-1])
plt.savefig(columns[col2-1]+"_"+columns[col1-1])
plt.show()
p=[]

#Basic Performance Calculation
pressure=engine_data[:,1]
volume=engine_data[:,7]
#Area under P-V Diagram gives Work Done in the process
work=np.trapz((pressure*pow(10,6)),volume)
print("Work done is:",work,"J")
#Workdone*Speed(in RPS)=Power
power=work*(1500/60)
print("Power is:",power/1000,"kW")
#SFC=(Mass of Fuel*3600)/Power
mfuel=20*pow(10,-6)
sfc=(mfuel*3600)/power
print("Specific Fuel Consumption is:",sfc,"g/kWhr")

Error Outputs -

The first part of this code is used to check the compatibility of the file.If the filename entered does not have a ".out" extension, i.e, if it is not a valid CONVERGE output file, then an error message is displayed and code is exited. If the file name entered is not present, another error message is displayed. This code is written using the try and except statements.

In the Data Analysis part,each line of text from the file is read usong the readlines function.This is stored separately in another array called data[].The column names and units are extracted from this and stored in separate arrays using the split function.After this appropriate functions are used in a for loop to get the required data from the data[] array. The user then inputs the required column numbers which are to be extracted and displayed. A graph is then plotted using the columns,and the figure is saved.

Thermodynamic formulas are used to perform the performance calculations.Work done is area under the PV Diagram which is calculated using the trapz funtion in numpy module.It basically performs integration along a curve.The calculated values are displayed.

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.