def ConvolveSmooth(self, numiters=10, lowreject=2, highreject=3): done = False data = self.smoothing_data.copy() # data.y /= data.cont iterations = 0 if self.window_size % 2 == 0: self.window_size += 1 while not done and iterations < numiters: iterations += 1 done = True y = FittingUtilities.savitzky_golay(data.y, self.window_size, 5) #s = np.r_[data.y[self.window_size/2:0:-1], data.y, data.y[-1:-self.window_size/2:-1]] #y = np.convolve(window/window.sum(), s, mode='valid') reduced = data.y / y sigma = np.std(reduced) mean = np.mean(reduced) badindices = \ np.where(np.logical_or((reduced - mean) / sigma < -lowreject, (reduced - mean) / sigma > highreject))[0] if badindices.size > 0: done = False data.y[badindices] = y[badindices] return DataStructures.xypoint(x=self.smoothing_data.x, y=y / self.smoothing_data.cont)
orders.pop(numorders - 1 - i) else: order.cont = FittingUtilities.Continuum(order.x, order.y, lowreject=3, highreject=3) orders[numorders - 1 - i] = order.copy() """ for i, order in enumerate(orders): plt.plot(order.x, order.y/order.cont+i) plt.show() sys.exit() """ #Smooth data if smooth: for order in orders: order.y /= FittingUtilities.savitzky_golay(order.y, 91, 5) output_dir = "Cross_correlations/" outfilebase = fname.split(".fits")[0] if "/" in fname: dirs = fname.split("/") output_dir = "" outfilebase = dirs[-1].split(".fits")[0] for directory in dirs[:-1]: output_dir = output_dir + directory + "/" output_dir = output_dir + "Cross_correlations/" #Do the cross-correlation for vsini in [10, 20, 30, 40]: Correlate.PyCorr2(orders, resolution=60000, outdir=output_dir, models=model_data, stars=star_list, temps=temp_list, gravities=gravity_list, metallicities=metal_list, vsini=vsini * units.km.to(units.cm), debug=False, outfilebase=outfilebase)
def Filter(fname, vsini=100, numiters=100, lowreject=3, highreject=3): orders = FitsUtils.MakeXYpoints(fname, extensions=True, x="wavelength", y="flux", errors="error") column_list = [] for i, order in enumerate(orders): smoothed = HelperFunctions.IterativeLowPass( order, vsini * units.km.to(units.cm), numiter=numiters, lowreject=lowreject, highreject=highreject ) plt.plot(order.x, order.y) plt.plot(order.x, smoothed) plt.show() continue done = False x = order.x.copy() y = order.y.copy() indices = np.array([True] * x.size) iteration = 0 while not done and iteration < numiters: done = True iteration += 1 smoothed = FittingUtilities.savitzky_golay(y, window_size, smoothorder) residuals = y / smoothed if iteration == 1: # Save the first and last points, for interpolation first, last = smoothed[0], smoothed[-1] mean = residuals.mean() std = residuals.std() print residuals.size, x.size, y.size # plt.plot((residuals - mean)/std) # plt.show() badindices = np.where( np.logical_or((residuals - mean) / std > highreject, (residuals - mean) / std < -lowreject) )[0] if badindices.size > 1 and y.size - badindices.size > 2 * window_size and iteration < numiters: done = False x = np.delete(x, badindices) y = np.delete(y, badindices) print "iter = %i" % iteration if x[0] > order.x[0]: x = np.append(np.array([order.x[0]]), x) smoothed = np.append(np.array([first]), smoothed) if x[-1] < order.x[-1]: x = np.append(x, np.array([order.x[-1]])) smoothed = np.append(smoothed, np.array([last])) print x.size, y.size, smoothed.size smooth_fcn = interp(x, smoothed, k=1) smoothed = smooth_fcn(order.x) order.cont = FittingUtilities.Continuum(order.x, order.y) plt.figure(1) plt.plot(order.x, order.y / order.cont, "k-") plt.plot(order.x, smoothed / order.cont, "r-") # plt.figure(2) # plt.plot(order.x, order.y/smoothed) # plt.plot(order.x, smoothed) # orders[i].y /= smoothed column = { "wavelength": order.x, "flux": order.y / smoothed, "continuum": np.ones(order.x.size), "error": order.err, } column_list.append(column) plt.show() outfilename = "%s_smoothed.fits" % (fname.split(".fits")[0]) print "Outputting to %s" % outfilename