In [1]:
import numpy as np
import matplotlib 
import matplotlib as mpl

import lightkurve 
from lightkurve import KeplerLightCurve, KeplerTargetPixelFile
from k2sc.standalone import k2sc_lc

%pylab inline --no-import-all
matplotlib.rcParams['image.origin'] = 'lower'
matplotlib.rcParams['figure.figsize']=(10.0,10.0)    #(6.0,4.0)
matplotlib.rcParams['font.size']=16              #10 
matplotlib.rcParams['savefig.dpi']= 300             #72 
colours = mpl.rcParams['axes.prop_cycle'].by_key()['color']


Populating the interactive namespace from numpy and matplotlib

In [16]:
lightkurve.__version__


Out[16]:
'1.0b13'

Here we debut a lightkurve-based wrapper for k2sc so you can more easily do k2sc operations interactively.


In [2]:
tpf = KeplerTargetPixelFile.from_archive(212300977) # WASP-55

In [3]:
tpf.plot(frame=1,aperture_mask=tpf.pipeline_mask)


Out[3]:
<matplotlib.axes._subplots.AxesSubplot at 0x106f2f8d0>

In [19]:
lc = tpf.to_lightcurve()
lc.primary_header = tpf.hdu[0].header
lc.data_header = tpf.hdu[1].header
lc.pos_corr1 = tpf.hdu[1].data['POS_CORR1'][tpf.quality_mask]
lc.pos_corr2 = tpf.hdu[1].data['POS_CORR2'][tpf.quality_mask]

In [5]:
plt.plot(lc.time, lc.flux,'.')


Out[5]:
[<matplotlib.lines.Line2D at 0x1316e65250>]

In [9]:
lc.to_fits('test.fits')


Out[9]:
[<astropy.io.fits.hdu.image.PrimaryHDU object at 0x1138c6590>, <astropy.io.fits.hdu.table.BinTableHDU object at 0x106b131d0>]

In [10]:
import fitsio

In [11]:
f = fitsio.FITS('test.fits')

In [14]:
hdr = fitsio.read_header('test.fits')

In [15]:
hdr


Out[15]:
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    = '2018-08-23'         / file creation date.
CREATOR = 'lightkurve'         / pipeline job and program used t
TELESCOP= 'KEPLER  '           / telescope
INSTRUME= 'Kepler Photometer'  / detector type
OBJECT  = '212300977'          / string version of target id
KEPLERID=            212300977 / unique Kepler target identifier
CHANNEL =                   36 / CCD channel
RADESYS = 'ICRS    '           / reference frame of celestial coordinates
RA_OBJ  =            203.75811 / [deg] right ascension
DEC_OBJ =            -17.50355 / [deg] declination
EQUINOX =                 2000 / equinox of celestial coordinate system
PROCVER = '1.0b13  '
CAMPAIGN=                    6
MISSION = 'K2      '
DATE-OBS= '2015-07-13T23:07:23.227'
CHECKSUM= 'PA66QA64PA64PA64'   / HDU checksum updated 2018-08-23T10:18:45
DATASUM = '0       '           / data unit checksum updated 2018-08-23T10:18:45

Let's now try K2SC


In [21]:
# now the magic happens
lc.__class__ = k2sc_lc

In [22]:
lc.k2sc()


Using default splits [2344] for campaign 5
Starting initial outlier detection
  Flagged 147 ( 0.0%) outliers.
Starting Lomb-Scargle period search
  Using SqrExp position kernel
  Found periodicity p =   18.94 (fap 3.4161e-303 < 1e-50), will use a quasiperiodic kernel
