# Path: blimpy/utils.py
# def unpack(data, nbit):
#     """upgrade data from nbits to 8bits
# 
#     Notes: Pretty sure this function is a little broken!
#     """
#     if nbit > 8:
#         raise ValueError("unpack: nbit must be <= 8")
#     if 8 % nbit != 0:
#         raise ValueError("unpack: nbit must divide into 8")
#     if data.dtype not in (np.uint8, np.int8):
#         raise TypeError("unpack: dtype must be 8-bit")
#     if nbit == 8:
#         return data
#     if nbit == 4:
#         data = unpack_4to8(data)
#         return data
#     if nbit == 2:
#         data = unpack_2to8(data)
#         return data
#     if nbit == 1:
#         data = unpack_1to8(data)
#         return data
# 
#     return None ## SHOULD NEVER HAPPEN
# 
# def rebin(d, n_x=None, n_y=None, n_z=None):
#     """ Rebin data by averaging bins together
# 
#     Args:
#     d (np.array): data
#     n_x (int): number of bins in x dir to rebin into one
#     n_y (int): number of bins in y dir to rebin into one
# 
#     Returns:
#     d: rebinned data with shape (n_x, n_y)
#     """
#     if n_x is None:
#         n_x = 1
#     else:
#         n_x = math.ceil(n_x)
#     if n_y is None:
#         n_y = 1
#     else:
#         n_y = math.ceil(n_y)
#     if n_z is None:
#         n_z = 1
#     else:
#         n_z = math.ceil(n_z)
# 
#     if d.ndim == 3:
#         d = d[:int(d.shape[0] // n_x) * n_x, :int(d.shape[1] // n_y) * n_y, :int(d.shape[2] // n_z) * n_z]
#         d = d.reshape((d.shape[0] // n_x, n_x, d.shape[1] // n_y, n_y, d.shape[2] // n_z, n_z))
#         d = d.mean(axis=5)
#         d = d.mean(axis=3)
#         d = d.mean(axis=1)
#     elif d.ndim == 2:
#         d = d[:int(d.shape[0] // n_x) * n_x, :int(d.shape[1] // n_y) * n_y]
#         d = d.reshape((d.shape[0] // n_x, n_x, d.shape[1] // n_y, n_y))
#         d = d.mean(axis=3)
#         d = d.mean(axis=1)
#     elif d.ndim == 1:
#         d = d[:int(d.shape[0] // n_x) * n_x]
#         d = d.reshape((d.shape[0] // n_x, n_x))
#         d = d.mean(axis=1)
#     else:
#         raise RuntimeError("Only NDIM <= 3 supported")
#     return d


import os
import sys
import numpy as np
        import matplotlib
    import pylab as plt
    import matplotlib
    import pylab as plt
from astropy.coordinates import Angle
from .utils import unpack, rebin
    from argparse import ArgumentParser
        self._d_x[:] = d[..., 0:2]
        self._d_y[:] = d[..., 2:4]
        return header, self._d_x, self._d_y

    def read_next_data_block_int8_2x(self):
        """ Read the next block of data and its header

        Returns: (header, data)
            header (dict): dictionary of header metadata
            data (np.array): Numpy array of data, converted into to complex64.
        TODO: Deprecate?
        """
        header, data_idx = self.read_header()
        self.file_obj.seek(data_idx)

        # Read data and reshape

        n_chan = int(header['OBSNCHAN'])
        n_pol = int(header['NPOL'])
        n_bit = int(header['NBITS'])
        n_samples = int(int(header['BLOCSIZE']) / (n_chan * n_pol * (n_bit / 8)))

        d = np.fromfile(self.file_obj, count=header['BLOCSIZE'], dtype='int8')

        header, data_idx = self.read_header()
        self.file_obj.seek(data_idx)
        d2 = np.fromfile(self.file_obj, count=header['BLOCSIZE'], dtype='int8')

        # Handle 2-bit and 4-bit data
        if n_bit != 8:

