Once you have detrended or altered a lightcurve in some way, you may want to save it as a FITS file. This allows you to easily share the file with your collaborators or submit your lightcurves as a MAST High Level Science Product (HLSP). Lightkurve provides a to_fits()
method which will easily convert your LightCurve
object into a fits file.
Below is a brief demostration showing how to_fits()
works.
Note: if you are considering contributing a HLSP you may want to read the guidelines for contributing fits files. These include which fits headers are required/suggested for your HLSP to be accepted.
First we'll obtain a random Kepler lightcurve from MAST.
from lightkurve import search_lightcurvefile
lcf = search_lightcurvefile('KIC 757076', quarter=3).download()
Now we'll make some edits to the lightcurve. Below we use the PDCSAP flux from MAST, remove NaN values and clip out any outliers.
lc = lcf.PDCSAP_FLUX.remove_nans().remove_outliers()
lc.scatter();
Now we can use the to_fits
method to save the lightcurve to a file called output.fits.
lc.to_fits(path='output.fits', overwrite=True)
Let's take a look at the file and check that it behaved as we expect
from astropy.io import fits
hdu = fits.open('output.fits')
type(hdu)
astropy.io.fits.hdu.hdulist.HDUList
hdu.info()
Filename: output.fits No. Name Ver Type Cards Dimensions Format 0 PRIMARY 1 PrimaryHDU 25 () 1 LIGHTCURVE 1 BinTableHDU 28 4133R x 7C [D, E, E, J, K, D, D]
hdu
is a set of astropy.io.fits objects, which is what we would expect. Lets take a look at the header of the first extension.
hdu[0].header
SIMPLE = T / conforms to FITS standards BITPIX = 8 / array data type NAXIS = 0 / number of array dimensions EXTEND = T / file contains extensions NEXTEND = 2 / number of standard extensions EXTNAME = 'PRIMARY ' / name of extension EXTVER = 1 / extension version number (not format version) ORIGIN = 'Unofficial data product' / institution responsible for file DATE = '2020-02-03' / file creation date. CREATOR = 'lightkurve.LightCurve.to_fits()' / pipeline job and program used t TELESCOP= 'KEPLER ' / telescope INSTRUME= 'Kepler Photometer' / detector type OBJECT = '757076 ' / string version of target id KEPLERID= 757076 / unique Kepler target identifier CHANNEL = 56 / CCD channel RADESYS = 'ICRS ' / reference frame of celestial coordinates RA_OBJ = 291.03872 / [deg] right ascension DEC_OBJ = 36.59813 / [deg] declination EQUINOX = 2000 / equinox of celestial coordinate system PROCVER = '1.8.dev ' MISSION = 'Kepler ' DATE-OBS= '2009-09-18T17:53:34.165' QUARTER = 3 CHECKSUM= 'RiUOTZTORfTORZTO' / HDU checksum updated 2020-02-03T16:54:18 DATASUM = '0 ' / data unit checksum updated 2020-02-03T16:54:18
Looks like it has all the correct information about the target. What about the second extension?
hdu[1].header
XTENSION= 'BINTABLE' / binary table extension BITPIX = 8 / array data type NAXIS = 2 / number of array dimensions NAXIS1 = 44 / length of dimension 1 NAXIS2 = 4133 / length of dimension 2 PCOUNT = 0 / number of group parameters GCOUNT = 1 / number of groups TFIELDS = 7 / number of table fields TTYPE1 = 'TIME ' TFORM1 = 'D ' TUNIT1 = 'bkjd ' TTYPE2 = 'FLUX ' TFORM2 = 'E ' TUNIT2 = 'counts ' TTYPE3 = 'FLUX_ERR' TFORM3 = 'E ' TUNIT3 = 'counts ' TTYPE4 = 'CADENCENO' TFORM4 = 'J ' TTYPE5 = 'SAP_QUALITY' TFORM5 = 'K ' TTYPE6 = 'MOM_CENTR1' TFORM6 = 'D ' TTYPE7 = 'MOM_CENTR2' TFORM7 = 'D ' EXTNAME = 'LIGHTCURVE' CHECKSUM= 'Y8EAa8C8T8CAY8C7' / HDU checksum updated 2020-02-03T16:54:18 DATASUM = '390244549' / data unit checksum updated 2020-02-03T16:54:18
This extension has 7 columns, TIME
, FLUX
, FLUX_ERR
, SAP_QUALITY
, CADENCENO
, MOM_CENTR1
, and MOM_CENTR2
. What if we wanted to add new keywords to our fits file? HLSP products require some extra keywords. Let's add some keywords to explain who made the data, and what our HLSP is.
lc.to_fits(path='output.fits',
overwrite=True,
HLSPLEAD='Kepler/K2 GO office',
HLSPNAME='TUTORIAL',
CITATION='HEDGES2018')
hdu = fits.open('output.fits')
hdu[0].header
SIMPLE = T / conforms to FITS standards BITPIX = 8 / array data type NAXIS = 0 / number of array dimensions EXTEND = T / file contains extensions NEXTEND = 2 / number of standard extensions EXTNAME = 'PRIMARY ' / name of extension EXTVER = 1 / extension version number (not format version) ORIGIN = 'Unofficial data product' / institution responsible for file DATE = '2020-02-03' / file creation date. CREATOR = 'lightkurve.LightCurve.to_fits()' / pipeline job and program used t TELESCOP= 'KEPLER ' / telescope INSTRUME= 'Kepler Photometer' / detector type OBJECT = '757076 ' / string version of target id KEPLERID= 757076 / unique Kepler target identifier CHANNEL = 56 / CCD channel RADESYS = 'ICRS ' / reference frame of celestial coordinates RA_OBJ = 291.03872 / [deg] right ascension DEC_OBJ = 36.59813 / [deg] declination EQUINOX = 2000 / equinox of celestial coordinate system PROCVER = '1.8.dev ' HLSPLEAD= 'Kepler/K2 GO office' HLSPNAME= 'TUTORIAL' CITATION= 'HEDGES2018' MISSION = 'Kepler ' DATE-OBS= '2009-09-18T17:53:34.165' QUARTER = 3 CHECKSUM= 'nMqboLnbnLnbnLnb' / HDU checksum updated 2020-02-03T16:54:18 DATASUM = '0 ' / data unit checksum updated 2020-02-03T16:54:18
Now our new keywords are included in the primary header! What about if we want to add more data columns to our fits file? We can simply add data columns in the same way. Let's add the data quality to our fits file.
demo_vector = lc.fold(1.23456789).phase
demo_vector
array([-0.49992399, -0.49952132, -0.49932792, ..., 0.49923243, 0.49957413, 0.49988861])
lc.to_fits(path='output.fits',
overwrite=True,
HLSPLEAD='Kepler/K2 GO office',
HLSPNAME='TUTORIAL',
CITATION='HEDGES2018',
DEMO_COLUMN=demo_vector)
hdu = fits.open('output.fits')
hdu[1].data.columns
ColDefs( name = 'TIME'; format = 'D'; unit = 'bkjd' name = 'FLUX'; format = 'E'; unit = 'counts' name = 'FLUX_ERR'; format = 'E'; unit = 'counts' name = 'CADENCENO'; format = 'J' name = 'DEMO_COLUMN'; format = 'D' name = 'SAP_QUALITY'; format = 'K' name = 'MOM_CENTR1'; format = 'D' name = 'MOM_CENTR2'; format = 'D' )
The vector is populated as a data column in the HDU extension. Once all your lightcurves are saved as fits files and you have a README file, you can consider submitting your data products to MAST.