Starting global hyperparameter optimisation using DE
('  DE iteration %3i -ln(L) %4.1f', 0, -4425.828285093708)
('  DE iteration %3i -ln(L) %4.1f', 1, -4425.828285093708)
('  DE iteration %3i -ln(L) %4.1f', 2, -4425.828285093708)
('  DE iteration %3i -ln(L) %4.1f', 3, -4440.557180362137)
('  DE iteration %3i -ln(L) %4.1f', 4, -4440.557180362137)
('  DE iteration %3i -ln(L) %4.1f', 5, -4440.557180362137)
('  DE iteration %3i -ln(L) %4.1f', 6, -4440.557180362137)
('  DE iteration %3i -ln(L) %4.1f', 7, -4443.7621256508055)
('  DE iteration %3i -ln(L) %4.1f', 8, -4443.7621256508055)
('  DE iteration %3i -ln(L) %4.1f', 9, -4445.160390554532)
('  DE iteration %3i -ln(L) %4.1f', 10, -4445.160390554532)
('  DE iteration %3i -ln(L) %4.1f', 11, -4467.81536158633)
('  DE iteration %3i -ln(L) %4.1f', 12, -4467.81536158633)
('  DE iteration %3i -ln(L) %4.1f', 13, -4467.81536158633)
('  DE iteration %3i -ln(L) %4.1f', 14, -4467.81536158633)
('  DE iteration %3i -ln(L) %4.1f', 15, -4467.81536158633)
('  DE iteration %3i -ln(L) %4.1f', 16, -4467.81536158633)
('  DE iteration %3i -ln(L) %4.1f', 17, -4467.81536158633)
('  DE iteration %3i -ln(L) %4.1f', 18, -4468.128113210132)
('  DE iteration %3i -ln(L) %4.1f', 19, -4473.981734372432)
('  DE iteration %3i -ln(L) %4.1f', 20, -4477.291193168639)
('  DE iteration %3i -ln(L) %4.1f', 21, -4481.751086451511)
('  DE iteration %3i -ln(L) %4.1f', 22, -4481.751086451511)
('  DE iteration %3i -ln(L) %4.1f', 23, -4481.751086451511)
('  DE iteration %3i -ln(L) %4.1f', 24, -4481.751086451511)
('  DE iteration %3i -ln(L) %4.1f', 25, -4481.751086451511)
('  DE iteration %3i -ln(L) %4.1f', 26, -4481.751086451511)
('  DE iteration %3i -ln(L) %4.1f', 27, -4481.751086451511)
('  DE iteration %3i -ln(L) %4.1f', 28, -4481.751086451511)
('  DE iteration %3i -ln(L) %4.1f', 29, -4483.155301008228)
('  DE iteration %3i -ln(L) %4.1f', 30, -4484.124362355757)
('  DE iteration %3i -ln(L) %4.1f', 31, -4485.155819021626)
('  DE iteration %3i -ln(L) %4.1f', 32, -4486.868208936356)
('  DE iteration %3i -ln(L) %4.1f', 33, -4486.868208936356)
('  DE iteration %3i -ln(L) %4.1f', 34, -4486.868208936356)
('  DE iteration %3i -ln(L) %4.1f', 35, -4486.868208936356)
('  DE iteration %3i -ln(L) %4.1f', 36, -4488.838389420864)
('  DE iteration %3i -ln(L) %4.1f', 37, -4490.897621263316)
('  DE iteration %3i -ln(L) %4.1f', 38, -4490.897621263316)
('  DE iteration %3i -ln(L) %4.1f', 39, -4490.897621263316)
('  DE iteration %3i -ln(L) %4.1f', 40, -4490.897621263316)
('  DE iteration %3i -ln(L) %4.1f', 41, -4491.060042734555)
('  DE iteration %3i -ln(L) %4.1f', 42, -4492.122356241238)
('  DE iteration %3i -ln(L) %4.1f', 43, -4492.122356241238)
('  DE iteration %3i -ln(L) %4.1f', 44, -4494.763819666909)
('  DE iteration %3i -ln(L) %4.1f', 45, -4494.763819666909)
('  DE iteration %3i -ln(L) %4.1f', 46, -4494.763819666909)
('  DE iteration %3i -ln(L) %4.1f', 47, -4494.763819666909)
('  DE iteration %3i -ln(L) %4.1f', 48, -4494.840160388741)
('  DE iteration %3i -ln(L) %4.1f', 49, -4494.840160388741)
('  DE iteration %3i -ln(L) %4.1f', 50, -4495.4335916340915)
('  DE iteration %3i -ln(L) %4.1f', 51, -4495.4335916340915)
('  DE iteration %3i -ln(L) %4.1f', 52, -4498.405738525526)
('  DE iteration %3i -ln(L) %4.1f', 53, -4498.405738525526)
('  DE iteration %3i -ln(L) %4.1f', 54, -4498.405738525526)
('  DE iteration %3i -ln(L) %4.1f', 55, -4498.405738525526)
('  DE iteration %3i -ln(L) %4.1f', 56, -4498.405738525526)
('  DE iteration %3i -ln(L) %4.1f', 57, -4499.044428320828)
('  DE iteration %3i -ln(L) %4.1f', 58, -4499.2834804641725)
('  DE iteration %3i -ln(L) %4.1f', 59, -4499.570015206928)
('  DE iteration %3i -ln(L) %4.1f', 60, -4499.570015206928)
('  DE iteration %3i -ln(L) %4.1f', 61, -4499.570015206928)
('  DE iteration %3i -ln(L) %4.1f', 62, -4501.185391170571)
('  DE iteration %3i -ln(L) %4.1f', 63, -4501.185391170571)
('  DE iteration %3i -ln(L) %4.1f', 64, -4501.185391170571)
('  DE iteration %3i -ln(L) %4.1f', 65, -4501.185391170571)
('  DE iteration %3i -ln(L) %4.1f', 66, -4501.185391170571)
('  DE iteration %3i -ln(L) %4.1f', 67, -4501.185391170571)
('  DE iteration %3i -ln(L) %4.1f', 68, -4501.185391170571)
('  DE iteration %3i -ln(L) %4.1f', 69, -4501.185391170571)
('  DE iteration %3i -ln(L) %4.1f', 70, -4501.205255478276)
('  DE iteration %3i -ln(L) %4.1f', 71, -4501.336648368822)
('  DE iteration %3i -ln(L) %4.1f', 72, -4501.510308258221)
('  DE iteration %3i -ln(L) %4.1f', 73, -4501.510308258221)
('  DE iteration %3i -ln(L) %4.1f', 74, -4501.510308258221)
('  DE iteration %3i -ln(L) %4.1f', 75, -4501.735648362131)
('  DE iteration %3i -ln(L) %4.1f', 76, -4502.918658856899)
('  DE iteration %3i -ln(L) %4.1f', 77, -4502.918658856899)
('  DE iteration %3i -ln(L) %4.1f', 78, -4502.918658856899)
('  DE iteration %3i -ln(L) %4.1f', 79, -4502.918658856899)
('  DE iteration %3i -ln(L) %4.1f', 80, -4504.47530752586)
('  DE iteration %3i -ln(L) %4.1f', 81, -4504.47530752586)
('  DE iteration %3i -ln(L) %4.1f', 82, -4504.47530752586)
('  DE iteration %3i -ln(L) %4.1f', 83, -4504.47530752586)
('  DE iteration %3i -ln(L) %4.1f', 84, -4504.47530752586)
('  DE iteration %3i -ln(L) %4.1f', 85, -4504.47530752586)
('  DE iteration %3i -ln(L) %4.1f', 86, -4504.47530752586)
('  DE iteration %3i -ln(L) %4.1f', 87, -4504.47530752586)
('  DE iteration %3i -ln(L) %4.1f', 88, -4504.47530752586)
('  DE iteration %3i -ln(L) %4.1f', 89, -4505.200494144365)
('  DE iteration %3i -ln(L) %4.1f', 90, -4505.200494144365)
('  DE iteration %3i -ln(L) %4.1f', 91, -4505.200494144365)
('  DE iteration %3i -ln(L) %4.1f', 92, -4505.200494144365)
('  DE iteration %3i -ln(L) %4.1f', 93, -4507.917969620311)
('  DE iteration %3i -ln(L) %4.1f', 94, -4508.122441639004)
('  DE iteration %3i -ln(L) %4.1f', 95, -4508.122441639004)
('  DE iteration %3i -ln(L) %4.1f', 96, -4508.122441639004)
('  DE iteration %3i -ln(L) %4.1f', 97, -4508.122441639004)
('  DE iteration %3i -ln(L) %4.1f', 98, -4508.122441639004)
('  DE iteration %3i -ln(L) %4.1f', 99, -4508.122441639004)
('  DE iteration %3i -ln(L) %4.1f', 100, -4508.122441639004)
('  DE iteration %3i -ln(L) %4.1f', 101, -4508.122441639004)
('  DE iteration %3i -ln(L) %4.1f', 102, -4508.122441639004)
('  DE iteration %3i -ln(L) %4.1f', 103, -4508.122441639004)
('  DE finished in %i seconds', 301.5297107696533)
('  DE minimum found at: %s', '[-4.389e+00  9.985e-01  4.726e+00  3.476e-03 -2.350e+00  3.046e+00  1.918e+01 -3.098e+00]')
('  DE -ln(L) %4.1f', -4508.122441639004)
Starting local hyperparameter optimisation
('  Local minimum found at: %s', '[-4.408e+00  9.994e-01  4.701e+00  4.698e-03 -2.293e+00  2.887e+00\n  2.007e+01 -3.105e+00]')
Starting final outlier detection
('  %5i too high', 43)
('  %5i too low', 121)
('  %5i not finite', 0)
Computing time and position trends
('  CDPP - raw - %6.3f', 14538.952100223865)
('  CDPP - position component removed - %6.3f', 367.8693336450076)
('  CDPP - full reduction - %6.3f', 368.31939197172034)
('Detrending time %6.3f', 312.2114460468292)

