#!/usr/bin/env python3

import re
import sys
import matplotlib.pyplot as plt

v1 = []
v2 = []
v3 = []
i1 = []
i2 = []
i3 = []
p1 = []
p2 = []
p3 = []
f1 = []
f2 = []
f3 = []
t1 = []
t2 = []
t3 = []
for line in open('../../monimod_test_log.txt', 'r').readlines():
    v_match = re.match('v1:[^\w]+([.\w]+)[^\w]+v2:[^\w]+([.\w]+)[^\w]+v3:[^\w]+([.\w]+)', line.upper(), re.I)
    if v_match:
        v1 += [float(v_match.group(1))]
        v2 += [float(v_match.group(2))]
        v3 += [float(v_match.group(3))]
    i_match = re.match('i1:[^\w]+([.\w]+)[^\w]+i2:[^\w]+([.\w]+)[^\w]+i3:[^\w]+([.\w]+)', line.upper(), re.I)
    if i_match:
        i1 += [float(i_match.group(1))]
        i2 += [float(i_match.group(2))]
        i3 += [float(i_match.group(3))]
    p_match = re.match('p1:[^\w]+([.\w]+)[^\w]+p2:[^\w]+([.\w]+)[^\w]+p3:[^\w]+([.\w]+)', line.upper(), re.I)
    if p_match:
        p1 += [float(p_match.group(1))]
        p2 += [float(p_match.group(2))]
        p3 += [float(p_match.group(3))]
    f_match = re.match('f1:[^\w]+([.\w]+)[^\w]+f2:[^\w]+([.\w]+)[^\w]+f3:[^\w]+([.\w]+)', line.upper(), re.I)
    if f_match:
        f1 += [float(f_match.group(1))]
        f2 += [float(f_match.group(2))]
        f3 += [float(f_match.group(3))]
    t_match = re.match('t1:[^\w]+([.\w]+)[^\w]+t2:[^\w]+([.\w]+)[^\w]+t3:[^\w]+([.\w]+)', line.upper(), re.I)
    if t_match:
        t1 += [float(t_match.group(1))]
        t2 += [float(t_match.group(2))]
        t3 += [float(t_match.group(3))]

print('got ' + str(len(v1)) + ' voltage triplets')
print('got ' + str(len(i1)) + ' current triplets')
print('got ' + str(len(p1)) + ' power triplets')
print('got ' + str(len(f1)) + ' fan speed triplets')
print('got ' + str(len(t1)) + ' temperature triplets')

def myplot(fname, vals1, vals2, vals3, labely, label1, label2, label3):
    plt.style.use('ggplot')
    plt.figure(figsize=(13,6))
    plt.xlabel('sample')
    plt.ylabel(labely)
    plt.plot(vals1, label=label1)
    plt.plot(vals2, label=label2)
    plt.plot(vals3, label=label3)
    plt.legend()
    print('saving plot \"' + fname + '\"...')
    plt.savefig(fname)
    plt.clf()

myplot('log_plot_v.pdf', v1, v2, v3, 'Voltage [V]', 'V1', 'V2', 'V3')
myplot('log_plot_i.pdf', i1, i2, i3, 'Current [A]', 'I1', 'I2', 'I3')
myplot('log_plot_p.pdf', p1, p2, p3, 'Power [W]', 'P1', 'P2', 'P3')
myplot('log_plot_f.pdf', f1, f2, f3, 'Fan speed [RPM]', 'F1', 'F2', 'F3')
myplot('log_plot_t.pdf', t1, t2, t3, 'Temperature [℃]', 'T1', 'T2', 'T3')
