Returns list of significant candidate loc in candsfile.
    Can define threshold and maximum number of locs per scan.
    Works on merge or per-scan cands pkls.

def thresholdcands(candsfile, threshold, numberperscan=1):
    """ Returns list of significant candidate loc in candsfile.
    Can define threshold and maximum number of locs per scan.
    Works on merge or per-scan cands pkls.
    """

    # read metadata and define columns of interest
    loc, prop, d = read_candidates(candsfile, returnstate=True)
    try:
        scancol = d['featureind'].index('scan')
    except ValueError:
        scancol = -1
    if 'snr2' in d['features']:
        snrcol = d['features'].index('snr2')
    elif 'snr1' in d['features']:
        snrcol = d['features'].index('snr1')

    snrs = [prop[i][snrcol] for i in range(len(prop)) if prop[i][snrcol] > threshold]

    # calculate unique list of locs of interest
    siglocs = [list(loc[i]) for i in range(len(prop)) if prop[i][snrcol] > threshold]
    siglocssort = sorted(zip([list(ll) for ll in siglocs], snrs), key=lambda stuff: stuff[1], reverse=True)

    if scancol >= 0:
        scanset = list(set([siglocs[i][scancol] for i in range(len(siglocs))]))
        candlist= []
        for scan in scanset:
            logger.debug('looking in scan %d' % scan)
            count = 0
            for sigloc in siglocssort:
                if sigloc[0][scancol] == scan:
                    logger.debug('adding sigloc %s' % str(sigloc))
                    candlist.append(sigloc)
                    count += 1
                if count >= numberperscan:
                    break
    else:
        candlist = siglocssort[:numberperscan]

    logger.debug('Returning %d cands above threshold %.1f' % (len(candlist), threshold))
    return [loc for loc,snr in candlist]