import matplotlib.pyplot as plt
import pandas as pd

from aeri_tools.io.dmv import housekeeping

HBB = ['HBBapexTemp', 'HBBbottomTemp', 'HBBtopTemp']
ABB = ['ABBapexTemp', 'ABBbottomTemp', 'ABBtopTemp']

check_vars = {
    'detector_temp_check' : ['detectorTemp',],
    'hbb_temp_outlier_check' : HBB,
    'abb_temp_outlier_check' : ABB,
    'bst_temp_outlier_check' : ['BBsupportStructureTemp',],
    'air_interferometer_outlier_check' : ['airNearInterferometerTemp',],
    'missing_data_flag_check' : ['missingDataFlag',],
    'imaginary_radiance_check' : ['skyViewImaginaryRadiance2510_2515',],
    'hbb_std_dev_check' : ['HBBviewStdDevRadiance985_990',],
    'hbb_lw_nen_check' : ['LW_HBB_NEN',],
    'hbb_sw_nen_check' : ['SW_HBB_NEN',],
    'lw_responsivity_check' : ['LWresponsivity',],
    'sw_responsivity_check' : ['SWresponsivity',],
    'sky_brightness_temp_spectral_averages_ch1_check' :
        ['surfaceLayerAirTemp675_680', 'BBsupportStructureTemp', 'airNearBBsTemp'],
    'sky_brightness_temp_spectral_averages_ch2_check' :
        ['surfaceLayerAirTemp2295_2300', 'BBsupportStructureTemp', 'airNearBBsTemp'],
    'hatch_check' : ['hatchOpen', 'sceneMirrorPosition'],
    'safing_check' : ['hatchOpen', 'sceneMirrorPosition'],
    'encoder_check' : ['sceneMirrorPosition',],
    'detector_check' : ['detectorTemp',],
    'hbb_thermistor_check' : HBB,
    'abb_thermistor_check' : ABB,
    'hbb_stable_check' : HBB,
    'hbb_covariance_check' : HBB,
    'sce_temp_deviation_check' : ['SCEtemp',]
}

sum_tests = [
    'imaginary_radiance_check', 'hbb_std_dev_check', 'hbb_lw_nen_check',
    'hbb_sw_nen_check', 'lw_responsivity_check', 'sw_responsivity_check',
    'sky_brightness_temp_spectral_averages_ch1_check',
    'sky_brightness_temp_spectral_averages_ch2_check'
]

def plot(test_name, path):
    data = housekeeping.get_all_housekeeping(path)

    fig = plt.figure(figsize=(25,15))
    for x in check_vars[test_name]:
        plt.plot(data['Time'], data[x])
    plt.suptitle(path, fontsize=20)
    plt.ylabel(test_name, fontsize=18)
    plt.legend(loc='upper right')
    if path.endswith('B1.CXS'):
        savename = path.split('/')[0] + '_' + path[-12:-6] + '_' + test_name + '.png'
    elif path.endswith('.SUM'):
        savename = path.split('/')[0] + '_' + path[-10:-4] + '_' + test_name + '.png'
    #savename = path.split('/')[0] + '_' + path[:-6] + '_' + test_name + '.png'
    #plt.savefig('/Users/adiebold/aeri_quality_control/testing/' + savename)
    plt.savefig('/Users/adiebold/aeri_quality_control/testing/pngs/diagnostics/'
                    + savename)
    # plt.savefig('/Users/adiebold/aeri_quality_control/testing/testing_diagnostics/'
                    # + savename)
    # plt.show()
    plt.close()

def test_plot():
    # pathname = '/Users/adiebold/aeri_quality_control/testing/'
    # pathname += 'awr/AE160911/160911B1.CXS'
    pathname = 'awr/AE160911/160911B1.CXS'
    for v in check_vars:
        print(v)
        if v in sum_tests:
            plot(v, pathname[:-6] + '.SUM')
        else:
            plot(v, pathname)

if __name__ == '__main__':
    test_plot()