<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 What's new in PyKE 3?
Developed since 2012, PyKE offers a user-friendly way to inspect and analyze the pixels and lightcurves obtained by NASA's Kepler and K2.
The latest version of PyKE, v3.1, was released in January 2018 and adds a new object-oriented Python API which is intended to aid the development of custom pipelines and tools by the community.
Step1:  Introducing a generic LightCurve class
The most notable change is the introduction of a generic LightCurve class which provides operations that are intended to suit time series data from any astronomical survey. A light curve is simply instantiated as follows
Step2:  A LightCurve object provides easy access to a range of common operations, such as fold(), flatten(), remove_outliers(), cdpp(), plot(), and more.  To demonstrate these operations, let's create a LightCurve object from a KeplerLightCurveFile we obtain from the data archive at MAST
Step3:  Now lc is a LightCurve object on which you can run operations. For example, we can plot it
Step4:  We can access several of the metadata properties
Step5:  We can access the time and flux as arrays
Step6:  We don't particularly care about the long-term trends, so let's use a Savitzky-Golay filter to flatten the lightcurve
Step7:  We can also compute the CDPP noise metric
Step8:  Target Pixel File (TPF)
PyKE 3.1 includes class called KeplerTargetPixelFile which is used to handle target pixel files
Step9:  A KeplerTargetPixelFile can be instantiated either from a local file or a url
Step10:  Additionally, we can mask out cadences that are flagged using the quality_bitmask argument in the constructor
Step11:  Furthermore, we can mask out pixel values using the aperture_mask argument. The default behaviour is to use
all pixels that have real values. This argument can also get a string value 'kepler-pipeline', in which case the default aperture used by Kepler's pipeline is applied.
Step12:  The TPF objects stores both data and a few metadata information, e.g., channel number, EPIC number, reference column and row, module, and shape. The whole header is also available
Step13:  The pixel fluxes time series can be accessed using the flux property
Step14:  This shows that our TPF is a 35 x 35 image recorded over 3209 cadences.
One can visualize the pixel data at a given cadence using the plot method
Step15:  We can perform aperture photometry using the method to_lightcurve
Step16:  Let's see how the previous light curve compares against the 'SAP_FLUX' produced by Kepler's pipeline. For that, we are going to explore the KeplerLightCurveFile class
Step17:  Now, let's correct this light curve using by fitting cotrending basis vectors. That can be achived either with the KeplerCBVCorrector class or the compute_cotrended_lightcurve in KeplerLightCurveFile. Let's try the latter
Step18:  Utility functions
PyKE has included two convinience functions to convert between module.output to channel and vice-versa
Step19:  PyKE 3.1 includes KeplerQualityFlags class which encodes the meaning of the Kepler QUALITY bitmask flags as documented in the Kepler Archive Manual (Table 2.3)
Step20:  It also can handle multiple flags
Step21:  A few quality flags are already computed
Step22:  Pixel Response Function (PRF) Photometry
PyKE 3.1 also includes tools to perform PRF Photometry
Step23:  For that, let's create a SceneModel which will be fitted to the object of the following TPF
Step24:  We also need to define prior distributions on the parameters of our SceneModel model. Those parameters are
the flux, center positions of the target, and a constant background level. We can do that with oktopus
<ASSISTANT_TASK:>
Python Code:
%load_ext autoreload
%autoreload 2
%matplotlib inline
from matplotlib import rcParams
rcParams["figure.figsize"] = (14, 5)
Explanation: What's new in PyKE 3?
Developed since 2012, PyKE offers a user-friendly way to inspect and analyze the pixels and lightcurves obtained by NASA's Kepler and K2.
The latest version of PyKE, v3.1, was released in January 2018 and adds a new object-oriented Python API which is intended to aid the development of custom pipelines and tools by the community.
End of explanation
from pyke import LightCurve
lc = LightCurve(time=[1, 2, 3], flux=[78.4, 79.6, 76.5])
Explanation: Introducing a generic LightCurve class
The most notable change is the introduction of a generic LightCurve class which provides operations that are intended to suit time series data from any astronomical survey. A light curve is simply instantiated as follows:
End of explanation
from pyke import KeplerLightCurveFile
lcfile = KeplerLightCurveFile("https://archive.stsci.edu/missions/kepler/lightcurves/0119/011904151/kplr011904151-2010009091648_llc.fits")
lc = lcfile.SAP_FLUX
Explanation: A LightCurve object provides easy access to a range of common operations, such as fold(), flatten(), remove_outliers(), cdpp(), plot(), and more.  To demonstrate these operations, let's create a LightCurve object from a KeplerLightCurveFile we obtain from the data archive at MAST:
End of explanation
lc.plot()
Explanation: Now lc is a LightCurve object on which you can run operations. For example, we can plot it:
End of explanation
lc.keplerid
lc.channel
lc.quarter
Explanation: We can access several of the metadata properties:
End of explanation
lc.time[:10]
lc.flux[:10]
Explanation: We can access the time and flux as arrays:
End of explanation
detrended_lc, _ = lc.flatten(polyorder=1)
detrended_lc.plot()
folded_lc = detrended_lc.fold(period=0.837495, phase=0.92)
folded_lc.plot();
Explanation: We don't particularly care about the long-term trends, so let's use a Savitzky-Golay filter to flatten the lightcurve:
End of explanation
lc.cdpp()
Explanation: We can also compute the CDPP noise metric:
End of explanation
from pyke import KeplerTargetPixelFile
Explanation: Target Pixel File (TPF)
PyKE 3.1 includes class called KeplerTargetPixelFile which is used to handle target pixel files:
End of explanation
tpf = KeplerTargetPixelFile('https://archive.stsci.edu/missions/k2/target_pixel_files/c14/'
                            '200100000/82000/ktwo200182949-c14_lpd-targ.fits.gz')
