def interpolate(time, flux, approx_duration): min_gap = approx_duration / gap_days # p.close(5) # p.figure(5) # p.subplot(2,1,1) # p.plot(time, flux, 'k.') """ Calculate noise properties """ flux_filt = filter.filt1d(flux, 10, 5) med_noise, sig_noise = filter.medsig(flux - flux_filt) """ Find gaps greater than 1 (1.1) data points and linear interp with noise """ diff1 = time[1:] - time[:-1] diff1 = scipy.append(diff1, gap_days) gap_find = diff1 > min_gap * gap_days * 0.9 # 1.1*gap_days gap_times = time[gap_find] time_index = scipy.r_[0 : len(time) : 1] fill_arr_t = scipy.zeros(1) fill_arr_f = scipy.zeros(1) # fill_arr_nan = scipy.zeros(1) print("Filling gaps...") for m in scipy.arange(len(gap_times)): time_start = time[time_index[gap_find][m]] flux_start = flux[time_index[gap_find][m]] time_end = time[time_index[gap_find][m] + 1] flux_end = flux[time_index[gap_find][m] + 1] span = time_end - time_start if span < 2.0 * gap_days: fill = scipy.array([time_start, time_start + gap_days, time_end]) else: fill = scipy.r_[time_start:time_end:gap_days] fill = fill[1:-1] if time_end - fill.max() > 1.1 * gap_days: # 1.1*gap_days*min_gap: fill = scipy.append(fill, fill.max() + gap_days) f = scipy.interpolate.interp1d([time_start, time_end], [flux_start, flux_end]) gap_new = f(fill) if sig_noise > 0: gap_new += normal(0, sig_noise, len(fill)) fill_arr_t = scipy.append(fill_arr_t, fill) fill_arr_f = scipy.append(fill_arr_f, gap_new) # fill_arr_nan = scipy.append(fill_arr_nan, scipy.ones(len(fill))*scipy.nan) # combine time and flux arrays with their filled sections fill_arr_t = fill_arr_t[1:] fill_arr_f = fill_arr_f[1:] # fill_arr_nan = fill_arr_nan[1:] fill_arr_t = scipy.append(fill_arr_t, time) fill_arr_f = scipy.append(fill_arr_f, flux) # fill_arr_nan = scipy.append(fill_arr_nan, flux_base) if len(fill_arr_t) == 0: print("*************** empty time array ***************") return False, atpy.Table(), 0, 0 # put in table and sort tf = atpy.Table() tf.add_column("time", fill_arr_t) tf.add_column("flux", fill_arr_f) # tf.add_column('flux_pdc', fill_arr_nan) tf.sort("time") # for i in range(0, len(gap_times)): # p.axvline(gap_times[i], color = 'y') # p.subplot(2,1,2) # p.plot(fill_arr_t, fill_arr_f, 'k.') return tf.time, tf.flux
def load_lc(quarter_load, kid_x = None, index = None, tr_out = False, tr_type = False): # read in tableset of LCs for all Qs, normalise and quarter join dir_lc = '/Users/angusr/angusr/data2/Q%s_public' %quarter_load + '/Light_Curves' print 'Reading in LCs...' tset, found = kt.GetLc(id = kid_x, dir = dir_lc, tr_out = tr_out, \ filename = index.filename[(index.keplerid == kid_x) * (index.quarter <= qmax) * (index.quarter >= qmin)]) if found == 0: print '****not found****' return False, atpy.Table(), 0, 0 # find number of Quarters and max(time) per quarter if tr_out == False: lc = tset[0] else: lc = tset[1] quarters = scipy.array(list(set(lc.Q))) tablen = len(quarters) qt_max = scipy.zeros(tablen) for z in scipy.arange(len(quarters)): qt_max[z] = max(lc.TIME[lc.Q == quarters[z]]) qt_max = scipy.append(0,qt_max) finite = scipy.where((scipy.isfinite(lc.TIME) == True) * (scipy.isfinite(lc.SAP_FLUX) == True) * \ (scipy.isfinite(lc.PDCSAP_FLUX) == True)) # keep unchanged set for table flux_base = lc.SAP_FLUX[finite] q_arr_base = lc.Q[finite] for qt in quarters: flux_base[q_arr_base == qt] = (flux_base[q_arr_base == qt] / scipy.median(flux_base[q_arr_base == qt])) - 1.0 time = lc.TIME flux_raw = lc.SAP_FLUX flux = lc.PDCSAP_FLUX if tr_out == True: print 'Removing eclipses...' # remove in-eclipse regions phase, inTr = kt.TransitPhase(tset) npl, nobs = inTr.shape for ipl in scipy.arange(npl): trlist = inTr[ipl,:].astype(bool) time[trlist] = scipy.nan flux_raw[trlist] = scipy.nan flux[trlist] = scipy.nan if tr_type == 'EB': print 'Removing EB transits...' phase, inTr = EBTransitPhase(tset, kid_x) npl, nobs = inTr.shape for ipl in scipy.arange(npl): trlist = inTr[ipl,:].astype(bool) time[trlist] = scipy.nan flux_raw[trlist] = scipy.nan flux[trlist] = scipy.nan pylab.plot(time, flux, 'r.') finite_tr = scipy.where((scipy.isfinite(time) == True) * \ (scipy.isfinite(flux) == True) * (scipy.isfinite(flux_raw) == True) * (scipy.isfinite(lc.Q) == True)) time = time[finite_tr] flux = flux[finite_tr] flux_raw = flux_raw[finite_tr] quarter_arr = lc.Q[finite_tr] # median normalise each quarter for qt in quarters: flux[quarter_arr == qt] = (flux[quarter_arr == qt] / scipy.median(flux[quarter_arr == qt])) - 1.0 flux_raw[quarter_arr == qt] = (flux_raw[quarter_arr == qt] / scipy.median(flux_raw[quarter_arr == qt])) - 1.0 # fit 1d polynomial and remove >5sig outliers coeff = scipy.polyfit(time,flux, 1) p = scipy.poly1d(coeff) med, sig = filter.medsig(flux - p(time)) sigclip = abs(flux - p(time)) <= 5*sig time = time[sigclip] flux = flux[sigclip] flux_base = flux_base[sigclip] # to get noise properties flux_filt = filter.filt1d(flux, 10, 5) med_noise, sig_noise = filter.medsig(flux-flux_filt) # find gaps greater than 1 (1.1) data points and linear interp with noise diff1 = time[1:] - time[:-1] diff1 = scipy.append(diff1, gap_days) gap_find = diff1 > 1.1*gap_days gap_times = time[gap_find] time_index = scipy.r_[0:len(time):1] fill_arr_t = scipy.zeros(1) fill_arr_f = scipy.zeros(1) fill_arr_nan = scipy.zeros(1) print 'Filling gaps...' for m in scipy.arange(len(gap_times)): time_start = time[time_index[gap_find][m]] flux_start = flux[time_index[gap_find][m]] time_end = time[time_index[gap_find][m] + 1] flux_end = flux[time_index[gap_find][m] + 1] span = time_end - time_start if span < 2.0*gap_days: fill = scipy.array([time_start, time_start+gap_days, time_end]) else: fill = scipy.r_[time_start: time_end: gap_days] fill = fill[1:-1] if time_end - fill.max() > 1.1*gap_days: fill = scipy.append(fill, fill.max()+gap_days) f = scipy.interpolate.interp1d([time_start, time_end], [flux_start, flux_end]) gap_new = f(fill) if sig_noise > 0: gap_new += normal(0,sig_noise,len(fill)) fill_arr_t = scipy.append(fill_arr_t, fill) fill_arr_f = scipy.append(fill_arr_f, gap_new) fill_arr_nan = scipy.append(fill_arr_nan, scipy.ones(len(fill))*scipy.nan) # combine time and flux arrays with their filled sections fill_arr_t = fill_arr_t[1:] fill_arr_f = fill_arr_f[1:] fill_arr_nan = fill_arr_nan[1:] fill_arr_t = scipy.append(fill_arr_t, time) fill_arr_f = scipy.append(fill_arr_f, flux) fill_arr_nan = scipy.append(fill_arr_nan, flux_base) if len(fill_arr_t) == 0: print '*************** empty time array ***************' return False, atpy.Table(), 0, 0 # put in table and sort tf = atpy.Table() tf.add_column('time', fill_arr_t) tf.add_column('flux', fill_arr_f) tf.add_column('flux_pdc', fill_arr_nan) tf.sort('time') return True, tf, qt_max, tablen
def interpolate(time, flux, approx_duration): min_gap = approx_duration/gap_days # p.close(5) # p.figure(5) # p.subplot(2,1,1) # p.plot(time, flux, 'k.') ''' Calculate noise properties ''' flux_filt = filter.filt1d(flux, 10, 5) med_noise, sig_noise = filter.medsig(flux-flux_filt) ''' find gaps greater than 1 (1.1) data points and linear interp with noise''' diff1 = time[1:] - time[:-1] diff1 = scipy.append(diff1, gap_days) gap_find = diff1 > min_gap*gap_days*0.9 #1.1*gap_days gap_times = time[gap_find] time_index = scipy.r_[0:len(time):1] fill_arr_t = scipy.zeros(1) fill_arr_f = scipy.zeros(1) #fill_arr_nan = scipy.zeros(1) print 'Filling gaps...' for m in scipy.arange(len(gap_times)): time_start = time[time_index[gap_find][m]] flux_start = flux[time_index[gap_find][m]] time_end = time[time_index[gap_find][m] + 1] flux_end = flux[time_index[gap_find][m] + 1] span = time_end - time_start if span < 2.0*gap_days: fill = scipy.array([time_start, time_start+gap_days, time_end]) else: fill = scipy.r_[time_start: time_end: gap_days] fill = fill[1:-1] if time_end - fill.max() > 1.1*gap_days: #1.1*gap_days*min_gap: fill = scipy.append(fill, fill.max()+gap_days) f = scipy.interpolate.interp1d([time_start, time_end], [flux_start, flux_end]) gap_new = f(fill) if sig_noise > 0: gap_new += normal(0,sig_noise,len(fill)) fill_arr_t = scipy.append(fill_arr_t, fill) fill_arr_f = scipy.append(fill_arr_f, gap_new) #fill_arr_nan = scipy.append(fill_arr_nan, scipy.ones(len(fill))*scipy.nan) # combine time and flux arrays with their filled sections fill_arr_t = fill_arr_t[1:] fill_arr_f = fill_arr_f[1:] #fill_arr_nan = fill_arr_nan[1:] fill_arr_t = scipy.append(fill_arr_t, time) fill_arr_f = scipy.append(fill_arr_f, flux) #fill_arr_nan = scipy.append(fill_arr_nan, flux_base) if len(fill_arr_t) == 0: print '*************** empty time array ***************' return False, atpy.Table(), 0, 0 # put in table and sort tf = atpy.Table() tf.add_column('time', fill_arr_t) tf.add_column('flux', fill_arr_f) # tf.add_column('flux_pdc', fill_arr_nan) tf.sort('time') # for i in range(0, len(gap_times)): # p.axvline(gap_times[i], color = 'y') # p.subplot(2,1,2) # p.plot(fill_arr_t, fill_arr_f, 'k.') return tf.time, tf.flux