In [23]:
plt.plot(lc.time,lc.flux,'.',label="Uncorrected")
plt.plot(lc.time,lc.corr_flux-lc.tr_time + np.nanmedian(lc.tr_time),'.',label="K2SC")
plt.legend()
plt.xlabel('BJD')
plt.ylabel('Flux')
plt.title('WASP-55',y=1.01)


Out[23]:
Text(0.5,1.01,'WASP-55')

In [24]:
plt.plot(lc.time,lc.corr_flux-lc.tr_time + np.nanmedian(lc.tr_time),'.',label="K2SC")
plt.legend()
plt.xlabel('BJD')
plt.ylabel('Flux')
plt.title('WASP-55',y=1.01)
plt.ylim(215000,235000)


Out[24]:
(215000, 235000)

Now we save this


In [60]:
extras = {'CORR_FLUX':lc.corr_flux,
          'TR_TIME':lc.tr_time,
          'TR_POSITION':lc.tr_position}
lc.to_fits(extra_data=extras,path='test.fits',overwrite=True)


Out[60]:
[<astropy.io.fits.hdu.image.PrimaryHDU object at 0x107009290>, <astropy.io.fits.hdu.table.BinTableHDU object at 0x11372c490>]

In [61]:
f = fitsio.FITS('test.fits')
f[1]


Out[61]:
  file: test.fits
  extension: 1
  type: BINARY_TBL
  extname: LIGHTCURVE
  rows: 3561
  column info:
    TIME                f8  
    FLUX                f4  
    FLUX_ERR            f4  
    CADENCENO           i4  

In [62]:
hdr = fitsio.read_header('test.fits')

In [66]:



---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-66-f75758e21c57> in <module>()
----> 1 f[2]

/anaconda2/lib/python2.7/site-packages/fitsio/fitslib.pyc in __getitem__(self, item)
   1067             else:
   1068                 if ext not in self.hdu_map:
-> 1069                     raise IOError("extension not found: %s %s" % (ext,mess))
   1070                 hdu = self.hdu_map[ext]
   1071 

IOError: extension not found: 2 (case insensitive)

In [ ]: