-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from bmorris3/lightkurve-parser
First light curve parser
- Loading branch information
Showing
7 changed files
with
168 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,48 @@ | ||
import os | ||
from astropy.io import fits | ||
from jdaviz.core.registries import data_parser_registry | ||
from lightkurve import LightCurve, KeplerLightCurve, TessLightCurve | ||
from lightkurve.io.detect import detect_filetype | ||
|
||
__all__ = ["lcviz_manual_data_parser"] | ||
__all__ = ["light_curve_parser"] | ||
|
||
|
||
@data_parser_registry("lcviz_manual_data_parser") | ||
def lcviz_manual_data_parser(app, data, data_label=None, show_in_viewer=True): | ||
@data_parser_registry("light_curve_parser") | ||
def light_curve_parser(app, file_obj, data_label=None, show_in_viewer=True, **kwargs): | ||
time_viewer_reference_name = app._jdaviz_helper._default_time_viewer_reference_name | ||
|
||
data._preferred_translation = True # Triggers custom viewer.set_plot_axes() | ||
# load local FITS file from disk by its path: | ||
if isinstance(file_obj, str) and os.path.exists(file_obj): | ||
if data_label is None: | ||
data_label = os.path.splitext(os.path.basename(file_obj))[0] | ||
|
||
app.add_data(data, data_label) | ||
app.add_data_to_viewer(time_viewer_reference_name, data_label) | ||
# detect the type light curve in a FITS file: | ||
with fits.open(file_obj) as hdulist: | ||
filetype = detect_filetype(hdulist) | ||
|
||
# get the constructor for this type of light curve: | ||
filetype_to_cls = { | ||
'KeplerLightCurve': KeplerLightCurve, | ||
'TessLightCurve': TessLightCurve | ||
} | ||
cls = filetype_to_cls[filetype] | ||
# read the light curve: | ||
light_curve = cls.read(file_obj) | ||
|
||
# load a LightCurve object: | ||
elif isinstance(file_obj, LightCurve): | ||
light_curve = file_obj | ||
|
||
# make a data label: | ||
if data_label is not None: | ||
new_data_label = f'{data_label}' | ||
else: | ||
new_data_label = light_curve.meta.get('OBJECT', 'Light curve') | ||
flux_origin = light_curve.meta.get('FLUX_ORIGIN', None) # i.e. PDCSAP or SAP | ||
if flux_origin is not None: | ||
new_data_label += f'[{flux_origin}]' | ||
|
||
app.add_data(light_curve, new_data_label) | ||
|
||
if show_in_viewer: | ||
app.add_data_to_viewer(time_viewer_reference_name, new_data_label) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# import pytest | ||
import numpy as np | ||
from astropy.time import Time | ||
# from astropy.utils.data import download_file | ||
from lightkurve import LightCurve | ||
# from lightkurve.io import kepler | ||
from gwcs.wcs import WCS | ||
import astropy.units as u | ||
|
||
|
||
# @pytest.mark.remote_data | ||
# def test_kepler_via_mast_local_file(helper): | ||
# url = ( | ||
# 'https://archive.stsci.edu/pub/kepler/' | ||
# 'lightcurves/0014/001429092/kplr001429092-2009166043257_llc.fits' | ||
# ) # 188 KB | ||
# | ||
# path = download_file(url, cache=True, timeout=100) | ||
# helper.load_data(path) | ||
# | ||
# data = helper.app.data_collection[0] | ||
# flux_arr = data['flux'] | ||
# flux_unit = u.Unit(data.get_component('flux').units) | ||
# flux = flux_arr * flux_unit | ||
# | ||
# assert isinstance(data.coords, WCS) | ||
# assert isinstance(flux, u.Quantity) | ||
# assert flux.unit.is_equivalent(u.electron / u.s) | ||
# | ||
# | ||
# @pytest.mark.remote_data | ||
# def test_kepler_via_mast_preparsed(helper): | ||
# url = ( | ||
# 'https://archive.stsci.edu/pub/kepler/' | ||
# 'lightcurves/0014/001429092/kplr001429092-2009166043257_llc.fits' | ||
# ) # 188 KB | ||
# | ||
# light_curve = kepler.read_kepler_lightcurve(url) | ||
# helper.load_data(light_curve) | ||
# | ||
# data = helper.app.data_collection[0] | ||
# flux_arr = data['flux'] | ||
# flux_unit = u.Unit(data.get_component('flux').units) | ||
# flux = flux_arr * flux_unit | ||
# | ||
# assert isinstance(data.coords, WCS) | ||
# assert isinstance(flux, u.Quantity) | ||
# assert flux.unit.is_equivalent(u.electron / u.s) | ||
|
||
|
||
def test_synthetic_lc(helper): | ||
time = Time(np.linspace(2460050, 2460060), format='jd') | ||
flux = np.ones(len(time)) * u.electron / u.s | ||
flux_err = 0.1 * np.ones_like(flux) | ||
lc = LightCurve(time=time, flux=flux, flux_err=flux_err) | ||
helper.load_data(lc) | ||
|
||
data = helper.app.data_collection[0] | ||
flux_arr = data['flux'] | ||
flux_unit = u.Unit(data.get_component('flux').units) | ||
flux = flux_arr * flux_unit | ||
|
||
assert isinstance(data.coords, WCS) | ||
assert isinstance(flux, u.Quantity) | ||
assert flux.unit.is_equivalent(u.electron / u.s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters