import sys
import json
import glob
from datetime import datetime

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

freqs = pd.read_csv("600.ghz")
freqs.columns = ["ghz",]
files = glob.glob("SW/ACS_A/*.json")
files = sorted(files)
print(len(freqs))

tstamp = [datetime.strptime(x[15:30], "%Y%m%dT%H%M%S") for x in files]
freq = np.linspace(4.4, 8.8, num=1024)

j = [i for i in range(len(freq)) if 5.5 < freq[i] < 6.5]

Th = 302.9
Tc =  94.0

fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize = (8.5,11))
for i in range(len(freqs)):
    f = freqs["ghz"][i]
    print(i, f)
    print(files[3*i:3*i+3])
    ax1.cla()
    ax2.cla()
    ax3.cla()
    with open(files[3*i+0]) as f:
        hot = json.load(f)
    with open(files[3*i+1]) as f:
        cold = json.load(f)
    with open(files[3*i+2]) as f:
        ru = json.load(f)
    hot = np.array(hot["data"])
    cold = np.array(cold["data"])
    ru = np.array(ru["data"])
    ax1.plot(freq, hot, "r-", freq, cold, 'b-', freq, ru, 'g-')
    ax1.set_ylim(0, 400)
    ax1.grid()
    ax1.legend(["hot","cold","ru"])

    ax2.plot(freq, hot-cold, 'r-', freq, ru-cold, 'b-')
    ax2.set_ylim(0, 50)
    ax2.grid()
    ax2.legend(["hot-cold","ru-cold"])

    ax3.plot(freq, (ru-cold)/(hot-cold)*(Th-Tc)+Tc, 'r-')
    ax3.set_ylim(225-10, 225+10)
    ax3.grid()
    ax3.plot(freq[j], (ru[j]-cold[j])/(hot[j]-cold[j])*(Th-Tc)+Tc, 'b-')
    ax3.legend(["(Th-Tc)*(ru-cold)/(hot-cold)+Tc"], handletextpad=0.0, handlelength=0.0)
    plt.pause(0.05)

plt.show()
