import json


def min_groups(mag, ins, filt, sub, mod, band, infile):
    """Estimates the minimum number of groups to reach 
    target acq sat requirements.

    Parameters
    ----------
    mag : float 
        Magnitude of star.
    ins : str
        Instrument.
    filt : str
        Filter.
    sub : str
        Subarray.
    mod : 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_groups : int
        The minimum number of groups to reach target snr.
    """

    with open(infile) as f:
        dat = json.load(f)
    
    # Match to closest magnitude
    mags = dat['mags']
    closest_mag = min(mags, key=lambda x:abs(x-mag))
    index = mags.index(closest_mag)
    
    # Match to data 
    min_groups = dat['ta_snr'][ins][filt][sub][mod][index]
    
    return min_groups