Week 6 - Data analysis
DATA ANALYSIS USING PYTHON
OBJECTIVES:-
1. Program 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.
2. Code should exit gracefully, if a non-compatible file is provided as an input. It should say something like "File not recognized. Please provide a valid CONVERGE output file".
3. 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.
THEORY:-
What is Data Analysis?
Data analysis is a process of inspecting, cleansing, transforming and modeling data with the goal of discovering useful information, informing conclusions and supporting decision-making. Data analysis has multiple facets and approaches, encompassing diverse techniques under a variety of names, and is used in different business, science, and social science domains. In today's business world, data analysis plays a role in making decisions more scientific and helping businesses operate more effectively.
There are several types of data analysis techniques that exist based on business and technology. The major types of data analysis are:
- Text Analysis
- Statistical Analysis
- Diagnostic Analysis
- Predictive Analysis
- Prescriptive Analysis
Data Analysis Process is nothing but gathering information by using proper application or tool which allows you to explore the data and find a pattern in it. Based on that, you can take decisions, or you can get ultimate conclusions. Data Analysis consists of the following phases:
- Data Requirement Gathering
- Data Collection
- Data Cleaning
- Data Analysis
- Data Interpretation
- Data Visualization
FORMULAS USED:-
1. Power Output of Engine
2. Specific Fuel Consumption
PROGRAM:-
"""
Program for Data Analysis
Program by Saurabh Kshirsagar
"""
#importing libraries
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import simps
#Input from user to open a required data file
print("Insert name of the file")
file = input("File Name : ")
#---Compatibility check---
try:
filename = open(file,'r')
except FileNotFoundError:
print("File not recognized. Please provide a valid CONVERGE output file")
#---Data Visualizer---
#Data separating from given file
comment = []
data = []
for i in filename:
if '#' in i:
comment.append(i.split())
else:
data.append(i.split())
#Assigning column with number
print("1-Crank 2-Pressure 3-Max_Pres 4-Min_Pres 5-Mean_Temp 6-Max_Temp 7-Min_Temp 8-Volume 9-Mass 10-Density 11-Integrated_HR 12-HR_Rate")
#Storing and Calculating Data for plot
x_variable = []
y_variable = []
x_parameter = input("Input column number for X axis : ")
y_parameter = input("Input column number for Y axis : ")
x = int(x_parameter)
y = int(y_parameter)
count = 1
for i in range(len(data)):
column_name_x = comment[2][x]
column_name_y = comment[2][y]
x_cor = data[i][x-1]
y_cor = data[i][y-1]
x_variable.append(float(x_cor))
y_variable.append(float(y_cor))
count = count+1
#Plotting
plt.plot(x_variable,y_variable)
plt.xlabel(column_name_x)
plt.ylabel(column_name_y)
#Saving the plot
plt.savefig(column_name_x + 'vs' + column_name_y + '.png')
plt.show()
#---Basic Performance Calculation---
#Calculating Area of plot
Area = float(simps(y_variable, x_variable, even = 'first')*pow(10,6))
print(' Area: {}' .format(Area))
#Calculating power output and specific fuel consumption
if x==8 and y==2:
rpm = 1500
speed = rpm/120
power = Area*speed
print('Power (Watt): {} ' .format(power))
Fuel_consumed = rpm*60*20*pow(10,-9)/2
specific_fuel_consumption = Fuel_consumed/(power/1000)
print('Specific Fuel Consumption (kg/KWhr): {}' .format(specific_fuel_consumption))
EXPLANATION:-
- We import math , matplotlib, numpy and scipy libraries to execute this program.
- Then we give instructions for input of the file using 'print' and take file input from the user using 'input'.
- We use try and except command for compatibility checking of the file.
- If input matches the file it reads it and stores in filename and if the input does not matches the file it will display "File not recognized. Please provide a valid CONVERGE output file".
- This is known as compatibility checking.
- Then we define two empty arrays 'comment' and 'data' for storing the values.
- We use for loop for separating and storing the comment and data from the file.
- If there is '#' in the line it stores it into comment array else it stores remaining in data array.
- We use 'print' for printing the column number of the parameter. It helps us in understanding which column contains which parameter.
- We define two empty arrays 'x_variable' and 'y_variable' for storing the x and y cocordinates respectively.
- We use 'input' function for getting input for two columns from user.
- We convert the input values into integers using 'int'.
- We use for loop for storing the values of x and y axis.
- We use matplotlib to plot the curves for parameters of inputed number of columns.
- Then we use plt.savefig command to save the figure automatically.
- We write a command to calculate the area under and curve and use 'print' to print the output.
- We use if x== and y==2 for performing calculation only for PV diagram.
- We define the inputs rpm, speed, workdone, etc.
- We used above mentioned formulas to calculate Power and Specific fuel consumption and print them.
OUTPUT:-
1. Plots obtained after giving column number as inputs.
2. Files automatically saving with name of column
3. Compatibility Check
4. Area Under PV diagram, Power Output of Engine and Specific Fuel Consumption
PV Diagram:-
Output:-
CONCLUSION:-
1. Using this program we can plot the graphs between two parameters by entering their column number.
2. It gives area under the curve.
3. It shows Power output and specific fuel consumption only in case of PV diagram.