<commit_before>..

Example 4: Removing Stellar Astrophysics and Quarter Stitching
==============================================================

The purpose of this tutorial is to flatten both systamtic artifacts and
astrophysical signal in order to fold candidate planet transits successfully
upon their orbital period and characterize them with a physical model.

For this purpose, we download quarter 10 and 11 of
`KIC 12557548 <http://archive.stsci.edu/kepler/preview.php?type=lc&dsn=KPLR012557548-2011271113734>`_:

.. code-block:: bash

    $ wget http://archive.stsci.edu/missions/kepler/lightcurves/0125/012557548/kplr012557548-2011271113734_llc.fits
    $ wget http://archive.stsci.edu/missions/kepler/lightcurves/0125/012557548/kplr012557548-2012004120508_llc.fits

We can use ``kepstitch`` to stitch those light curves together:

.. code-block:: bash

    $ kepstitch kplr012557548-2011271113734_llc.fits kplr012557548-2012004120508_llc.fits --outfile kplr012557548-kepstitch.fits

We can visualize the stitched light curve using kepdraw:

.. code-block:: bash

    $ kepdraw kplr012557548-kepstitch.fits
    $ kepdraw kplr012557548-kepstitch.fits --datacol PDCSAP_FLUX

.. image:: ../_static/images/tutorials/kplr012557548-kepstitch-kepdraw.png
.. image:: ../_static/images/tutorials/kplr012557548-kepstitch-kepdraw-pdcsap.png

With systematic artifacts already mostly-removed, the light curves need to be
flattened but in a way that does not radically contaminate the transit
profile with new systematic structure. The PyKE tool kepflatten fits piecemeal
low-order polynomials on small timeslices aross the time-series. Fitting makes
user of iterative σ-clipping that removes statistical outliers from a best-fit
before refitting until no more time-stamps are rejected. In this way, transits
profiles are conserved during the fit process and subsequent normalization by
the final best-fit. The flattened are written to the output file in two new
columns, DETSAP_FLUX and DETSAP_ERR_FLUX.


.. code-block:: bash

    $ kepflatten kplr012557548-kepstitch.fits --nsig 3 --stepsize 1 --npoly 2 --niter 10 --plot

.. image:: ../_static/images/tutorials/kplr012557548-kepstitch-kepdraw-kepflatten.png


Now, we can apply ``kepfold`` to fold our final light curve:

.. code-block:: bash

    $ kepfold kplr012557548-kepstitch-kepflatten.fits --nsig 3 --stepsize 1 --npoly 2 --niter 10 --plot

.. image:: ../_static/images/tutorials/kplr012557548-kepstitch-kepdraw-kepflatten-kepfold.png<commit_msg>fix kefold statement<commit_after>..

Example 4: Removing Stellar Astrophysics and Quarter Stitching
==============================================================

The purpose of this tutorial is to flatten both systamtic artifacts and
astrophysical signal in order to fold candidate planet transits successfully
upon their orbital period and characterize them with a physical model.

For this purpose, we download quarter 10 and 11 of
`KIC 12557548 <http://archive.stsci.edu/kepler/preview.php?type=lc&dsn=KPLR012557548-2011271113734>`_:

.. code-block:: bash

    $ wget http://archive.stsci.edu/missions/kepler/lightcurves/0125/012557548/kplr012557548-2011271113734_llc.fits
    $ wget http://archive.stsci.edu/missions/kepler/lightcurves/0125/012557548/kplr012557548-2012004120508_llc.fits

We can use ``kepstitch`` to stitch those light curves together:

.. code-block:: bash

    $ kepstitch kplr012557548-2011271113734_llc.fits kplr012557548-2012004120508_llc.fits --outfile kplr012557548-kepstitch.fits

We can visualize the stitched light curve using kepdraw:

.. code-block:: bash

    $ kepdraw kplr012557548-kepstitch.fits
    $ kepdraw kplr012557548-kepstitch.fits --datacol PDCSAP_FLUX

.. image:: ../_static/images/tutorials/kplr012557548-kepstitch-kepdraw.png
.. image:: ../_static/images/tutorials/kplr012557548-kepstitch-kepdraw-pdcsap.png

With systematic artifacts already mostly-removed, the light curves need to be
flattened but in a way that does not radically contaminate the transit
profile with new systematic structure. The PyKE tool kepflatten fits piecemeal
low-order polynomials on small timeslices aross the time-series. Fitting makes
user of iterative σ-clipping that removes statistical outliers from a best-fit
before refitting until no more time-stamps are rejected. In this way, transits
profiles are conserved during the fit process and subsequent normalization by
the final best-fit. The flattened are written to the output file in two new
columns, DETSAP_FLUX and DETSAP_ERR_FLUX.


.. code-block:: bash

    $ kepflatten kplr012557548-kepstitch.fits --nsig 3 --stepsize 1 --npoly 2 --niter 10 --plot

.. image:: ../_static/images/tutorials/kplr012557548-kepstitch-kepdraw-kepflatten.png


Now, we can apply ``kepfold`` to fold our final light curve:

.. code-block:: bash

    $ kepfold kplr012557548-kepstitch-kepflatten.fits 0.653534 2455372.883 --threshold 3. --niter 5 --bindata --binmethod median --nbins 100 --plottype det

.. image:: ../_static/images/tutorials/kplr012557548-kepstitch-kepdraw-kepflatten-kepfold.png
{'fraction_non_alphanumeric': 0.10672716512132571, 'fraction_numerical': 0.0980469520615506, 'mean_word_length': 4.547045951859956, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3227676', 'n_tokens_mistral': 2086, 'n_tokens_neox': 1652, 'n_words': 477}