# Path: blimpy/utils.py
# def unpack(data, nbit):
#     """upgrade data from nbits to 8bits
# 
#     Notes: Pretty sure this function is a little broken!
#     """
#     if nbit > 8:
#         raise ValueError("unpack: nbit must be <= 8")
#     if 8 % nbit != 0:
#         raise ValueError("unpack: nbit must divide into 8")
#     if data.dtype not in (np.uint8, np.int8):
#         raise TypeError("unpack: dtype must be 8-bit")
#     if nbit == 8:
#         return data
#     if nbit == 4:
#         data = unpack_4to8(data)
#         return data
#     if nbit == 2:
#         data = unpack_2to8(data)
#         return data
#     if nbit == 1:
#         data = unpack_1to8(data)
#         return data
# 
#     return None ## SHOULD NEVER HAPPEN
# 
# def rebin(d, n_x=None, n_y=None, n_z=None):
#     """ Rebin data by averaging bins together
# 
#     Args:
#     d (np.array): data
#     n_x (int): number of bins in x dir to rebin into one
#     n_y (int): number of bins in y dir to rebin into one
# 
#     Returns:
#     d: rebinned data with shape (n_x, n_y)
#     """
#     if n_x is None:
#         n_x = 1
#     else:
#         n_x = math.ceil(n_x)
#     if n_y is None:
#         n_y = 1
#     else:
#         n_y = math.ceil(n_y)
#     if n_z is None:
#         n_z = 1
#     else:
#         n_z = math.ceil(n_z)
# 
#     if d.ndim == 3:
#         d = d[:int(d.shape[0] // n_x) * n_x, :int(d.shape[1] // n_y) * n_y, :int(d.shape[2] // n_z) * n_z]
#         d = d.reshape((d.shape[0] // n_x, n_x, d.shape[1] // n_y, n_y, d.shape[2] // n_z, n_z))
#         d = d.mean(axis=5)
#         d = d.mean(axis=3)
#         d = d.mean(axis=1)
#     elif d.ndim == 2:
#         d = d[:int(d.shape[0] // n_x) * n_x, :int(d.shape[1] // n_y) * n_y]
#         d = d.reshape((d.shape[0] // n_x, n_x, d.shape[1] // n_y, n_y))
#         d = d.mean(axis=3)
#         d = d.mean(axis=1)
#     elif d.ndim == 1:
#         d = d[:int(d.shape[0] // n_x) * n_x]
#         d = d.reshape((d.shape[0] // n_x, n_x))
#         d = d.mean(axis=1)
#     else:
#         raise RuntimeError("Only NDIM <= 3 supported")
#     return d


# Path: blimpy/guppi.py
import os
import sys
import numpy as np
        import matplotlib
    import pylab as plt
    import matplotlib
    import pylab as plt
from astropy.coordinates import Angle
from .utils import unpack, rebin
    from argparse import ArgumentParser

        self._d_x[:] = d[..., 0:2]
        self._d_y[:] = d[..., 2:4]
        return header, self._d_x, self._d_y

    def read_next_data_block_int8_2x(self):
        """ Read the next block of data and its header

        Returns: (header, data)
            header (dict): dictionary of header metadata
            data (np.array): Numpy array of data, converted into to complex64.
        TODO: Deprecate?
        """
        header, data_idx = self.read_header()
        self.file_obj.seek(data_idx)

        # Read data and reshape

        n_chan = int(header['OBSNCHAN'])
        n_pol = int(header['NPOL'])
        n_bit = int(header['NBITS'])
        n_samples = int(int(header['BLOCSIZE']) / (n_chan * n_pol * (n_bit / 8)))

        d = np.fromfile(self.file_obj, count=header['BLOCSIZE'], dtype='int8')

        header, data_idx = self.read_header()
        self.file_obj.seek(data_idx)
        d2 = np.fromfile(self.file_obj, count=header['BLOCSIZE'], dtype='int8')

        # Handle 2-bit and 4-bit data
        if n_bit != 8:

            d = unpack(d, n_bit)