Esempio n. 1
0
def FFICut(ffis, x, y, size):
    ncads = len(ffis['FFIs'])

    x = int(x)
    y = int(y)

    xshape = ffis['FFIs'].shape[1]
    yshape = ffis['FFIs'].shape[2]

    x1 = np.max([0, x - size // 2])
    x2 = np.min([xshape, x + size // 2 + 1])
    y1 = np.max([0, y - size // 2])
    y2 = np.min([yshape, y + size // 2 + 1])

    aflux = ffis['FFIs'][0:ncads, x1:x2, y1:y2]
    aerrs = ffis['errs'][0:ncads, x1:x2, y1:y2]

    boxing = TargetPixelFileFactory(n_cadences=ncads,
                                    n_rows=aflux.shape[1],
                                    n_cols=aflux.shape[2])

    for i, f in enumerate(aflux):
        ti = ffis['data'][0, i]
        tf = ffis['data'][1, i]
        b = ffis['data'][2, i]
        q = ffis['data'][3, i]

        header = {'TSTART': ti, 'TSTOP': tf, 'QUALITY': q}

        boxing.add_cadence(frameno=i, flux=f, flux_err=aerrs[i], header=header)

    TPF = boxing.get_tpf()

    return TPF
Esempio n. 2
0
def bite(target, sector=None, shape=(5, 5)) -> TargetPixelFile:
    """Returns a target pixel file."""
    from lightkurve.targetpixelfile import TargetPixelFileFactory

    crd = locate(target=target, sector=sector)[0]
    n_cadences = 1
    urls = [crd.get_images()[idx].url for idx in range(n_cadences)]
    factory = TargetPixelFileFactory(n_cadences=n_cadences,
                                     n_rows=shape[1],
                                     n_cols=shape[0])
    for idx, url in enumerate(urls):
        flux = bite_ffi(url, col=crd.column, row=crd.row, shape=shape)
        factory.add_cadence(idx, flux=flux)
    tpf = factory.get_tpf(hdu0_keywords={
        "TELESCOP": "TESS",
        "CREATOR": "TessTargetPixelFile"
    })
    tpf.quality_mask = [True] * n_cadences
    tpf.get_header(1)["1CRV5P"] = 0
    tpf.get_header(1)["2CRV5P"] = 0
    return tpf
Esempio n. 3
0
def FFICut(ffis, x, y, size):
    with h5py.File(ffis, 'r', libver='latest') as ffis:

        ncads = len(ffis['FFIs'])
        x = int(x)
        y = int(y)

        #aflux  = np.transpose(ffis['FFIs'], axes=[2,0,1])[:, x-size//2:x+size//2+1, y-size//2:y+size//2+1]
        #aerrs  = np.transpose(ffis['errs'], axes=[2,0,1])[:, x-size//2:x+size//2+1, y-size//2:y+size//2+1]
        aflux = ffis['FFIs'][:, x - size // 2:x + size // 2 + 1,
                             y - size // 2:y + size // 2 + 1]
        aerrs = ffis['errs'][:, x - size // 2:x + size // 2 + 1,
                             y - size // 2:y + size // 2 + 1]

        boxing = TargetPixelFileFactory(n_cadences=ncads,
                                        n_rows=size,
                                        n_cols=size)

        for i, f in enumerate(tqdm(aflux)):
            ti = ffis['data'][0, i]
            tf = ffis['data'][1, i]
            b = ffis['data'][2, i]
            q = ffis['data'][3, i]

            header = {'TSTART': ti, 'TSTOP': tf, 'QUALITY': q}

            boxing.add_cadence(frameno=i,
                               flux=f,
                               flux_err=aerrs[i],
                               header=header)

    TPF = boxing.get_tpf()
    #TPF.hdu[1].data['QUALITY']   = ffis['data'][2]
    #TPF.hdu[1].data['TIME']      = ffis['data'][0]
    #TPF.hdu[1].header['BJDREFI'] = hdr['BJDREFI']
    #TPF.hdu[1].data.columns['TIME'].unit = 'BJD - %d' % hdr['BJDREFI']

    return TPF
def test_tpf_factory():
    """Can we create TPFs using TargetPixelFileFactory?"""
    from lightkurve.targetpixelfile import FactoryError

    factory = TargetPixelFileFactory(n_cadences=10, n_rows=6, n_cols=8)
    flux_0 = np.ones((6, 8))
    factory.add_cadence(frameno=0,
                        flux=flux_0,
                        header={
                            "TSTART": 0,
                            "TSTOP": 10
                        })
    flux_9 = 3 * np.ones((6, 8))
    factory.add_cadence(frameno=9,
                        flux=flux_9,
                        header={
                            "TSTART": 90,
                            "TSTOP": 100
                        })

    # You shouldn't be able to build a TPF like this...because TPFs shouldn't
    # have extensions where time stamps are duplicated (here frames 1-8 will have)
    # time stamp zero
    with pytest.warns(LightkurveWarning, match="identical TIME values"):
        tpf = factory.get_tpf()
    [
        factory.add_cadence(frameno=i,
                            flux=flux_0,
                            header={
                                "TSTART": i * 10,
                                "TSTOP": (i * 10) + 10
                            }) for i in np.arange(2, 9)
    ]

    # This should fail because the time stamps of the images are not in order...
    with pytest.warns(LightkurveWarning, match="chronological order"):
        tpf = factory.get_tpf()

    [
        factory.add_cadence(frameno=i,
                            flux=flux_0,
                            header={
                                "TSTART": i * 10,
                                "TSTOP": (i * 10) + 10
                            }) for i in np.arange(1, 9)
    ]

    # This should pass
    tpf = factory.get_tpf(hdu0_keywords={"TELESCOP": "TESS"})

    assert_array_equal(tpf.flux[0].value, flux_0)
    assert_array_equal(tpf.flux[9].value, flux_9)

    tpf = factory.get_tpf(hdu0_keywords={"TELESCOP": "Kepler"})

    assert_array_equal(tpf.flux[0].value, flux_0)
    assert_array_equal(tpf.flux[9].value, flux_9)
    assert tpf.time[0].value == 5
    assert tpf.time[9].value == 95

    # Can you add the WRONG sized frame?
    flux_wrong = 3 * np.ones((6, 9))
    with pytest.raises(FactoryError):
        factory.add_cadence(frameno=2,
                            flux=flux_wrong,
                            header={
                                "TSTART": 90,
                                "TSTOP": 100
                            })

    # Can you add the WRONG cadence?
    flux_wrong = 3 * np.ones((6, 8))
    with pytest.raises(FactoryError):
        factory.add_cadence(frameno=11,
                            flux=flux_wrong,
                            header={
                                "TSTART": 90,
                                "TSTOP": 100
                            })

    # Can we add our own keywords?
    tpf = factory.get_tpf(hdu0_keywords={
        "creator": "Christina TargetPixelFileWriter",
        "TELESCOP": "TESS"
    })
    assert tpf.get_keyword("CREATOR") == "Christina TargetPixelFileWriter"