# Path: blimpy/io/hdf_reader.py
# def examine_h5(h5):
#     """ Examine an HDF5 file for missing/corrupted components. """
#     if "CLASS" in h5.attrs:
#         classstr = h5.attrs["CLASS"]
#     else:
#         oops("examine_h5: HDF5 CLASS attribute missing")
#     if not classstr in ["FILTERBANK", b"FILTERBANK"]:
#         oops("examine_h5: Expected HDF5 CLASS attribute to be 'FILTERBANK' but saw '{}'".format(classstr))
#     if "VERSION" in h5.attrs:
#         verblob = h5.attrs["VERSION"]
#     else:
#         oops("examine_h5: HDF5 VERSION attribute missing")
#     if type(verblob) == str:
#         version = float(verblob)
#     elif type(verblob) == bytes or type(verblob) == np.bytes_:
#         version = float(verblob.decode("utf-8"))
#     else:
#         oops("examine_h5: HDF5 VERSION attribute is neither str nor bytes, saw {}".format(verblob))
#     if not "data" in h5:
#         oops("examine_h5: HDF5 data matrix missing")
#     if h5["data"].ndim != 3:
#         oops("examine_h5: Expected HDF5 data.ndim to be 3 but saw '{}'".format(h5["data"].ndim))
#     try:
#         xx = h5["data"][-1][0][0]
#     except:
#         oops("examine_h5: HDF5 Time-dimension data corruption")
#     try:
#         xx = h5["data"][0][0][-1]
#     except OSError:
#         oops("examine_h5: HDF5 Frequency-dimension data corruption")
#     try:
#         xx = h5["data"][-1][0][-1]
#     except:
#         oops("examine_h5: HDF5 End-diagonal data corruption")
# 
#     return version


import os
import sys
import h5py
from argparse import ArgumentParser
from astropy.coordinates import Angle
from blimpy.io.hdf_reader import examine_h5
def read_header(h5):
    """ Read header and return a Python dictionary of key:value pairs
    """

    header = {} # Initialise as a nil dictionary.

    for key, val in h5["data"].attrs.items():
        #if six.PY3:
        #    key = bytes(key, "ascii")
        if isinstance(val, bytes):
            val = val.decode("ascii")
        if key == "src_raj":
            header[key] = Angle(val, unit="hr")
        elif key == "src_dej":
            header[key] = Angle(val, unit="deg")
        else:
            header[key] = val

    return header


def examine(filename):
    r""" Diagnose the given HDF5 file"""
    h5file = h5py.File(filename, mode="r")
    version = examine_h5(h5file)
    print("VERSION attribute:", version)
    header = read_header(h5file)
    print("header:", header)
    print("data shape:", h5file["data"].shape)
    if version >= 1.999:

# Path: blimpy/io/hdf_reader.py
# def examine_h5(h5):
#     """ Examine an HDF5 file for missing/corrupted components. """
#     if "CLASS" in h5.attrs:
#         classstr = h5.attrs["CLASS"]
#     else:
#         oops("examine_h5: HDF5 CLASS attribute missing")
#     if not classstr in ["FILTERBANK", b"FILTERBANK"]:
#         oops("examine_h5: Expected HDF5 CLASS attribute to be 'FILTERBANK' but saw '{}'".format(classstr))
#     if "VERSION" in h5.attrs:
#         verblob = h5.attrs["VERSION"]
#     else:
#         oops("examine_h5: HDF5 VERSION attribute missing")
#     if type(verblob) == str:
#         version = float(verblob)
#     elif type(verblob) == bytes or type(verblob) == np.bytes_:
#         version = float(verblob.decode("utf-8"))
#     else:
#         oops("examine_h5: HDF5 VERSION attribute is neither str nor bytes, saw {}".format(verblob))
#     if not "data" in h5:
#         oops("examine_h5: HDF5 data matrix missing")
#     if h5["data"].ndim != 3:
#         oops("examine_h5: Expected HDF5 data.ndim to be 3 but saw '{}'".format(h5["data"].ndim))
#     try:
#         xx = h5["data"][-1][0][0]
#     except:
#         oops("examine_h5: HDF5 Time-dimension data corruption")
#     try:
#         xx = h5["data"][0][0][-1]
#     except OSError:
#         oops("examine_h5: HDF5 Frequency-dimension data corruption")
#     try:
#         xx = h5["data"][-1][0][-1]
#     except:
#         oops("examine_h5: HDF5 End-diagonal data corruption")
# 
#     return version


# Path: blimpy/h5diag.py
import os
import sys
import h5py
from argparse import ArgumentParser
from astropy.coordinates import Angle
from blimpy.io.hdf_reader import examine_h5

def read_header(h5):
    """ Read header and return a Python dictionary of key:value pairs
    """

    header = {} # Initialise as a nil dictionary.

    for key, val in h5["data"].attrs.items():
        #if six.PY3:
        #    key = bytes(key, "ascii")
        if isinstance(val, bytes):
            val = val.decode("ascii")
        if key == "src_raj":
            header[key] = Angle(val, unit="hr")
        elif key == "src_dej":
            header[key] = Angle(val, unit="deg")
        else:
            header[key] = val

    return header


def examine(filename):
    r""" Diagnose the given HDF5 file"""
    h5file = h5py.File(filename, mode="r")
    version = examine_h5(h5file)
    print("VERSION attribute:", version)
    header = read_header(h5file)
    print("header:", header)
    print("data shape:", h5file["data"].shape)
    if version >= 1.999:

        print("Number of fine channels:", header["nchans"])