def bin_mag_catalog(mag_table, b, mag_table_keys=None, bin_keys=None):
    """ Bin an external catalogue based on a bin's provided parameters.

    Args:
        mag_table The magnitude table to rebin
        b The tbridge.Bin object
        mag_table_keys The magnitude table keys to use for binning.
        bin_keys The associated bin keys (must be the same length AND order as mag_table_keys).

    Returns:
        Table of magnitudes binned by the appropriate parameters.
    """
    if bin_keys is None:
        bin_keys = ["MASSES", "REDSHIFTS"]
    if mag_table_keys is None:
        mag_table_keys = ["MASS", "Z"]

    # Gather the necessary information from the provided bin.
    bin_dict = b.return_param_dict()

    # Iterate over all keys and only return the rows that are within the bin limits.
    for i in range(len(bin_keys)):
        low, high = bin_dict[bin_keys[i]]
        mag_table = mag_table[mag_table[mag_table_keys[i]] >= low]
        mag_table = mag_table[mag_table[mag_table_keys[i]] <= high]

    return mag_table