# Path: blimpy/utils.py
# 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
# 
# def db(x, offset=0):
#     """ Convert linear to dB """
#     return 10 * np.log10(x + offset)


from .config import *
from ..utils import rebin, db
        plot_data = plot_data[..., ::-1]  # Reverse data
        plot_f = plot_f[::-1]

    if logged:
        db_plot_data = db(plot_data[0])
        fig_max = np.nanmax(db_plot_data[db_plot_data != np.inf])
        fig_min = np.nanmin(db_plot_data[db_plot_data != -np.inf])
    else:
        fig_max = plot_data[0].max()
        fig_min = plot_data[0].min()

    print("averaging along time axis...")

    # Since the data has been squeezed, the axis for time goes away if only one bin, causing a bug with axis=1
    if len(plot_data.shape) > 1:
        plot_max = plot_data.max(axis=0)
        plot_min = plot_data.min(axis=0)
        plot_data = plot_data.mean(axis=0)
    else:
        plot_max = plot_data.max()
        plot_min = plot_data.min()
        plot_data = plot_data.mean()

    # Rebin to max number of points
    dec_fac_x = 1
    MAX_PLT_POINTS = 8 * 64  # Low resoluition to see the difference.
    if plot_data.shape[0] > MAX_PLT_POINTS:
        dec_fac_x = int(plot_data.shape[0] / MAX_PLT_POINTS)

    plot_data = rebin(plot_data, dec_fac_x, 1)

# Path: blimpy/utils.py
# 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
# 
# def db(x, offset=0):
#     """ Convert linear to dB """
#     return 10 * np.log10(x + offset)


# Path: blimpy/plotting/plot_spectrum_min_max.py
from .config import *
from ..utils import rebin, db

        plot_data = plot_data[..., ::-1]  # Reverse data
        plot_f = plot_f[::-1]

    if logged:
        db_plot_data = db(plot_data[0])
        fig_max = np.nanmax(db_plot_data[db_plot_data != np.inf])
        fig_min = np.nanmin(db_plot_data[db_plot_data != -np.inf])
    else:
        fig_max = plot_data[0].max()
        fig_min = plot_data[0].min()

    print("averaging along time axis...")

    # Since the data has been squeezed, the axis for time goes away if only one bin, causing a bug with axis=1
    if len(plot_data.shape) > 1:
        plot_max = plot_data.max(axis=0)
        plot_min = plot_data.min(axis=0)
        plot_data = plot_data.mean(axis=0)
    else:
        plot_max = plot_data.max()
        plot_min = plot_data.min()
        plot_data = plot_data.mean()

    # Rebin to max number of points
    dec_fac_x = 1
    MAX_PLT_POINTS = 8 * 64  # Low resoluition to see the difference.
    if plot_data.shape[0] > MAX_PLT_POINTS:
        dec_fac_x = int(plot_data.shape[0] / MAX_PLT_POINTS)

    plot_data = rebin(plot_data, dec_fac_x, 1)

    plot_min = rebin(plot_min, dec_fac_x, 1)