How to use the lightkurve.KeplerTargetPixelFile.from_fits_images function in lightkurve

To help you get started, we’ve selected a few lightkurve examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github afeinstein20 / eleanor / ELLIE_v1.2 / customTPFs.py View on Github external
def make_postcard(self):
        fns = self.sort_by_date()
        fns = [self.dir+i for i in fns]
        mast, mheader = fits.getdata(fns[0], header=True)
        x, y = np.linspace(0, len(mast), 4, dtype=int), np.linspace(0, len(mast[0]), 4, dtype=int)
        x_cens, y_cens = [], []
        cat = 'postcard_catalog.txt'
        
        for i in range(len(x)-1):
            for j in range(len(y)-1):
                fn = 'postcard_{}_{}-{}_{}-{}.fits'.format(self.sector, self.camera, self.chip, i, j)
                x_cen = (x[i]+x[i+1]) / 2.
                y_cen = (y[j]+y[j+1]) / 2.
                
                radec = WCS(mheader).all_pix2world(x_cen, y_cen, 1)
                tpf   = ktpf.from_fits_images(images=fns, position=(x_cen,y_cen), size=(350,350))
                tpf.to_fits(output_fn=fn)
                
                fits.setval(fn, 'CEN_X' , value=np.round(x_cen,5))
                fits.setval(fn, 'CEN_Y' , value=np.round(y_cen,5))
                fits.setval(fn, 'CEN_RA', value=float(radec[0]))
                fits.setval(fn, 'CEN_DEC', value=float(radec[1]))

                lower = WCS(mheader).all_pix2world(x_cen-350, y_cen-350, 1)
                upper = WCS(mheader).all_pix2world(x_cen+350, y_cen+350, 1)
                row = [fn, lower[0], lower[1], upper[0], upper[1]]
                with open(cat, 'a') as tf:
                    tf.write('{}\n'.format(' '.join(str(e) for e in row)))
        return
github afeinstein20 / eleanor / ELLIE / ellie.py View on Github external
typeList = np.full(len(colnames), 'S30', dtype='S30')
        t = Table(names=colnames, dtype=typeList)

        for i in range(len(x)-1):
            for j in range(len(y)-1):
                mast, mheader = fits.getdata(fns[0], header=True)
                fn = 'postcard_{}_{}-{}_{}-{}.fits'.format(sector, camera, chip, i, j)
                if os.path.isfile(self.post_dir+fn)==True:
                    return
                print("Creating postcard: {}".format(fn))
                x_cen = (x[i]+x[i+1]) / 2.
                y_cen = (y[j]+y[j+1]) / 2.

                radec = WCS(mheader).all_pix2world(x_cen, y_cen, 1)
                s = 300
                tpf   = ktpf.from_fits_images(images=fns, position=(x_cen,y_cen), size=(s,s))
                # Edits header of FITS files
                tempVals = list(mheader.values())
                moreData = [fn, s, s, x_cen, y_cen, float(radec[0]), float(radec[1])]
                for m in moreData:
                    tempVals.append(m)
                t.add_row(vals=tempVals)

                hdr = mheader
                hdr.append(('COMMENT', '***********************'))
                hdr.append(('COMMENT', '*     ELLIE INFO      *'))
                hdr.append(('COMMENT', '***********************'))
                hdr.append(('AUTHOR' , 'Adina D. Feinstein'))
                hdr.append(('VERSION', '1.0'))
                hdr.append(('GITHUB' , 'https://github.com/afeinstein20/ELLIE'))
                hdr.append(('CREATED', strftime('%Y-%m-%d'),
                            'ELLIE file creation date (YYY-MM-DD)'))
