#!/usr/bin/env python # coding: utf-8 # # What are TargetPixelFile objects? # Target Pixel Files (TPFs) are a file common to Kepler/K2 and the TESS mission. They contain data that is usually centered around a single star. # # TPFs can be thought of as stacks of images, with one image for every timestamp the telescope took data. Each timestamp is referred to as a **cadence**. These images are cut out 'postage stamps' of the full observation to make them easier to work with. # # TPFs are given in FITS files, which you can read more about [here](https://fits.gsfc.nasa.gov/fits_primer.html). `lightkurve` includes tools for you to work directly with these files easily and intuitively. # # In this tutorial we'll cover the basics of working with TPFs. In lightcurve there are classes to work with each mission. For example `KeplerTargetPixelFile` is used to work with data from the Kepler (and K2) mission. `TessTargetPixelFile` is used to work with data from the TESS mission. # # We'll use a Kepler TPF as an example. First, let's open a file. We can do this easily with the `KeplerTargetPixelFile.from_archive` function. This will retrieve the data from the [MAST data archive](https://archive.stsci.edu/kepler/), which holds all of the Kepler and K2 data. # In this case we are downloading the Target Pixel File with the Kepler ID 6922244 for Quarter 4 (Kepler's observations were split into quarters of a year). # You can also download a file with `from_archive` using the name of the target, or using the astronomical coordinates (Right Ascension and Declination, often referred to as "RA" and "Dec"). # In[1]: from lightkurve import KeplerTargetPixelFile tpf = KeplerTargetPixelFile.from_archive(6922244, quarter=4) # We can access lots of meta data using this object very simply with properties of the `KeplerTargetPixelFile` object. For example, we can find the mission name, and the quarter that the data was taken in by typing the following: # In[2]: tpf.mission # In[3]: tpf.quarter # You can find the full list of properties in the [API documentation](http://lightkurve.keplerscience.org/api/targetpixelfile.html). # The most interesting data in a `KeplerTargetPixelFile` object are the `flux` and `time` values which give access to the brightness of the observed target over time. You can access the timestamps of the observations using the `time` property: # In[4]: tpf.time # By default, `time` is in the Kepler-specific *Barycentric Kepler Julian Day* format (BKJD). You can easily convert this into [AstroPy Time objects](http://docs.astropy.org/en/stable/time/) using the `astropy_time` property: # In[5]: tpf.astropy_time # In turn, this gives you access to human-readable ISO timestamps using the `astropy_time.iso` property: # In[6]: tpf.astropy_time.iso # **Beware:** these ISO timestamps are in the Barycentric frame, and do not include corrections for light travel time or leap seconds. # # Next, let's look at the actual image data, which is available via the `flux` property: # In[7]: tpf.flux.shape # The `flux` data is a 4116x5x5 array in units electrons/second. The first axis is the time axis, and the images themselves are 5 pixels by 5 pixels. You can use the `plot` method on the `KeplerTargetPixelFile` object to view the data. (By default, this will show just one cadence of the data. But you can pass the cadence you want to look at to the `frame` keyword if you would like to check a particular flux point for thruster firings, cosmic rays or asteroids.) # In[8]: get_ipython().run_line_magic('matplotlib', 'inline') tpf.plot(frame=0); # The values shown in this image are also directly accessible as an array: # In[9]: tpf.flux[0] # You can use normal `numpy` methods on these to find the shape, mean etc! # We can now turn this Target Pixel File into a light curve, with a single flux value for every time value. Each of the pixels are 4 arcseconds across. The point spread function (PSF) of the telescope causes the light from the star fall onto several different pixels, which can be seen in the image above. Because of this spreading, we have to sum up many pixels to collect all the light from the source. To do this we sum up all the pixels in an **aperture**. An aperture is a pixel mask, where we take only the pixels related to the target. # The *Kepler* pipeline adds an aperture mask to each target pixel file. This aperture determines which pixels are summed to create a 1D light curve of the target. There are some science cases where you might want to create a different aperture. For example, there may be a nearby contaminant or you may want to measure the background. # # The standard pipeline aperture is easily accessed in a `KeplerTargetPixelFile` object using `tpf.pipeline_mask`, which is a boolean array: # In[10]: tpf.pipeline_mask # We can also plot this aperture over the target pixel file above to see if the flux of the star is all contained within the aperture. # In[11]: tpf.plot(aperture_mask=tpf.pipeline_mask) # Now that we have the aperture we can create a Simple Aperture Photometry light curve in the next tutorial. # Finally, note that you can inspect all the raw metadata of the target by taking a look at the 'header' of the FITS file, which contains information about the data set. Let's just print the first 10 lines: # In[12]: tpf.header()[:10] # We can look at the values in the second extention of the fits file by passing an extention number to the `header` function. For example, to look at all the column titles: # In[13]: tpf.header(1)['TTYPE*']