#! /usr/bin/python3
import sys
import math
import csv

def main():
    try:
        f = open(sys.argv[1], "r")
        if (f.mode == "r"):
            t = f.read()
    except IOError:
        sys.exit(84)
    l = t.split("\n")
    t = []
    for i in range(0, len(l)):
        t.append(l[i].split(";"))
    vol = []
    ph = []
    for i in range(0, len(t)):
        if (len(t[i]) == 2):
            vol.append(float(t[i][0]))
            ph.append(float(t[i][1]))
    derivate = []
    for i in range(1, len(vol) - 1):
        derivate.append((float(ph[i+1]) - float(ph[i-1])) / (float(vol[i+1]) - float(vol[i-1])))
    print("Derivative:")
    for i in range(1, len(derivate) + 1):
        if ((round(vol[i])) == vol[i]):
            print("%i ml -> %.2f"%(vol[i], derivate[i-1]))
        else:
            print("%.1f ml -> %.2f"%(vol[i], derivate[i-1]))
    point = float(0)
    index = 0
    for i in range (0, len(derivate)):
        if (derivate[i] > point):
            point = vol[i + 1]
            index = i + 1
    if ((round(point)) == point):
        print("\nEquivalent point at %i ml"%(point))
    else:
        print("\nEquivalent point at %.1f ml"%(point))
    second_derivate = []
    for i in range(1, len(derivate) - 1):
        second_derivate.append((float(derivate[i+1]) - float(derivate[i-1])) / (float(vol[i+2]) - float(vol[i])))
    print("\nSecond derivative:")
    for i in range(1, len(second_derivate) + 1):
        if ((round(vol[i+1])) == vol[i+1]):
            print("%i ml -> %.2f"%(vol[i+1], second_derivate[i-1]))
        else:
            print("%.1f ml -> %.2f"%(vol[i+1], second_derivate[i-1]))
    new_vol = []
    second_derivate_estimated = []
    for i in range(index - 1, index + 1):
        a = float((float(vol[i+1]) - float(vol[i])) * 10)
        b = float((float(second_derivate[i-1]) - float(second_derivate[i-2])) / a)
        for e in range(0, int(a)):
            new_vol.append(float(vol[i]) + 0.1 * float(e))
            second_derivate_estimated.append(float(second_derivate[i-2]) + b * float(e))
    print("\nSecond derivative estimated:")
    for i in range(0, len(second_derivate_estimated)):
        if ((round(new_vol[i])) == new_vol[i]):
            print("%i ml -> %.2f"%(new_vol[i], second_derivate_estimated[i]))
        else:
            print("%.1f ml -> %.2f"%(new_vol[i], second_derivate_estimated[i]))
    if ((round(vol[index + 1])) == vol[index + 1]):
        print("%i ml -> %.2f"%(vol[index + 1], second_derivate[index-1]))
    else:
        print("%.1f ml -> %.2f"%(new_vol[index + 1], second_derivate[index-1]))
    save_vol = float(1000000)
    save = float(1000000)
    for i in range(0, len(second_derivate_estimated)):
        if (abs(second_derivate_estimated[i]) < abs(save)):
            save = abs(second_derivate_estimated[i])
            save_vol = float(new_vol[i])
    if ((round(save_vol)) == save_vol):
        print("\nEquivalent point at %i ml"%(save_vol))
    else:
        print("\nEquivalent point at %.1f ml"%(save_vol))

if __name__ == "__main__":
    main()