#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov  1 12:54:32 2021

@author: ppantina
"""

import numpy as np
import h5py
import sys
from matplotlib import pyplot as plt
import glob

radName       = 'HIWRAP_KA'
flightDate    = '20200227'

fileout_n   = glob.glob('/data10/ppantina/IMPACTS-2022/Level1B/'        + '*' + radName + '*' + flightDate + '*.h5')[0]
if radName == 'CRS'    :fileout_o   = glob.glob('/data10/ppantina/IMPACTS-2020/Level1B_testbed/'+ '*' + radName  + '*RevB_' + flightDate + '*.h5')[0]
if 'EXRAD'  in radName :fileout_o   = glob.glob('/data10/ppantina/IMPACTS-2020/Level1B_testbed/'+ '*' + 'EXRAD'  + '*RevC_' + flightDate + '*.h5')[0]
if 'HIWRAP' in radName :fileout_o   = glob.glob('/data10/ppantina/IMPACTS-2020/Level1B_testbed/'+ '*' + 'HIWRAP' + '*RevC_' + flightDate + '*.h5')[0]
n  = h5py.File(fileout_n , 'r')
o  = h5py.File(fileout_o , 'r')

n_time  = n ['Time']['Data']['TimeUTC']
o_time  = o ['Time']['Data']['TimeUTC']

##Information
variables = o['Information'].keys()
for i, j in enumerate(variables):
    print (j)
    print (o['Information'][j][:])
    print (n['Information'][j][:])
    print ('\n')

##Navigation/Information
variables = o['Navigation']['Information'].keys()
for i, j in enumerate(variables):
    print (j)
    print (o['Navigation']['Information'][j][:])
    print (n['Navigation']['Information'][j][:])
    print ('\n')

##Time/Information
variables = o['Time']['Information'].keys()
for i, j in enumerate(variables):
    print (j)
    print (o['Time']['Information'][j][:])
    print (n['Time']['Information'][j][:])
    print ('\n')

##Products/Information
variables = o['Products']['Information'].keys()
for i, j in enumerate(variables):
    if (('HRRR' not in j)&('Velocity_corrected' not in j)&('horizwind' not in j)):
        if j not in ['AircraftMotion', 'MaskCoPol', 'MaskCrPol', 'Range', 'ResolutionHorizontal6dB', 'SNR', 'Velocity_nubf_offset']:
            print (j)
            print (o['Products']['Information'][j][:])
            print (n['Products']['Information'][j][:])
            print ('\n')
        else:
            if j in ['MaskCoPol', 'MaskCrPol', 'SNR', 'Velocity_nubf_offset']:
                fig = plt.figure()
                ax = fig.add_subplot(111)
                if ((j == 'SNR') | (j == 'Velocity_nubf_offset')):
                    ax.plot(o_time,  o ['Products']['Information'][j][:,500])
                    ax.plot(n_time,  n ['Products']['Information'][j][:,500], linewidth = 0.5)
                    ax.set_title(j)
                else:
                    ax.plot(o_time,  o ['Products']['Information'][j][:,500], '.')
                    ax.plot(n_time,  n ['Products']['Information'][j][:,500], '.' ,markersize = 0.5)
                    ax.set_title(j)
            else:
                fig = plt.figure()
                ax = fig.add_subplot(111)
                if j == 'Range':
                    ax.plot(o ['Products']['Information'][j][:].T)
                    ax.plot(n ['Products']['Information'][j][:], linewidth = 0.5)
                    ax.set_title(j)
                else:
                    if j == 'ResolutionHorizontal6dB':
                        ax.plot(o ['Products']['Information'][j][:])
                        ax.plot(n ['Products']['Information'][j][:], linewidth = 0.5)
                        ax.set_title(j)
                    else:
                        ax.plot(o_time, o ['Products']['Information'][j][:])
                        ax.plot(n_time, n ['Products']['Information'][j][:], linewidth = 0.5)
                        ax.set_title(j)

##Navigation/Data
variables = o['Navigation']['Data'].keys()
for i, j in enumerate(variables):
    if j in ['NominalDistance_units', 'NominalDistance_desciption']:
        print (j)
        print (o['Navigation']['Data'][j][:])
        print (n['Navigation']['Data'][j][:])
        print ('\n')
    else:    
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.plot(o_time,  o ['Navigation']['Data'][j][:])
        ax.plot(n_time,  n ['Navigation']['Data'][j][:], linewidth = 0.5)
        ax.set_title(j)

##Time/Data
variables = o['Time']['Data'].keys()
for i, j in enumerate(variables):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(o_time, o ['Time']['Data'][j][:])
    ax.plot(n_time, n ['Time']['Data'][j][:], linewidth =0.5)
    ax.set_title(j)

##Products/Data
variables = o['Products']['Data'].keys()
for i, j in enumerate(variables):
    if 'Velocity_corrected' not in j:
        if j in 'sigma0':
            fig = plt.figure()
            ax = fig.add_subplot(111)
            ax.plot(o_time, o ['Products']['Data'][j][:])
            ax.plot(n_time,  n['Products']['Data'][j][:], linewidth = 0.5)
            ax.set_title(j)
        else:
            fig = plt.figure()
            ax = fig.add_subplot(111)
            ax.plot(o_time, o ['Products']['Data'][j][:,500])
            ax.plot(n_time,n['Products']['Data'][j][:,500], linewidth = 0.5)
            ax.set_title(j)

##
##
##
##
##
if radName == 'HIWRAP_KU':kind = 'Ku'
if radName == 'HIWRAP_KA':kind = 'Ka'
for a in [kind]:
    ##Products/Information
    variables = o['Products'][a]['Information'].keys()
    for i, j in enumerate(variables):
        if 'Velocity' not in j:
            if j not in ['ResolutionHorizontal6dB']:
                print(j, a)
                print (o ['Products'][a]['Information'][j][:])
                print (n['Products']['Information'][j][:])
                print ('\n')
            else:
                fig = plt.figure()
                ax = fig.add_subplot(111)
                ax.plot(o ['Products'][a]['Information'][j][:])
                ax.plot(n['Products']['Information'][j][:], linewidth = 0.5)
                ax.set_title(a+' ' + j)
for a in [kind]:
    for b in ['Chirp', 'LowResPulse', 'HighResPulse']:
        ##Products/Information
        variables = o['Products'][a][b]['Information'].keys()
        for i, j in enumerate(variables):
            if 'Velocity' not in j:
                    if j not in ['MaskCoPol']:
                        print(j, a, b)
                        print (o ['Products'][a][b]['Information'][j][:])
                        print (n['Products'][b]['Information'][j][:])
                        print ('\n')
                    else:
                        fig = plt.figure()
                        ax = fig.add_subplot(111)
                        ax.plot(o_time,  o ['Products'][a][b]['Information'][j][:,500], '.')
                        ax.plot(n_time, n['Products'][b]['Information'][j][:,500], '.', markersize = 0.5)
                        ax.set_title(a+' ' +b+' ' + j)
            

for a in [kind]:
    for b in ['Combined']:
        ##Products/Information
        variables = o['Products'][a][b]['Information'].keys()
        for i, j in enumerate(variables):
            if 'Velocity' not in j:
                    if j not in ['ChannelMask']:
                        print(j, a, b)
                        print (o ['Products'][a][b]['Information'][j][:])
                        print (n['Products'][b]['Information'][j][:])
                        print ('\n')
                    else:
                        fig = plt.figure()
                        ax = fig.add_subplot(111)
                        ax.plot(o_time,  o ['Products'][a][b]['Information'][j][:,500], '.')
                        ax.plot(n_time, n['Products'][b]['Information'][j][:,500], '.', markersize = 0.5)
                        ax.set_title(a+' ' +b+' ' + j)
    
for a in [kind]:
    for b in ['Chirp', 'LowResPulse', 'HighResPulse']:
        ##Products/Information
        variables = o['Products'][a][b]['Data'].keys()
        for i, j in enumerate(variables):
            if 'Velocity' not in j:
                if j == 'sigma0':
                    fig = plt.figure()
                    ax = fig.add_subplot(111)
                    ax.plot(o_time,  o ['Products'][a][b]['Data'][j][:])
                    ax.plot(n_time, n['Products'][b]['Data'][j][:], linewidth = 0.5)
                    ax.set_title(a+' ' +b+' ' + j)
                else:
                    fig = plt.figure()
                    ax = fig.add_subplot(111)
                    ax.plot(o_time,  o ['Products'][a][b]['Data'][j][:,500])
                    ax.plot(n_time, n['Products'][b]['Data'][j][:,500], linewidth = 0.5)
                    ax.set_title(a+' ' +b+' ' + j)

for a in [kind]:
    for b in ['Combined']:
        ##Products/Information
        variables = o['Products'][a][b]['Data'].keys()
        for i, j in enumerate(variables):
            if 'Velocity' not in j:
                if j == 'sigma0':
                    fig = plt.figure()
                    ax = fig.add_subplot(111)
                    ax.plot(o_time,  o ['Products'][a][b]['Data'][j][:])
                    ax.plot(n_time, n['Products'][b]['Data'][j][:], linewidth = 0.5)
                    ax.set_title(a+' ' +b+' ' + j)
                else:
                    fig = plt.figure()
                    ax = fig.add_subplot(111)
                    ax.plot(o_time,  o ['Products'][a][b]['Data'][j][:,500])
                    ax.plot(n_time, n['Products'][b]['Data'][j][:,500], linewidth = 0.5)
                    ax.set_title(a+' ' +b+' ' + j)
