def test_k2_attributes(): """ """ # test inherited attributes s = K2(epicid=EPICID, campaign=CAMPAIGN) assert s.epicid is not None assert s.target_coord is not None gaia_params = s.query_gaia_dr2_catalog(return_nearest_xmatch=True) assert isinstance(gaia_params, pd.Series) tic_params = s.query_tic_catalog(return_nearest_xmatch=True) assert isinstance(tic_params, pd.Series)
def test_k2_plots(): fig = K2.plot_everest_k2sff_comparison(EPICID) assert isinstance(fig, Figure)
def test_k2_methods(): lcs = K2.get_lightcurves(EPICID) # assert isinstance(lcs, lk.LightCurve) assert isinstance(lcs, _KeplerLightCurve)
# -*- coding: utf-8 -*- """ test methods of k2 module """ import pandas as pd import lightkurve as lk from chronos.k2 import K2, Everest, K2sff, _KeplerLightCurve from matplotlib.figure import Figure # from matplotlib.axes import Axes EPICID = 211916756 # k2-95 CAMPAIGN = 5 # or 18 s = K2(epicid=EPICID, campaign=CAMPAIGN) def test_k2_attributes(): """ """ # test inherited attributes assert s.epicid is not None assert s.target_coord is not None gaia_params = s.query_gaia_dr2_catalog(return_nearest_xmatch=True) assert isinstance(gaia_params, pd.Series) tic_params = s.query_tic_catalog(return_nearest_xmatch=True) assert isinstance(tic_params, pd.Series) def test_k2_lc_pipeline(): assert isinstance(s.lc_sap, lk.LightCurve)
def test_k2_tpf(): s = K2(epicid=EPICID, campaign=CAMPAIGN) tpf = s.get_tpf() assert isinstance(tpf, lk.targetpixelfile.TargetPixelFile)
def test_k2_lc_pipeline(): s = K2(epicid=EPICID, campaign=CAMPAIGN) s.get_lc("sap") assert isinstance(s.lc_sap, lk.LightCurve) s.get_lc("pdcsap") assert isinstance(s.lc_pdcsap, lk.LightCurve)
def save_ini_johannes( self, outdir=".", save_lc=False, cadence="short", # pipeline="everest", lctype="pdcsap", teff=None, logg=None, feh=None, rstar=None, period=None, epoch=None, duration=None, phase_trim=0.12, ): """target.ini file for johannes lcfit script Parameters ---------- outdir : str save_lc : bool (default=False) save raw lcs in each sector needed to run johannes' lcfit script and folded lcs in each sector and candidate to run vespa's calc_fpp cadence : str (default='short') lctype : str (default='pdcsap') teff : tuple logg : tuple feh : tuple rstar : tuple period : float orbital period [d] epoch : float [BJD] duration : float transit duration [d] Example file ------------ [planets] [[251319382.03]] per = 3.708550700 t0 = 2458102.387129 t14 = 0.06 rprs = 0.0140929 [[251319382.02]] [star] teff = 5875, 100 logg = 4.92, 0.08 feh = 0.0, 0.1 rad = 0.95, 0.05 """ # errmsg = "This method is available for TESS mission" # assert self.mission=='tess', errmsg if teff is not None: assert isinstance(teff, tuple) if logg is not None: assert isinstance(logg, tuple) if feh is not None: assert isinstance(feh, tuple) if rstar is not None: assert isinstance(rstar, tuple) target_name = self.target_name.replace(" ", "") outdir = target_name if outdir == "." else outdir if self.mission == "tess": Teff = self.toi_Teff if teff is None else teff[0] Teff_err = self.toi_Teff_err if teff is None else teff[1] log10g = self.toi_logg if logg is None else logg[0] log10g_err = self.toi_logg_err if logg is None else logg[1] metfeh = self.toi_feh if feh is None else feh[0] metfeh_err = self.toi_feh_err if feh is None else feh[1] Rstar = self.toi_Rstar if rstar is None else rstar[0] Rstar_err = self.toi_Rstar_err if rstar is None else rstar[1] # get TOI candidate ephemerides tois = get_tois(clobber=False) df = tois[tois["TIC ID"] == self.ticid] nplanets = len(df) errmsg = f"{target_name} has {nplanets}" # if period is not None: # assert len(period) == nplanets, errmsg # if epoch is not None: # assert len(epoch) == nplanets, errmsg # if duration is not None: # assert len(duration) == nplanets, errmsg lcs = [] if save_lc: if cadence == "short": kernel_size = 501 print(f"Querying {cadence} cadence PDCSAP light curve") sc = ShortCadence(ticid=self.ticid) for sector in tqdm(self.all_sectors): try: lc = sc.get_lc(lctype=lctype, sector=sector) # BTJD lc.time = lc.time + TESS_TIME_OFFSET lcname = ( f"{target_name}-{lctype}-s{sector}-raw.txt") lcpath = Path(outdir, lcname) # for johannes lc.to_csv( lcpath, columns=["time", "flux"], header=False, sep=" ", index=False, ) lcs.append(lc) print("Saved: ", lcpath) except Exception as e: print( f"Error: Cannot save {cadence} cadence light curve\n{e}" ) else: kernel_size = 49 errmsg = "Only cadence='short' is available for now." raise ValueError(errmsg) else: lcname = ( f"{target_name}-{lctype}-s{self.all_sectors[0]}-raw.txt") else: kernel_size = 49 if teff is None: errmsg = "Provide `teff`, `logg`, `feh`" raise ValueError(errmsg) Teff, Teff_err = teff log10g, log10g_err = logg metfeh, metfeh_err = feh Rstar, Rstar_err = rstar lcs = [] if save_lc: sc = K2(epicid=self.epicid) for camp in tqdm(self.all_campaigns): try: lc = sc.get_lc(lctype=lctype, campaign=camp) lcname = f"{target_name}-{lctype}-c{camp}-raw.txt" lcpath = Path(outdir, lcname) # for johannes lc.to_csv( lcpath, columns=["time", "flux"], header=False, sep=" ", index=False, ) lcs.append(lc) print("Saved: ", lcpath) except Exception as e: print( f"Error: Cannot save {cadence} cadence light curve\n{e}" ) else: lcname = ( f"{target_name}-{lctype}-s{self.all_campaigns[0]}-raw.txt") out = f"#command: lcfit -b {self.mission.upper()[0]} -i {lcname} -c {target_name}.ini " out += f"-o johannes --mcmc-steps 1000 --mcmc-burn 500 --mcmc-thin 10 -k {kernel_size}" self.lcfit_cmd = out out += "\n[planets]\n" if self.mission == "tess": for i, (k, row) in enumerate(df.iterrows()): d = row per = d["Period (days)"] if period is None else period[i] t0 = d["Epoch (BJD)"] if epoch is None else epoch[i] dur = (d["Duration (hours)"] / 24 if duration is None else duration[i]) if save_lc and self.mission == "tess": for lc in lcs: # save folded lc for vespa # TODO: merge all sectors in one folded lc flat = sc.get_flat_lc( lc, period=per, epoch=t0, duration=dur * 24, window_length=5 * dur, edge_cutoff=0.1, ) flat = flat.remove_nans() # flat_lcs.append(flat) lcname2 = f"{target_name}-0{i+1}-{lctype}-s{lc.sector}-flat.txt" lcpath2 = Path(outdir, lcname2) flat.to_csv( lcpath2, columns=["time", "flux"], header=False, sep=" ", index=False, ) print("Saved: ", lcpath2) fold = flat.fold(period=per, t0=t0).remove_nans() lcname3 = f"{target_name}-0{i+1}-{lctype}-s{lc.sector}-fold.txt" lcpath3 = Path(outdir, lcname3) # clip folded lc near phase=0 idx = (fold.phase > -phase_trim) & (fold.phase < phase_trim) fold[idx].to_csv( lcpath3, columns=["time", "flux"], header=False, sep=" ", index=False, ) print("Saved: ", lcpath3) print(f"==={d.TOI}===") out += f"\t[[{d.TOI}]]\n" out += f"\t\tper = {per:.6f}\n" out += f"\t\tt0 = {t0:.6f}\n" out += f"\t\tt14 = {dur:.2f}\n" else: raise NotImplementedError() out += "[star]\n" out += f"\tteff = {Teff:.0f}, {Teff_err:.0f}\n" out += f"\tlogg = {log10g:.2f}, {log10g_err:.2f}\n" out += f"\tfeh = {metfeh:.2f}, {metfeh_err:.2f}\n" out += f"\trad = {Rstar:.2f}, {Rstar_err:.2f}\n" outpath = Path(outdir, f"{target_name}.ini") if not Path(outdir).exists(): Path(outdir).mkdir() with open(outpath, "w") as file: file.write(out) print(f"Saved: {outpath}\n{out}")