github afeinstein20 / eleanor / ELLIE_v1.2 / oneSource.py View on Github external
def main(id, mission):
    """ Temporary main function """
    info = from_class(id, mission)

    pos = [info['RA'].data[0], info['Dec'].data[0]]
    files, xy, camera, chip = find_camera_chip(id, pos)

    # Shifts (x,y) coordinates based on pointing model
    initShift = np.loadtxt('pointingModel_{}-{}.txt'.format(camera, chip), skiprows=1,
                           usecols=(1,2,3))[0]
    initShift[0] = np.radians(initShift[0])
    x = xy[0]*np.cos(initShift[0]) - xy[1]*np.sin(initShift[0]) - initShift[1]
    y = xy[0]*np.sin(initShift[0]) + xy[1]*np.cos(initShift[0]) - initShift[2]

    # Creates first TPF 
    tpf = ktpf.from_fits_images(images=files, position=[x,y], size=(9,9))

    # Shifts (x,y) coordinates to center on centroid
    xy_new = add_shift(tpf)
    xy_new_start = [x+xy_new[0]-4.0, y+xy_new[1]-4.0]
    new_tpf = ktpf.from_fits_images(images=files, position=xy_new_start, size=(9,9))

    # Converts new (x,y) center to world coordinates
    flux, header = fits.getdata(files[0], header=True)
    pos = WCS(header).all_pix2world(xy_new_start[0], xy_new_start[1], 1)
    pos = [float(pos[0]), float(pos[1])]

    # Saves TPF to FITS file and edits header
    output_fn = 'TIC{}_tpf.fits'.format(id)
    new_tpf.to_fits(output_fn=output_fn)
    print(info)
    edit_header(output_fn, int(info['TIC_ID']), int(info['Gaia_ID']), pos, xy_new_start)
github afeinstein20 / eleanor / ELLIE_v1.2 / customTPFs.py View on Github external
yRot = xy[:,0]*np.sin(theta) + xy[:,1]*np.cos(theta)+yT

                dist = np.sqrt((xRot-centroids[:,0])**2 + (yRot-centroids[:,1])**2)
                return np.sum(np.square(dist))

            fns = np.array([self.dir+i for i in fns])
            x_cen, y_cen = len(mast)/2., len(mast[0])/2.
            xy = np.zeros((len(x),2))
            matrix = np.zeros((len(x), len(fns), 2))

            with open(self.corrFile, 'w') as tf:
                tf.write('cadence medT medX medY\n')

            for i in range(len(x)):
                xy[i][0], xy[i][1] = x[i]-x_cen, y[i]-y_cen
                tpf = ktpf.from_fits_images(images=fns, position=(x[i],y[i]), size=(6,6))
                for j in range(len(fns)):
                    com = ndimage.measurements.center_of_mass(tpf.flux[j].T-np.median(tpf.flux[j])) # subtracts bkg
                    matrix[i][j][0] = com[0]+xy[i][0]
                    matrix[i][j][0] = com[1]+xy[i][1]
            
            for i in range(len(fns)):
                centroids = matrix[:,i]
                if i == 0:
                    initGuess = [0.001, 0.1, -0.1]
                else:
                    initGuess = solution.x
                bnds = ((-0.08, 0.08), (-5.0, 5.0), (-5.0, 5.0))
                solution = minimize(model, initGuess, method='L-BFGS-B', bounds=bnds, options={'ftol':5e-11,
                                                                                               'gtol':5e-05})
                sol = solution.x
github afeinstein20 / eleanor / ELLIE / oneSource.py View on Github external
def main(id, mission):
    """ Temporary main function """
    info = from_class(id, mission)

    pos = [info['RA'].data[0], info['Dec'].data[0]]
    files, xy, camera, chip = find_camera_chip(id, pos)

    # Shifts (x,y) coordinates based on pointing model
    initShift = np.loadtxt('pointingModel_{}-{}.txt'.format(camera, chip), skiprows=1,
                           usecols=(1,2,3))[0]
    initShift[0] = np.radians(initShift[0])
    x = xy[0]*np.cos(initShift[0]) - xy[1]*np.sin(initShift[0]) - initShift[1]
    y = xy[0]*np.sin(initShift[0]) + xy[1]*np.cos(initShift[0]) - initShift[2]

    # Creates first TPF 
    tpf = ktpf.from_fits_images(images=files, position=[x,y], size=(9,9))

    # Shifts (x,y) coordinates to center on centroid
    xy_new = add_shift(tpf)
    xy_new_start = [x+xy_new[0]-4.0, y+xy_new[1]-4.0]
    new_tpf = ktpf.from_fits_images(images=files, position=xy_new_start, size=(9,9))

    # Converts new (x,y) center to world coordinates
    flux, header = fits.getdata(files[0], header=True)
    pos = WCS(header).all_pix2world(xy_new_start[0], xy_new_start[1], 1)
    pos = [float(pos[0]), float(pos[1])]

    # Saves TPF to FITS file and edits header
    output_fn = 'TIC{}_tpf.fits'.format(id)
#    new_tpf.to_fits(output_fn=output_fn)
    print(xy_new_start)
    mast = fits.getdata(files[0])