import matplotlib.pyplot as plt from matplotlib import gridspec import re import lightkurve as lk import exoarch import starspot as ss import starspot.rotation_tools as rt from stardate.lhf import gk_age_model from stardate import load_samples, read_samples from isochrones import get_ichrone import kepler_data as kd import kplr client = kplr.API() def get_index_from_filename(df, filename): kepid = re.findall(r'\d+', filename)[0] m = df.kepid == int(kepid) return int(np.arange(len(df))[m]), kepid def load_lc(starname, timespan=200): # # lcfs = lk.search_lightcurvefile(starname).download_all() # lc1 = lk.search_lightcurvefile(starname, quarter=2) # lc2 = lk.search_lightcurvefile(starname, quarter=3) # # lc = lcf.PDCSAP_FLUX # # lc = lcfs.PDCSAP_FLUX.stitch() # lc_collection = lk.LightCurveCollection([lc1, lc2])
def export_planets_to_pkl(): client = kplr.API() planets = client.planets(koi_prad=">1.5") + client.planets(koi_prad="<=1.5") with open('planets.pkl', 'rb') as input_f: pickled_planets = cPickle.load(input_f) print "Saved planet info to planets.pkl"
def EstablishCompatableKOI(): '''** No arguments, please. The function will issue a prompt for user's choice of KOI.''' # declare that the koi established in this function be used as a global variable global koi global usercall invalidAttempts = 0 totalPlanetsInSystem = 0 selectableList= [] KOIlist = [] usercall = int(input("Enter the KOI system to lookup in MAST: ")) client = kplr.API() #Determine how many KOI are in the system associated with the user's choice print" KOI# KOI Name Period Usable?" print"_"*55 for PlanetInSystem in range(1,15,1): try: KOI = client.koi(float(usercall+.01*PlanetInSystem)) totalPlanetsInSystem+=1 KOIlist = np.append(KOIlist, KOI) if KOI.koi_period < 15.4: selectableList = np.append(selectableList, PlanetInSystem) print "(",PlanetInSystem,") ", KOI, round(KOI.koi_period,2), " usable KOI" else: invalidAttempts+=1 print "(",PlanetInSystem,") ", KOI, round(KOI.koi_period,2), " non-usable KOI" except ValueError: break print totalPlanetsInSystem, " total KOI in system" print totalPlanetsInSystem-invalidAttempts, " compatable KOI" #while the system has no compatable KOI for this program, require another system from the user. while totalPlanetsInSystem == invalidAttempts: usercall = int(input("Sorry, non are compatable, enter another system: ")) # reset the total count and invalid count totalPlanetsInSystem = 0 invalidAttempts = 0 print" KOI# KOI Name Period Usable?" print"_"*55 for PlanetInSystem in range(1,15,1): try: KOI = client.koi(float(usercall+.01*PlanetInSystem)) totalPlanetsInSystem+=1 KOIlist = np.append(KOIlist, KOI) if KOI.koi_period < 15.4: selectableList = np.append(selectableList, PlanetInSystem) print "(",PlanetInSystem,") ", KOI, round(KOI.koi_period,2), " usable KOI" else: invalidAttempts+=1 print "(",PlanetInSystem,") ", KOI, round(KOI.koi_period,2), " non-usable KOI" except ValueError: break print totalPlanetsInSystem, " total KOI in system" print totalPlanetsInSystem-invalidAttempts, " compatable KOI" if len(selectableList) > 1: KOIChoice = int(input("Choose from "+str(selectableList)+" : ")) print "that is ", KOIlist[KOIChoice-1] else: KOIChoice = 1 # now we have #print" PROPERTIES:" koi = KOIlist[KOIChoice-1]
def simulate_one(smass, srad, duration, catalog, data_path, injection): import warnings warnings.filterwarnings("error") pop = ParaSampler(smass, srad, duration) warnings.resetwarnings() B = LcNoiseSampler(catalog, data_path, pop.rprs_pop) client = kplr.API('/media/yinanzhao/Backup_7/kepdata') #star = client.star(kepid) lcs = client.light_curves(B.kepid, short_cadence=False) #filenames.sort() #datepat = re.compile(r'Q+\d+') #index = 0 #for i in range(len(filenames)): # index = datepat.findall(filenames[i])[0][1:] # rms = self.catalog.loc[lambda df: df.kepid == self.kepid, 'Q'+index+'rms'].values[0] # #read into cadence # instr = fits.open(filenames[i]) # #read into time series # intime = kepreduce.fetchtseries(instr, filenames[i]) # #simulate # rs = np.random.RandomState(seed=13) # flux1 = np.zeros(intime.size)+ 1.0 # errors1 = rms*np.ones_like(intime)#use global std # #errors1 = cdpp*1e-06*np.ones_like(nordata)*math.sqrt(timescale * 3600.0 / cadence)#correct from cdpp to std # #add gaussian noise # flux1 += errors1*rs.randn(len(intime)) # all_time.append(intime) # all_flux.append(flux1) # instr.close() with Pool(5) as p: task = [lc for lc in lcs] #print(task) #prod_lc=partial(generateone, kepid=self.kepid, catalog=self.catalog) cube = np.transpose(p.map(B.generateone, task, 4)) t = cube[0] simflux = cube[1] #print(np.shape(cube)) t = np.concatenate(t) bareflux = np.concatenate(simflux) t0 = np.random.choice(t) if injection == 1.0: transitflux = batman_planet(t0, pop.P_pop, pop.rprs_pop,pop.ars_pop,\ pop.inc_pop, 0, 90, t) #t0, per, rp, a, inc, ecc, w, t final_flux = bareflux * transitflux time_array = t flux = final_flux phase = keputils.phase_fold_time(time_array, pop.P_pop, t0) sorted_i = np.argsort(phase) phase = phase[sorted_i] flux = flux[sorted_i] transitflux = transitflux[sorted_i] TP = TransitProfile(pop.P_pop, t0, phase, flux, transitflux, pop.duration_pop, 1 - pop.rprs_pop**2) cksnr = TP.check_snr()[0] if cksnr == 1.0: profile = TP.transit_profile para = {'t0':t0, 'Period':pop.P_pop, 'rprs':pop.rprs_pop, 'ars':pop.ars_pop,\ 'inclination':pop.inc_pop, 'duration':pop.duration_pop, 'prekid':B.kepid,\ 'std_nobining': TP.check_snr()[1], 'std_bining': TP.check_snr()[2],'flag': 1.0} lc = np.array([profile['phase'], profile['flux']]) #plt.scatter(phase, flux, color='black', marker='.', alpha=0.2, label=str(1.0)) #plt.scatter(phase, np.ones(len(phase))*(1-pop.rprs_pop**2.0), marker='.', color='g', alpha=0.2, label='depth') #plt.show() #print(para) return para, lc, cksnr else: return None, None, cksnr else: final_flux = bareflux time_array = t flux = final_flux phase = keputils.phase_fold_time(time_array, pop.P_pop, t0) sorted_i = np.argsort(phase) phase = phase[sorted_i] flux = flux[sorted_i] TP = TransitProfile(pop.P_pop, t0, phase, flux, pop.duration_pop, 1 - pop.rprs_pop**2.0) profile = TP.transit_profile para = {'t0':t0, 'Period':pop.P_pop, 'rprs':pop.rprs_pop, 'ars':pop.ars_pop,\ 'inclination':pop.inc_pop, 'duration':pop.duration_pop, 'prekid':B.kepid,\ 'std': np.std(flux), 'flag':0.0} lc = np.array([profile['phase'], profile['flux']]) return para, lc, injection
def make_postcard(self, npix=300, shape=(1070, 1132), buffer_size=15): """ Develop a "postcard" region around the target star. Other stars in this postcard will be used as possible reference stars. Args: npix: The size of the postcard region. The region will be a square with sides npix pixels (default: ``300``) shape: The size of each individual image. For Kepler/K2 FFIs this should never need to be changed from the default, but will be different for e.g. TESS FFIs (default: ``(1070, 1132)``) buffer_size: The number of pixels at the edge of the detector to avoid (default: ``15``) """ source = self.kic client = kplr.API() targ = client.target(source) channel = [ targ.params['Channel_0'], targ.params['Channel_1'], targ.params['Channel_2'], targ.params['Channel_3'] ] col = [ targ.params['Column_0'], targ.params['Column_1'], targ.params['Column_2'], targ.params['Column_3'] ] row = [ targ.params['Row_0'], targ.params['Row_1'], targ.params['Row_2'], targ.params['Row_3'] ] if None in row: raise ValueError('Star not on detector all quarters!') if None in col: raise ValueError('Star not on detector all quarters!') center = np.array([npix / 2, npix / 2]) # If star close to edge, shift frame so that we have the full npix by npix # In this case, postcard will not be centered on target star if (np.min(col) < npix / 2): jump = npix / 2 - np.min(col) + buffer_size col += jump center[1] -= jump if (np.min(row) < npix / 2): jump = npix / 2 - np.min(row) + buffer_size row += jump center[0] -= jump if (np.max(row) > shape[0] - npix / 2): jump = shape[0] - npix / 2 - np.max(row) - buffer_size row += jump center[0] -= jump if (np.max(col) > shape[1] - npix / 2): jump = shape[1] - npix / 2 - np.max(col) - buffer_size col += jump center[1] -= jump fin_arr = np.zeros((len(self.times), npix, npix)) for icount, iname in enumerate(self.obs_filenames): a = p.open(self.ffi_dir + iname) quarter = a[0].header['quarter'] if int(quarter) == 0: season = 3 else: season = (int(quarter) - 2) % 4 #season_arr[icount] = season img = a[channel[season]].data img -= np.median(img) ymin = int(max([int(row[season]) - npix / 2, 0])) ymax = int(min([int(row[season]) + npix / 2, img.shape[0]])) xmin = int(max([int(col[season]) - npix / 2, 0])) xmax = int(min([int(col[season]) + npix / 2, img.shape[1]])) pimg = img[ymin:ymax, xmin:xmax] fin_arr[icount, :, :] = pimg self.postcard = fin_arr self.integrated_postcard = np.sum(self.postcard, axis=0) self.center = center
def processPlanetData(index, planets, df, baseline_sap_flux, baseline_sap_bkg, baseline_pd_sap_flux, baseline_det_sap_flux): # Get Kepler Object of Interest name and disposition kepoi_name = planets.iloc[index]['kepoi_name'] koi_disposition = planets.iloc[index]['koi_disposition'] # Set Files Names for the resultant Stiched File and Resultant Detrended File filenameStitch = "data/stitched/{}.fits".format(kepoi_name) filenameDetrend = "data/detrended/{}.fits".format(kepoi_name) # Initialize kplr API client = kplr.API() print "Processing Kepler Object: {} at index: {}".format(kepoi_name, index) # Find a Kepler Object of Interest koi = client.koi(planets.iloc[index]['kepoi_name']) # Get a list of light curve data sets. lcs = koi.get_light_curves(short_cadence=False, fetch=True, clobber=False) print "Got Kepler Object of Interest and Light Curve Files for {}".format( kepoi_name) lc_list = "" # Looping trough lcs to get list of light curves path for lc in lcs: lc_list += str(lc.filename) + "," # Removing trailing comma lc_list_clean = lc_list[:-1] print "Got path list of light curves." # Stitching together light curves quarters kepstitch(lc_list_clean, filenameStitch, True, False, LOG_FILENAME, 0) print "Finished Stiching the data for {}".format(kepoi_name) # Detrending Light Curve kepflatten(filenameStitch, filenameDetrend, "PDCSAP_FLUX", "PDCSAP_FLUX_ERR", 3.0, 1.0, 3.0, 3, 10, "0,0", False, True, False, LOG_FILENAME, 0, True) print "Finished Detrending the data for {}".format(kepoi_name) # Opening Detrended FITs File hdu = pyfits.open(filenameDetrend) # Getting Detrended Data hdu_data = hdu[1].data # Getting all features form the Fit Files sap_flux = pd.Series(np.array(hdu_data["SAP_FLUX"])) sap_bkg = pd.Series(np.array(hdu_data["SAP_BKG"])) pd_sap_flux = pd.Series(np.array(hdu_data["PDCSAP_FLUX"])) det_sap_flux = pd.Series(np.array(hdu_data["DETSAP_FLUX"])) mom_centr1 = pd.Series(np.array(hdu_data["MOM_CENTR1"])) mom_centr2 = pd.Series(np.array(hdu_data["MOM_CENTR2"])) pos_corr1 = pd.Series(np.array(hdu_data["POS_CORR1"])) pos_corr2 = pd.Series(np.array(hdu_data["POS_CORR2"])) # Try to apply DTW with the baseline. If fails return NaN try: dtw_sap_flux = LB_Keogh(sap_flux, baseline_sap_flux, 10) dtw_sap_bkg = LB_Keogh(sap_bkg, baseline_sap_bkg, 10) dtw_pd_sap_flux = LB_Keogh(pd_sap_flux, baseline_pd_sap_flux, 10) dtw_det_sap_flux = LB_Keogh(det_sap_flux, baseline_det_sap_flux, 10) except: dtw_sap_flux, dtw_sap_bkg, dtw_pd_sap_flux, dtw_det_sap_flux = 'nan', 'nan', 'nan', 'nan' pass print "dtw_sap_flux: {}, dtw_sap_bkg: {}, dtw_pd_sap_flux: {}, dtw_det_sap_flux: {}".format( dtw_sap_flux, dtw_sap_bkg, dtw_pd_sap_flux, dtw_det_sap_flux) # Describe the Features Extracted (STD, MEAN, MIN, MAX) desc_sap_flux, desc_sap_bkg = sap_flux.describe(), sap_bkg.describe() desc_pd_sap_flux, desc_det_sap_flux = pd_sap_flux.describe( ), det_sap_flux.describe() desc_mom_centr1, desc_mom_centr2 = mom_centr1.describe( ), mom_centr2.describe() desc_pos_corr1, desc_pos_corr2 = pos_corr1.describe(), pos_corr2.describe() print "Features Described Correctly." # Apply the Features to the dataframe df.loc[index] = [ kepoi_name, koi_disposition, dtw_sap_flux, desc_sap_flux['mean'], desc_sap_flux['std'], desc_sap_flux['min'], desc_sap_flux['max'], dtw_sap_bkg, desc_sap_bkg['mean'], desc_sap_bkg['std'], desc_sap_bkg['min'], desc_sap_bkg['max'], dtw_pd_sap_flux, desc_pd_sap_flux['mean'], desc_pd_sap_flux['std'], desc_pd_sap_flux['min'], desc_pd_sap_flux['max'], dtw_det_sap_flux, desc_det_sap_flux['mean'], desc_det_sap_flux['std'], desc_det_sap_flux['min'], desc_det_sap_flux['max'], desc_mom_centr1['mean'], desc_mom_centr1['std'], desc_mom_centr1['min'], desc_mom_centr1['max'], desc_mom_centr2['mean'], desc_mom_centr2['std'], desc_mom_centr2['min'], desc_mom_centr2['max'], desc_pos_corr1['mean'], desc_pos_corr1['std'], desc_pos_corr1['min'], desc_pos_corr1['max'], desc_pos_corr2['mean'], desc_pos_corr2['std'], desc_pos_corr2['min'], desc_pos_corr2['max'] ] print "Dataframe Row Added Correctly."
def load_kepler_lc(target, cadence='l', use_pdc=False, phase_offset=0, imin=0, imax=None, **kwargs): """ Import a Kepler light curve. Parameters ---------- koi koi number planet planet name (if koi not given) cadence cadence to load use_pdc load pdcsap flux instead of sap phase_offset imin imax bl_factor baseline duration to include in transit durations tr_factor in-transit duration to exclude for oot level estimation bl_min minimum baseline duration """ client = kplr.API() if isinstance(target, kplr.api.KOI): koi = target elif isinstance(target, str) and 'kepler-' in target.lower(): koi = client.planet(planet).koi else: koi = client.koi(koi) bl_min = kwargs.get('bl_min', 0.5) bl_factor = kwargs.get('bl_factor', 3.0) td_factor = kwargs.get('td_factor', 1.2) files = koi.get_light_curves( short_cadence=False if cadence == 'l' else True) cadence_string = 'llc' if cadence == 'l' else 'slc' t, f, q = [], [], [] for ff in files: if cadence_string in ff.filename: with ff.open() as hdul: fl = hdul[1] t.append(fl.data.field( 'time')) # + fl.header['bjdrefi'] + fl.header['bjdreff']) f.append(fl.data.field('pdcsap_flux')) q.append(hdul[0].header['quarter'] * np.ones(t[-1].size, dtype=np.int)) t, f, q = map(np.concatenate, (t, f, q)) tcenter = koi.koi_time0bk period = koi.koi_period trdur = koi.koi_duration / 24. return KeplerLC(t, f, tcenter + phase_offset * period, period, max(td_factor * trdur, 0.18), max(bl_factor * trdur, 0.5), q, **kwargs)
import pandas as pd import kplr import kepio import time import urllib cata_path = '../catalog/keplerstellar1' catalog = pd.read_csv(cata_path) allid = kepio.get_id(catalog) i = 0 s = time.time() while i < len(allid): try: client = kplr.API('/media/yinanzhao/Backup_7/kepdata') lcs = client.light_curves(allid[i], fetch=True) i = i + 1 except (TimeoutError, urllib.error.URLError) as e: print(e, allid[i]) if str(e) == 'HTTP Error 404: Not Found': i = i + 1 else: pass e = time.time() print(e - s)
there will then be N data points of flux for that time in the orbit. The average of these can be taken as the best estimate of the true flux value at that point in time, from which a model of the true flux vs time can be descirbed and plotted as an overlaid red line. The base intensity of the host star is taken as the best estimate of flux values that are not associated with the transit event. Because we will be mainly looking at KOI with periods greater than 300 days(**needs justification - larger objects farther away??), we can determine the average base intensity of the star by taking the average of flux that lies between For each time bin, the best estimate (model value) of flux can be subtracted from the base intensity, from which the maxmium value outside''' # Establish the search criteria for kepler objects of interest. # here, we search for koi that have period greater than 500 days, producing # a list 'kois' from which we can work with. kois = kplr.API().kois(where="koi_period<800", sort=("koi_period", 1)) print "Found {} KOI list with specified criteria.".format(len(kois)) # here, we further filter the results to those who are not false positives, # leaving only the candidates and the confirmed planets. good_kois = [] for i in kois: if i.koi_disposition != 'CONFIRMED': kois.remove(i) else: good_kois = np.append(good_kois, i) print i.kepoi_name, i.koi_disposition, "Period:", i.koi_period count = len(good_kois) print count, " Confirmed Planets."
def update_kepler(wtoken, rtoken, endpoint, limit): client = kplr.API() click.echo('Fetching all KOI where koi_period>{}'.format(limit)) # Getting all Kepler object of Interests, not K2 kois = client.kois(where="koi_period>{}".format(limit), sort="kepid") click.echo('Found {} KOIS'.format(len(kois))) koisdict = {} for koi in kois: disposition = koi.koi_disposition score = koi.koi_score kepid = koi.kepid kepoi_name = koi.kepoi_name kepler_name = koi.kepler_name if score is None: score = -1 if kepler_name is None: kepler_name = '' # Example: # kepoi_name:K00992.01, kepler_name:Kepler-745 b, disposition:CONFIRMED, score:0 click.echo('fetched info for {}: kepoi_name:{}, kepler_name:{}, disposition:{}, score:{}' .format(kepid, kepoi_name, kepler_name, disposition, score)) lcs = koi.get_light_curves(short_cadence=False) files = [] for lcsfile in lcs: _, filename = os.path.split(lcsfile.filename) filename = filename.split("-")[0] filename = filename[4:] files.append(filename) files = set(files) if kepid in koisdict: koisdict[kepid]['attributes']['{}'.format(kepoi_name)] = disposition if disposition == 'CONFIRMED': koisdict[kepid]['attributes']['status'] = "CONFIRMED" else: koisdict[kepid] = { 'filenames': files, 'kepid': kepid, 'attributes': { '{}'.format(kepoi_name): disposition, }, } if disposition == 'CONFIRMED': koisdict[kepid]['attributes']['status'] = "CONFIRMED" click.echo("attributes fetched, updating GTS with the new attributes") files = set(files) for _, value in koisdict.items(): mc2 = Template(META_UPDATE_MC2_TEMPLATE) mc2 = mc2.substitute(rt=rtoken, wt=wtoken, labels="~(" + ")|(".join(value['filenames']) + ")", attributes=json.dumps(value['attributes'])) r = requests.post(endpoint + '/api/v0/exec', data = mc2) if r.status_code is not 200: click.echo(r.text) click.echo(mc2) else: click.echo("Warp respond 200")
def inject(kicid, rng=6): # Download the data. client = kplr.API() kic = client.star(kicid) lcs = kic.get_light_curves(short_cadence=False) lc = lcs[np.random.randint(len(lcs))] # Read the data. data = lc.read() t = data["TIME"] f = data["SAP_FLUX"] fe = data["SAP_FLUX_ERR"] q = data["SAP_QUALITY"] # Remove missing points. m = np.isfinite(t) * np.isfinite(f) * np.isfinite(fe) * (q == 0) t, f, fe = t[m], f[m], fe[m] t -= t.min() # Build the transit system. s = transit.System( transit.Central(q1=np.random.rand(), q2=np.random.rand())) body = transit.Body(period=365.25, b=np.random.rand(), r=0.04, t0=np.random.uniform(t.max())) s.add_body(body) # Compute the transit model. texp = kplr.EXPOSURE_TIMES[1] / 86400.0 # Long cadence exposure time. model = s.light_curve(t, texp=texp) f *= model # Trim the dataset to include data only near the transit. m = np.abs(t - body.t0) < rng t, f, fe = t[m], f[m], fe[m] t -= body.t0 # Save the injection as a FITS light curve. dt = [("TIME", float), ("SAP_FLUX", float), ("SAP_FLUX_ERR", float)] data = np.array(zip(t, f, fe), dtype=dt) hdr = dict(b=body.b, period=body.period, r=body.r, t0=0.0, q1=s.central.q1, q2=s.central.q2) fitsio.write("{0}-injection.fits".format(kicid), data, header=hdr, clobber=True) # Plot the light curve. ppm = (f / np.median(f) - 1) * 1e6 fig = pl.figure(figsize=(6, 6)) ax = fig.add_subplot(111) ax.plot(t, ppm, ".k") ax.set_xlim(-rng, rng) ax.set_xlabel("time since transit [days]") ax.set_ylabel("relative flux [ppm]") ax.set_title("raw light curve") fig.subplots_adjust(left=0.2, bottom=0.2, top=0.9, right=0.9) fig.savefig("{0}-raw.pdf".format(kicid))
def update(wtoken, rtoken, endpoint, limit): client = kplr.API() click.echo('Fetching all KOI where koi_period>{}'.format(limit)) # Getting all Kepler object of Interests, not K2 kois = client.kois(where="koi_period>{}".format(limit), sort="kepid") click.echo('Found {} KOIS'.format(len(kois))) koisdict = {} for koi in kois: disposition = koi.koi_disposition score = koi.koi_score kepid = koi.kepid kepoi_name = koi.kepoi_name kepler_name = koi.kepler_name if score is None: score = -1 if kepler_name is None: kepler_name = '' # Example: # kepoi_name:K00992.01, kepler_name:Kepler-745 b, disposition:CONFIRMED, score:0 click.echo( 'fetched info for {}: kepoi_name:{}, kepler_name:{}, disposition:{}, score:{}' .format(kepid, kepoi_name, kepler_name, disposition, score)) lcs = koi.get_light_curves(short_cadence=False) files = [] for lcsfile in lcs: _, filename = os.path.split(lcsfile.filename) files.append(filename) if "": click.echo("ktwo found! {}".format(filename)) sys.exit(1) if kepid in koisdict: koisdict[kepid]['attributes']['{}'.format(kepoi_name)] = { 'kepler_name': kepler_name, 'disposition': disposition, 'score': score, } koisdict[kepid]['attributes']['size'] += 1 else: koisdict[kepid] = { 'filenames': files, 'kepid': kepid, 'attributes': { '{}'.format(kepoi_name): { 'kepler_name': kepler_name, 'disposition': disposition, 'score': score, }, 'size': 1, }, } click.echo("attributes fetched, updating GTS with the new attributes") for _, value in koisdict.items(): mc2 = Template(META_UPDATE_MC2_TEMPLATE) mc2 = mc2.substitute(rt=rtoken, wt=wtoken, filenames="~(" + ")|(".join(value['filenames']) + ")", attributes=json.dumps(value['attributes'])) click.echo('{}'.format(mc2))