import json


def min_num_groups_for_sat(magnitude, instrument, filt, subarray, model, band, infile):
    """Estimates the minimum number of groups to reach target acq
    saturation requirements.

    Parameters
    ----------
    magnitude : float
        Magnitude of star.
    instrument : str
        Instrument.
    filt : str
        Filter.
    subarray : str
        Subarray.
    model : str
        Phoenix model key.
    band : str, currently unused?
        The band -- right now only k sooo?
    infile : str
        The file with the pandeia data.

    Returns
    -------
    min_num_groups_for_sat : int
        The minimum number of groups to reach target snr.
    """

    with open(infile) as f:
        data = json.load(f)

    # Match to closest magnitude
    magnitudes = [float(i) for i in data['mags']]
    closest_magnitude = min(magnitudes, key=lambda x: abs(x - float(magnitude)))
    index = magnitudes.index(closest_magnitude)

    # Match to data
    minimum_num_groups_for_sat = data['ta_snr'][instrument][filt][subarray][model][index]

    return minimum_num_groups_for_sat