Explanation: A KeplerTargetPixelFile can be instantiated either from a local file or a url:
End of explanation
tpf = KeplerTargetPixelFile('https://archive.stsci.edu/missions/k2/target_pixel_files/c14/'
                            '200100000/82000/ktwo200182949-c14_lpd-targ.fits.gz',
                            quality_bitmask=KeplerQualityFlags.CONSERVATIVE_BITMASK)
Explanation: Additionally, we can mask out cadences that are flagged using the quality_bitmask argument in the constructor:
End of explanation
tpf = KeplerTargetPixelFile('https://archive.stsci.edu/missions/k2/target_pixel_files/c14/'
                            '200100000/82000/ktwo200182949-c14_lpd-targ.fits.gz',
                            aperture_mask='kepler-pipeline',
                            quality_bitmask=KeplerQualityFlags.CONSERVATIVE_BITMASK)
tpf.aperture_mask
Explanation: Furthermore, we can mask out pixel values using the aperture_mask argument. The default behaviour is to use
all pixels that have real values. This argument can also get a string value 'kepler-pipeline', in which case the default aperture used by Kepler's pipeline is applied.
End of explanation
tpf.header(ext=0)
Explanation: The TPF objects stores both data and a few metadata information, e.g., channel number, EPIC number, reference column and row, module, and shape. The whole header is also available:
End of explanation
tpf.flux.shape
Explanation: The pixel fluxes time series can be accessed using the flux property:
End of explanation
tpf.plot(frame=1)
Explanation: This shows that our TPF is a 35 x 35 image recorded over 3209 cadences.
One can visualize the pixel data at a given cadence using the plot method:
End of explanation
lc = tpf.to_lightcurve()
plt.figure(figsize=[17, 4])
plt.plot(lc.time, lc.flux)
Explanation: We can perform aperture photometry using the method to_lightcurve:
End of explanation
from pyke.lightcurve import KeplerLightCurveFile
klc = KeplerLightCurveFile('https://archive.stsci.edu/missions/k2/lightcurves/'
                           'c14/200100000/82000/ktwo200182949-c14_llc.fits',
                           quality_bitmask=KeplerQualityFlags.CONSERVATIVE_BITMASK)
