for file in files:
    fitsTable = fits.open(file, memmap=True)
    objName = fitsTable[0].header['OBJECT']
    print("\nReading in " + objName + " Filename: " + file)
    try:
        curveTable = Table(fitsTable[1].data).to_pandas()
    except:
        print('*************** ERROR ***************')
        f = open('errors.txt', 'a')
        f.write(file + '\n')
        f.close()
    else:
        curveData = curveTable.loc[curveTable['QUALITY'] == 0].dropna(
            subset=['TIME']).dropna(subset=['PDCSAP_FLUX']).copy()
        curveData = curveTable.filter(
            ['TIME', 'PDCSAP_FLUX', 'PDCSAP_FLUX_ERR'])

        idx = np.where(
            (curveData['TIME'][1:] - curveData['TIME'][:-1]).isnull())[0]
        idxL = idx[np.where(idx[1:] - idx[:-1] > 1)]
        idxR = idx[np.where(idx[1:] - idx[:-1] > 1)[0] + 1]

        for badDataPoint in idxL:
            # Set data points to the right to null
            r = range(badDataPoint + 1, badDataPoint + 501)

            try:
                curveData.loc[r, 'PDCSAP_FLUX'] = None
                curveData.loc[r, 'TIME'] = None
            except:
                pass