Runs refine_cand on all positive SNR candidates above threshold. Any detected at higher SNR are highlighted.

def refine_cands(candsfile, threshold=0, scaledm=2.1, scalepix=2, scaleuv=1.0, chans=[], savepkl=True):
    """ Runs refine_cand on all positive SNR candidates above threshold. Any detected at higher SNR are highlighted. """

    # get snrs above threshold
    locs, props, d = pc.read_candidates(candsfile, snrmin=threshold, returnstate=True)

    if 'snr2' in d['features']:
        snrcol = d['features'].index('snr2')
    elif 'snr1' in d['features']:
        snrcol = d['features'].index('snr1')
    scancol = d['featureind'].index('scan')
    segmentcol = d['featureind'].index('segment')
    intcol = d['featureind'].index('int')
    dtindcol = d['featureind'].index('dtind')
    dmindcol = d['featureind'].index('dmind')
    snrs = props[:, snrcol]
    
    for (i, snr) in enumerate(snrs):
        if snr > 0:
            d, cands = refine_cand(candsfile, threshold=threshold, candnum=i,
                                   scaledm=scaledm, scalepix=scalepix, scaleuv=scaleuv, chans=chans)
            if cands:
                candlocs = np.array(cands.keys())
                candprops = np.array(cands.values())

                scan = locs[i, scancol]
                segment = locs[i, segmentcol]
                candint = locs[i, intcol]
                dmind = locs[i, dmindcol]
                dtind = locs[i, dtindcol]
                candfile = 'cands_{0}_sc{1}-seg{2}-i{3}-dm{4}-dt{5}.pkl'.format(d['fileroot'], scan, segment, candint, dmind, dtind)

                if any([candsnr > snr for candsnr in candprops[:, snrcol]]):
                    logger.info('Cand {0} had SNR {1} and refinement found a higher SNR in new ones: {2}.'.format(i, snr, candprops[:, snrcol]))
                    logger.info('Saving to {0}: {1}'.format(candfile, cands))

                    with open(candfile, 'w') as pkl:
                        pickle.dump(d, pkl, protocol=2)
                        pickle.dump((candlocs, candprops), pkl, protocol=2)
                else:
                    logger.info('Cand {0} had SNR {1}, but refinement found no improvement: {2}'.format(i, snr, candprops[:, snrcol]))