sap_lc = klc.SAP_FLUX
plt.figure(figsize=[17, 4])
plt.plot(lc.time, lc.flux)
plt.plot(sap_lc.time, sap_lc.flux)
plt.ylabel('Flux (e-/s)')
plt.xlabel('Time (BJD - 2454833)')
Explanation: Let's see how the previous light curve compares against the 'SAP_FLUX' produced by Kepler's pipeline. For that, we are going to explore the KeplerLightCurveFile class:
End of explanation
klc_corrected = klc.compute_cotrended_lightcurve(cbvs=range(1, 17))
plt.figure(figsize=[17, 4])
plt.plot(klc_corrected.time, klc_corrected.flux)
plt.ylabel('Flux (e-/s)')
plt.xlabel('Time (BJD - 2454833)')
pdcsap_lc = klc.PDCSAP_FLUX
plt.figure(figsize=[17, 4])
plt.plot(klc_corrected.time, klc_corrected.flux)
plt.plot(pdcsap_lc.time, pdcsap_lc.flux)
plt.ylabel('Flux (e-/s)')
plt.xlabel('Time (BJD - 2454833)')
Explanation: Now, let's correct this light curve using by fitting cotrending basis vectors. That can be achived either with the KeplerCBVCorrector class or the compute_cotrended_lightcurve in KeplerLightCurveFile. Let's try the latter:
End of explanation
%matplotlib inline
import matplotlib.pyplot as plt
from pyke.utils import module_output_to_channel, channel_to_module_output
module_output_to_channel(module=19, output=3)
channel_to_module_output(67)
Explanation: Utility functions
PyKE has included two convinience functions to convert between module.output to channel and vice-versa:
End of explanation
from pyke.utils import KeplerQualityFlags
KeplerQualityFlags.decode(1)
Explanation: PyKE 3.1 includes KeplerQualityFlags class which encodes the meaning of the Kepler QUALITY bitmask flags as documented in the Kepler Archive Manual (Table 2.3):
End of explanation
KeplerQualityFlags.decode(1 + 1024 + 1048576)
Explanation: It also can handle multiple flags:
End of explanation
KeplerQualityFlags.decode(KeplerQualityFlags.DEFAULT_BITMASK)
KeplerQualityFlags.decode(KeplerQualityFlags.CONSERVATIVE_BITMASK)
Explanation: A few quality flags are already computed:
End of explanation
from pyke.prf import PRFPhotometry, SceneModel, SimpleKeplerPRF
Explanation: Pixel Response Function (PRF) Photometry
PyKE 3.1 also includes tools to perform PRF Photometry:
End of explanation
tpf = KeplerTargetPixelFile('https://archive.stsci.edu/missions/k2/target_pixel_files/c14/'
                            '201500000/43000/ktwo201543306-c14_lpd-targ.fits.gz',
                            quality_bitmask=KeplerQualityFlags.CONSERVATIVE_BITMASK)
tpf.plot(frame=100)
scene = SceneModel(prfs=[SimpleKeplerPRF(channel=tpf.channel, shape=tpf.shape[1:],
                                         column=tpf.column, row=tpf.row)])
Explanation: For that, let's create a SceneModel which will be fitted to the object of the following TPF:
End of explanation
from oktopus.prior import UniformPrior
unif_prior = UniformPrior(lb=[0, 1090., 706., 0.],
                          ub=[1e5, 1096., 712., 1e5])
scene.plot(*unif_prior.mean)
prf_phot = PRFPhotometry(scene_model=scene, prior=unif_prior)
results = prf_phot.fit(tpf.flux + tpf.flux_bkg)
plt.imshow(prf_phot.residuals[1], origin='lower')
plt.colorbar()
flux = results[:, 0]
xcenter = results[:, 1]
ycenter = results[:, 2]
bkg_density = results[:, 3]
plt.figure(figsize=[17, 4])
plt.plot(tpf.time, flux)
plt.ylabel('Flux (e-/s)')
plt.xlabel('Time (BJD - 2454833)')
plt.figure(figsize=[17, 4])
plt.plot(tpf.time, xcenter)
plt.ylabel('Column position')
plt.xlabel('Time (BJD - 2454833)')
plt.figure(figsize=[17, 4])
plt.plot(tpf.time, ycenter)
plt.ylabel('Row position')
plt.xlabel('Time (BJD - 2454833)')
plt.figure(figsize=[17, 4])
plt.plot(tpf.time, bkg_density)
plt.ylabel('Background density')
plt.xlabel('Time (BJD - 2454833)')
Explanation: We also need to define prior distributions on the parameters of our SceneModel model. Those parameters are
the flux, center positions of the target, and a constant background level. We can do that with oktopus:
End of explanation
<END_TASK>