# Path: blimpy/utils.py
# def db(x, offset=0):
# def lin(x):
# def closest(xarr, val):
# def rebin(d, n_x=None, n_y=None, n_z=None):
# def unpack(data, nbit):
# def unpack_1to8(data):
# def unpack_2to8(data):
# def unpack_4to8(data):
# def change_the_ext(path, old_ext, new_ext):


import os
import sys
import time
import h5py
import hdf5plugin
from blimpy import utils

def __write_to_hdf5_light(wf, filename_out, f_scrunch=None, *args, **kwargs):
    """ Copy the header and the selected data matrix subset from the input file to the output HDF5 file in one go.

    Args:
        wf : Waterfall object
        filename_out (str): Name of output file
        f_scrunch (int or None): Average (scrunch) N channels together
    """

    wf.logger.info("__write_to_hdf5_light: Writing the spectra matrix for {} without blobbing."
                   .format(filename_out))
    try:
        os.remove(filename_out)  # Try to pre-remove output .h5 file.
    except:
        pass # Ignore errors.
    
    with h5py.File(filename_out, 'w') as h5:

        h5.attrs['CLASS']   = 'FILTERBANK'
        h5.attrs['VERSION'] = '1.0'

        bs_compression = hdf5plugin.Bitshuffle(nelems=0, lz4=True)['compression']
        bs_compression_opts = hdf5plugin.Bitshuffle(nelems=0, lz4=True)['compression_opts']

        # Frequency scrunching?
        if f_scrunch is None:
            data_out = wf.data # No, just copy as-is from SIGPROC Filterbank (.fil) selection
        else:
            wf.logger.info('Frequency scrunching by %i' % f_scrunch)

# Path: blimpy/utils.py
# def db(x, offset=0):
# def lin(x):
# def closest(xarr, val):
# def rebin(d, n_x=None, n_y=None, n_z=None):
# def unpack(data, nbit):
# def unpack_1to8(data):
# def unpack_2to8(data):
# def unpack_4to8(data):
# def change_the_ext(path, old_ext, new_ext):


# Path: blimpy/io/hdf_writer.py
import os
import sys
import time
import h5py
import hdf5plugin
from blimpy import utils


def __write_to_hdf5_light(wf, filename_out, f_scrunch=None, *args, **kwargs):
    """ Copy the header and the selected data matrix subset from the input file to the output HDF5 file in one go.

    Args:
        wf : Waterfall object
        filename_out (str): Name of output file
        f_scrunch (int or None): Average (scrunch) N channels together
    """

    wf.logger.info("__write_to_hdf5_light: Writing the spectra matrix for {} without blobbing."
                   .format(filename_out))
    try:
        os.remove(filename_out)  # Try to pre-remove output .h5 file.
    except:
        pass # Ignore errors.
    
    with h5py.File(filename_out, 'w') as h5:

        h5.attrs['CLASS']   = 'FILTERBANK'
        h5.attrs['VERSION'] = '1.0'

        bs_compression = hdf5plugin.Bitshuffle(nelems=0, lz4=True)['compression']
        bs_compression_opts = hdf5plugin.Bitshuffle(nelems=0, lz4=True)['compression_opts']

        # Frequency scrunching?
        if f_scrunch is None:
            data_out = wf.data # No, just copy as-is from SIGPROC Filterbank (.fil) selection
        else:
            wf.logger.info('Frequency scrunching by %i' % f_scrunch)

            data_out = utils.rebin(wf.data, n_z=f_scrunch)