def optimalAperture(t_time, t_fluxarr, t_quality, qual_cut=False, return_qual=False, toss_resat=False, bg_cut=5, skip=0): """ This routine determines an optimal apertures and outputs the flux (i.e. a light curve) from a TPF. Inputs: ------------ t_time = 1D array of 'TIME' from TPF t_fluxarr = 1D array of 'FLUX' from TPF t_quality = 1D array of 'QUALITY' from TPF qual_cut = exclude cadences with a non-zero quality flag; this is False by default return_qual = if True then nothing is returned; this is True by default toss_resat = exclude cadences where there is a wheel resaturation event; this is True by default bg_cut = threshold to find pixels that are bg_cut * MAD above the median skip = index of first cadence that should be used in the time series Outputs: ------------ time = 1D array of time in BKJD lc = 1D array of flux measured in optimal aperture xbar = 1D array of x-coordinate of target centroid ybar = 1D array of y-coordinate of target centroid regnum = integer value of brightest pixel lab = 2D array identifying pixels used in aperture Usage: ------------ tpf,tpf_hdr = ar.getLongTpf(k2id, campaign, header=True) tpf_time = tpf['TIME'] tpf_flux = tpf['FLUX'] tpf_quality = tpf['QUALITY'] time,lc,xbar,ybar,regnum,lab = optimalAperture(tpf_time, tpf_flux, tpf_quality, qual_cut=False, return_qual=False, toss_resat=True, bg_cut=5, skip=0) """ time = t_time[skip:] fluxarr = t_fluxarr[skip:] quality = t_quality[skip:] if qual_cut: time = time[quality == 0] fluxarr = fluxarr[quality == 0, :, :] elif toss_resat: # cadences where there is a wheel resaturation event time = time[quality != 32800] fluxarr = fluxarr[quality != 32800, :, :] #remove any nans try: fluxarr[fluxarr == 0] = np.nan except ValueError: pass #subtract background flux_b = bg_sub(fluxarr) # create a median image to calculate where the pixels to use are flatim = np.nanmedian(flux_b, axis=0) #find pixels that are X MAD above the median vals = flatim[np.isfinite(flatim)].flatten() mad_cut = 1.4826 * MAD(vals) * bg_cut flatim[np.isnan(flatim)] = 0. region = np.where(flatim > mad_cut, 1, 0) lab = label(region)[0] #find the central pixel imshape = np.shape(flatim) centralpix = [1 + imshape[0] // 2, 1 + imshape[1] // 2] #find brightest pix within 9x9 of central pix #this assumes target is at center of postage stamp which I think is ok centflatim = flatim[centralpix[0] - 2:centralpix[0] + 2, centralpix[1] - 2:centralpix[1] + 2] flatimfix = np.where(np.isfinite(centflatim), centflatim, 0) brightestpix = np.unravel_index(flatimfix.argmax(), centflatim.shape) bpixy, bpixx = brightestpix #use all pixels in the postage stamp that are X MAD above the median #this identifies location of brightest pixel only regnum = lab[centralpix[0] - 2 + bpixy, centralpix[1] - 2 + bpixx] lc = np.zeros_like(time) xbar = np.zeros_like(time) ybar = np.zeros_like(time) #make a rectangular aperture for the moments thing ymin = np.min(np.where(lab == regnum)[0]) ymax = np.max(np.where(lab == regnum)[0]) xmin = np.min(np.where(lab == regnum)[1]) xmax = np.max(np.where(lab == regnum)[1]) momlims = [ymin, ymax + 1, xmin, xmax + 1] #loop that performs the aperture photometry for i, fl in enumerate(flux_b): lc[i] = np.sum(fl[lab == regnum]) #lc[i] = np.sum(fl[np.where(lab == 1)] momim = fl[momlims[0]:momlims[1], momlims[2]:momlims[3]] momim[~np.isfinite(momim)] == 0.0 xbar[i], ybar[i], cov = intertial_axis(momim) if return_qual: return None else: # TODO: think about whether this should be normalized return (time, lc, xbar - np.nanmean(xbar), ybar - np.nanmean(ybar), regnum, lab)
def run_C0_data_extract(fn, second_half_only=True, qual_cut=False, return_qual=False, toss_resat=True, bg_cut=5, skip_cads=None, skip=None): #for C0, lets just ise the good data post safe mode if second_half_only and skip_cads == None: skip = 1976 elif skip is not None: skip = skip_cads else: skip = 0 with pyfits.open(fn) as f: time = f[1].data['TIME'][skip:] - f[1].data['TIME'][0] fluxarr = f[1].data['FLUX'][skip:] quality = f[1].data['QUALITY'][skip:] if qual_cut: time = time[quality == 0] fluxarr = fluxarr[quality == 0,:,:] elif toss_resat: # data the cadences where there is a wheel # resetuation event time = time[quality != 32800] fluxarr = fluxarr[quality != 32800,:,:] # fix dodgy data # the first C0 data release included zeros # this will be changed later but we need this # fix for now fluxarr[fluxarr == 0] = np.nan #subtract background flux_b = bg_sub(fluxarr) # create a median image to calculate where # the pixels to use are: is this good? # this needs to be investiagated. flatim = np.nanmedian(flux_b,axis=0) #fixd pixels that are X MAD above the median vals = flatim[np.isfinite(flatim)].flatten() mad_cut = 1.4826 * MAD(vals) * bg_cut region = np.where(flatim > mad_cut,1,0) lab = label(region)[0] #find the central pixel imshape = np.shape(flatim) centralpix = [1+imshape[0] // 2,1+imshape[1] // 2] #find brightest pix within 9x9 of central pix centflatim = flatim[centralpix[0]-4:centralpix[0]+4, centralpix[1]-4:centralpix[1]+4] flatimfix = np.where(np.isfinite(centflatim),centflatim,0) brightestpix = np.unravel_index(flatimfix.argmax(), centflatim.shape) bpixy, bpixx = brightestpix regnum = lab[centralpix[0]-4+bpixy,centralpix[1]-4+bpixx] if regnum == 0: print('WARNING, no star was found in light curve, \ {} light curve will be junk!'.format(fn)) lc = np.zeros_like(time) xbar = np.zeros_like(time) ybar = np.zeros_like(time) #there is a loop that performs the aperture photometry #lets also calcualte the moments of the image #make a rectangular aperture for the moments thing ymin = np.min(np.where(lab == regnum)[0]) ymax = np.max(np.where(lab == regnum)[0]) xmin = np.min(np.where(lab == regnum)[1]) xmax = np.max(np.where(lab == regnum)[1]) #print(np.where(lab == regnum)) momlims = [ymin,ymax+1,xmin,xmax+1] for i,fl in enumerate(fluxarr): lc[i] = np.sum(fl[lab == regnum]) momim = fl[momlims[0]:momlims[1], momlims[2]:momlims[3]] momim[~np.isfinite(momim)] == 0.0 xbar[i], ybar[i], cov = intertial_axis(momim) if return_qual: return None else: return (time,lc, xbar / np.mean(xbar), ybar / np.mean(xbar), regnum)
def optimalAperture(t_time, t_fluxarr, t_quality, qual_cut=False, return_qual=False, toss_resat=False, bg_cut=5, skip=0): """ This routine determines an optimal apertures and outputs the flux (i.e. a light curve) from a TPF. Inputs: ------------ t_time = 1D array of 'TIME' from TPF t_fluxarr = 1D array of 'FLUX' from TPF t_quality = 1D array of 'QUALITY' from TPF qual_cut = exclude cadences with a non-zero quality flag; this is False by default return_qual = if True then nothing is returned; this is True by default toss_resat = exclude cadences where there is a wheel resaturation event; this is True by default bg_cut = threshold to find pixels that are bg_cut * MAD above the median skip = index of first cadence that should be used in the time series Outputs: ------------ time = 1D array of time in BKJD lc = 1D array of flux measured in optimal aperture xbar = 1D array of x-coordinate of target centroid ybar = 1D array of y-coordinate of target centroid regnum = integer value of brightest pixel lab = 2D array identifying pixels used in aperture Usage: ------------ tpf,tpf_hdr = ar.getLongTpf(k2id, campaign, header=True) tpf_time = tpf['TIME'] tpf_flux = tpf['FLUX'] tpf_quality = tpf['QUALITY'] time,lc,xbar,ybar,regnum,lab = optimalAperture(tpf_time, tpf_flux, tpf_quality, qual_cut=False, return_qual=False, toss_resat=True, bg_cut=5, skip=0) """ time = t_time[skip:] fluxarr = t_fluxarr[skip:] quality = t_quality[skip:] if qual_cut: time = time[quality == 0] fluxarr = fluxarr[quality == 0,:,:] elif toss_resat: # cadences where there is a wheel resaturation event time = time[quality != 32800] fluxarr = fluxarr[quality != 32800,:,:] #remove any nans fluxarr[fluxarr == 0] = np.nan #subtract background flux_b = bg_sub(fluxarr) # create a median image to calculate where the pixels to use are flatim = np.nanmedian(flux_b,axis=0) #find pixels that are X MAD above the median vals = flatim[np.isfinite(flatim)].flatten() mad_cut = 1.4826 * MAD(vals) * bg_cut flatim[np.isnan(flatim)] = 0. region = np.where(flatim > mad_cut,1,0) lab = label(region)[0] #find the central pixel imshape = np.shape(flatim) centralpix = [1+imshape[0] // 2,1+imshape[1] // 2] #find brightest pix within 9x9 of central pix #this assumes target is at center of postage stamp which I think is ok centflatim = flatim[centralpix[0]-4:centralpix[0]+4, centralpix[1]-4:centralpix[1]+4] flatimfix = np.where(np.isfinite(centflatim),centflatim,0) brightestpix = np.unravel_index(flatimfix.argmax(), centflatim.shape) bpixy, bpixx = brightestpix #use all pixels in the postage stamp that are X MAD above the median #this identifies location of brightest pixel only regnum = lab[centralpix[0]-4+bpixy,centralpix[1]-4+bpixx] lc = np.zeros_like(time) xbar = np.zeros_like(time) ybar = np.zeros_like(time) #make a rectangular aperture for the moments thing ymin = np.min(np.where(lab == regnum)[0]) ymax = np.max(np.where(lab == regnum)[0]) xmin = np.min(np.where(lab == regnum)[1]) xmax = np.max(np.where(lab == regnum)[1]) momlims = [ymin,ymax+1,xmin,xmax+1] #loop that performs the aperture photometry for i,fl in enumerate(flux_b): lc[i] = np.sum(fl[lab == regnum]) #lc[i] = np.sum(fl[np.where(lab == 1)] momim = fl[momlims[0]:momlims[1], momlims[2]:momlims[3]] momim[~np.isfinite(momim)] == 0.0 xbar[i], ybar[i], cov = intertial_axis(momim) if return_qual: return None else: # TODO: think about whether this should be normalized return (time,lc, xbar - np.nanmean(xbar), ybar - np.nanmean(ybar), regnum, lab)