# 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


import os
import sys
import gc
import logging
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import blimpy as bl
from os.path import dirname, abspath, isdir
from argparse import ArgumentParser
from blimpy.utils import rebin
    parser.add_argument('--plot_dir', '-p', type=str, default=".", help='Directory to receive the plot (.png).  Default: current directory.')
    parser.add_argument("--dpi", "-d", type=int, default=200,
                        help="Image file dots per inch.  Default: 200.")
    if args is None:
        args = parser.parse_args()
    else:
        args = parser.parse_args(args)
    if args.file_list == []:
        os.system("stax -h")
        sys.exit(0)
    args.plot_dir += "/"

    # Get file paths in alphabetical order.
    args.file_list = sorted(args.file_list)

    # Compute the array of height ratios.
    height_ratios = []
    for file in args.file_list:
        wf = bl.Waterfall(file, max_load=1)
        nints = wf.file_shape[0]
        height_ratios.append((nints - 1) * wf.header["tsamp"])
        del wf

    # Make the plots.
    logger.info("Begin")
    make_waterfall_plots(args.file_list,
                         args.plot_dir,
                         args.dpi,
                         height_ratios,
                         args.f_start,

# 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


# Path: blimpy/stax.py
import os
import sys
import gc
import logging
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import blimpy as bl
from os.path import dirname, abspath, isdir
from argparse import ArgumentParser
from blimpy.utils import rebin

    parser.add_argument('--plot_dir', '-p', type=str, default=".", help='Directory to receive the plot (.png).  Default: current directory.')
    parser.add_argument("--dpi", "-d", type=int, default=200,
                        help="Image file dots per inch.  Default: 200.")
    if args is None:
        args = parser.parse_args()
    else:
        args = parser.parse_args(args)
    if args.file_list == []:
        os.system("stax -h")
        sys.exit(0)
    args.plot_dir += "/"

    # Get file paths in alphabetical order.
    args.file_list = sorted(args.file_list)

    # Compute the array of height ratios.
    height_ratios = []
    for file in args.file_list:
        wf = bl.Waterfall(file, max_load=1)
        nints = wf.file_shape[0]
        height_ratios.append((nints - 1) * wf.header["tsamp"])
        del wf

    # Make the plots.
    logger.info("Begin")
    make_waterfall_plots(args.file_list,
                         args.plot_dir,
                         args.dpi,
                         height_ratios,
                         args.f_start,

                         args.f_stop)