# -*- coding: utf-8 -*-
"""
@author: matias mamo
"""

import matplotlib.pyplot as plt
import numpy as np

# Measured values
heights = [0.69, 1.05, 1.42, 1.70, 2.07]
times   = [13*60 + 18, 8*60 + 18, 6*60 + 14, 5*60 + 22, 4*60 + 47]
mass    = 4.605-1.44

# Calculate mass flow
mass_flow = [mass/a for a in times]

# Quadratic fit
p = np.polyfit(mass_flow, heights, 2)
x2 = np.linspace(min(mass_flow), max(mass_flow), 10)
y2 = p[0]*x2**2 + p[1]*x2 + p[2]

# Linear fit
m, b = np.polyfit(mass_flow, heights, 1)
x = np.linspace(min(mass_flow), max(mass_flow), 10)
y = m*x+b

print("The D value is", m, "m*s/kg")

# Generate the graph
plt.cla()
plt.plot(mass_flow, heights, 'o', label="Measured values")
#plt.plot(x2, y2, '--', label="Quadratic fit")
#plt.plot(x, y, '--', label="Linear fit")
plt.xlabel("Mass flow rate (kg/s)")
plt.ylabel("Height of tank 1 (m)")
plt.legend()

# Calculate the height for a 5 minute emptying time
time_set = 300. # 5 minutes
mass = mass # remains the same
mass_flow_set = mass/time_set
height_calculated = m*mass_flow_set + b
print("Calculated height ", height_calculated, " meters", sep="")