#!/usr/bin/python3

import sys
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
import time
import numpy as np
import datetime as dt

def get_data(filename):
    data = pd.read_csv(filename)

    #convert date column to datetime type
    data['Date'] = pd.to_datetime(data['Date'])

    return data

#####################################################
def plot_temps(data, period):
    x = data['Date'].dt.to_pydatetime()
    mask = data['Date']>=(data['Date'].iloc[-1]-period)
    tstamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())

    fig, ax = plt.subplots(3, 2)
    fig.suptitle('DI/OT System Board IGL (%s)' % tstamp)
    sel_cols = ['Amb_T','LDO_T','FPGA_T']
    ax[0, 0].set_ylabel('deg C')
    ax[0, 0].plot(x[mask], data[mask][sel_cols])
    ax[0, 0].legend(sel_cols)

    sel_cols = ['P12V_V']
    ax[1, 0].set_ylabel('V')
    ax[1, 0].plot(x[mask], data[mask][sel_cols], scaley=True)
    ax[1, 0].legend(sel_cols)

    sel_cols = ['P12V_I']
    ax[2, 0].set_ylabel('A')
    ax[2, 0].set_xlabel('<day> <time>')
    ax[2, 0].plot(x[mask], data[mask][sel_cols], scaley=True)
    ax[2, 0].legend(sel_cols)

    sel_cols = ['P1V2_V', 'P3V3_V']
    ax[0, 1].set_ylabel('V')
    ax[0, 1].plot(x[mask], data[mask][sel_cols], scaley=True)
    ax[0, 1].legend(sel_cols)

    sel_cols = ['Com_E', 'Flash_E', 'Ping_E']
    ax[1, 1].set_ylabel('Cnt')
    ax[1, 1].plot(x[mask], data[mask][sel_cols], scaley=True)
    ax[1, 1].legend(sel_cols)

    # Not used for now
    ax[2, 1].set_facecolor('tab:gray')
    ax[2, 0].set_xlabel('<day> <time>')

    mng = plt.get_current_fig_manager()
    mng.resize(*mng.window.maxsize())
    plt.show()

#####################################################

if len(sys.argv) < 3:
        print ("You did not specify the csv file and/or period (in minutes)")
        sys.exit()

data = get_data(sys.argv[1])
period = dt.timedelta(minutes=int(sys.argv[2]))

plot_temps(data, period)