t_div = target / r_avg t_err = np.sqrt(target) # find the target uncertainty t_div_err = t_div * np.sqrt((t_err / target)**2 + (r_avg_err / r_avg)**2) from astropy.stats import LombScargle radius = 0.01 upper_range = 0.054105527 + radius lower_range = 0.054105527 - radius frequency = np.linspace(lower_range, upper_range, 1000) power = LombScargle(hr, t_div, t_div_err).power(frequency) print "Power at peak frequency: ", np.max(power) print "Peak frequency: ", frequency[power.argmax()] # PLOT LOMB-SCARGLE fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12.8, 7.6)) ax1.plot(frequency, power, 'k') ax1.set_title("Lomb-Scargle Periodogram of HD 345439") ax1.set_xlabel("Frequency (Cycles per hour)") ax1.set_ylabel("Power") ax1.set_xlim([np.min(frequency), np.max(frequency)]) ax1.annotate("Power at peak frequency: %.4f" % np.max(power), xy=(0.01, 0.85), xycoords='axes fraction') ax1.annotate("Peak Frequency: %.11f" % frequency[power.argmax()], xy=(0.01, 0.90), xycoords='axes fraction')
def kepperiodogram(infile, outfile=None, datacol='PDCSAP_FLUX', pmin=0.1, pmax=10., nfreq=2000, plot=False, noninteractive=False, overwrite=False, verbose=False, logfile='kepperiodogram.log'): """ kepperiodogram -- Calculate and store a Lomb Scargle Periodogram based on a Kepler time series. The result is stored in a new FITS file that is a direct copy of the input file but with an additional table extension containing the periodogram. Parameters ---------- infile : str The name of a MAST standard format FITS file containing a Kepler light curve within the first data extension. outfile : str The name of the output FITS file with a new extension containing the Fourier spectrum. datacol : str The name of the FITS table column in extension 1 of infile upon which the Fourier transform will be calculated. pmin : float [day] The minimum of the period range over which the Fourier transform will be calculated. pmax : float [day] The maximum of the period range over which the Fourier transform will be calculated. nfreq : int The number of uniform frequency steps between :math:`1/pmax` and :math:`1/pmin` that the Fourier transform will be calculated. plot : bool Plot the output Fourier spectrum? non-interactive : bool If True, prevents the matplotlib window to pop up. overwrite : bool Overwrite the output file? verbose : bool Print informative messages and warnings to the shell and logfile? logfile : str Name of the logfile containing error and warning messages. Examples -------- .. code-block:: bash $ kepperiodogram kplr002436324-2009259160929_llc.fits --pmin 0.5 --pmax 100 --nfreq 1000 --plot --verbose .. image:: ../_static/images/api/kepperiodogram.png :align: center """ if outfile is None: outfile = infile.split('.')[0] + "-{}.fits".format(__all__[0]) ## log the call hashline = '--------------------------------------------------------------' kepmsg.log(logfile, hashline, verbose) call = ('kepperiodogram -- ' + ' infile={}'.format(infile) + ' outfile={}'.format(outfile) + ' datacol={}'.format(datacol) + ' pmin={}'.format(pmin) + ' pmax={}'.format(pmax) + ' nfreq={}'.format(nfreq) + ' plot={}'.format(plot) + ' noninteractive={}'.format(noninteractive) + ' overwrite={}'.format(overwrite) + ' verbose={}'.format(verbose) + ' logfile={}'.format(logfile)) kepmsg.log(logfile, call+'\n', verbose) ## start time kepmsg.clock('Start time is', logfile, verbose) ## overwrite output file if overwrite: kepio.overwrite(outfile, logfile, verbose) if kepio.fileexists(outfile): errmsg = 'ERROR -- kepperiodogram: {} exists. Use --overwrite'.format(outfile) kepmsg.err(logfile, errmsg, verbose) ## open input file instr = pyfits.open(infile) tstart, tstop, bjdref, cadence = kepio.timekeys(instr, infile, logfile, verbose) ## fudge non-compliant FITS keywords with no values instr = kepkey.emptykeys(instr, infile, logfile, verbose) ## read table columns try: barytime = instr[1].data.field('barytime') + bjdref except: barytime = kepio.readfitscol(infile, instr[1].data, 'time', logfile, verbose) + bjdref signal = kepio.readfitscol(infile, instr[1].data, datacol, logfile, verbose) ## remove infinite data from time series incols = [barytime, signal] outcols = kepstat.removeinfinlc(signal, incols) barytime = outcols[0] signal = outcols[1] - np.median(outcols[1]) ## period to frequency conversion fmin = 1.0 / pmax fmax = 1.0 / pmin deltaf = (fmax - fmin) / nfreq ## loop through frequency steps; determine FT power fr = np.linspace(fmin,fmax,nfreq) power = LombScargle(barytime, signal, deltaf).power(fr) #find highest power period period = 1. / fr[power.argmax()] ## write output file col1 = pyfits.Column(name='FREQUENCY', format='E', unit='1/day', array=fr) col2 = pyfits.Column(name='POWER', format='E', array=power) cols = pyfits.ColDefs([col1, col2]) instr.append(pyfits.BinTableHDU.from_columns(cols)) instr[-1].header['EXTNAME'] = ('POWER SPECTRUM', 'extension name') instr[-1].header['PERIOD'] = (period, 'most significant trial period [d]') kepmsg.log(logfile, "kepperiodogram - best period found: {}".format(period), verbose) kepmsg.log(logfile, "Writing output file {}...".format(outfile), verbose) instr.writeto(outfile) ## history keyword in output file kepkey.history(call, instr[0], outfile, logfile, verbose) ## close input file instr.close() ## data limits nrm = int(math.log10(power.max())) power = power / 10 ** nrm ylab = 'Power (x10$^{}$)'.format(nrm) xmin = fr.min() xmax = fr.max() ymin = power.min() ymax = power.max() xr = xmax - xmin yr = ymax - ymin fr = np.insert(fr, [0], fr[0]) fr = np.append(fr, fr[-1]) power = np.insert(power, [0], 0.0) power = np.append(power, 0.0) if plot: plt.figure() plt.clf() plt.axes([0.06, 0.113, 0.93, 0.86]) plt.plot(fr, power, color='#0000ff', linestyle='-', linewidth=1.0) plt.fill(fr, power, color='#ffff00', linewidth=0.0, alpha=0.2) plt.xlim(xmin - xr * 0.01, xmax + xr * 0.01) if ymin - yr * 0.01 <= 0.0: plt.ylim(1.0e-10, ymax + yr * 0.01) else: plt.ylim(ymin - yr * 0.01, ymax + yr *0.01) plt.xlabel(r'Frequency (d$^{-1}$)', {'color' : 'k'}) plt.ylabel(ylab, {'color' : 'k'}) plt.grid() # render plot if not noninteractive: plt.show() ## end time kepmsg.clock('kepperiodogram completed at', logfile, verbose)