def run_mc(field, bin, w, spec, wssps, ssps, Nsim, redo=False): """ Run MC routine in single spectrum. """ outname = "mclick_{0}_bin{1:04d}_nsim{2}.txt".format(field, bin, Nsim) if os.path.exists(outname) and redo: return pp = ppload("{0}_bin{1}".format(field, bin)) pp = pPXF(pp, velscale) ########################################################################## if pp.has_emission: # Correct for emission line if present em = interp1d(pp.w, pp.gas, kind="linear", bounds_error=False, fill_value=0.) emission = em(w) spec -= emission ########################################################################## # Make the V=0 spectra poly = interp(wssps, pp.w, pp.poly) ssps_unbroad_v0 = ssps.dot(pp.w_ssps) ssps_unbroad = losvd_convolve(ssps_unbroad_v0, np.array([pp.sol[0], 0.1*velscale, 0, 0])) ssps_broad = losvd_convolve(ssps_unbroad_v0, pp.sol) bestfit = ssps_broad + poly # Extrapolated in wavelength and no emission bestfit_unbroad = ssps_unbroad + poly ########################################################################## # Calculate arrays with perturbed LOSVD if pp.ncomp == 1: sol = pp.sol error = pp.error nmoments = pp.moments else: sol = pp.sol[0] error = pp.error[0] nmoments = pp.moments[0] losvdpert = np.zeros((Nsim, int(nmoments))) for i in range(nmoments): losvdpert[:,i] = np.random.normal(sol[i], error[i], Nsim) ########################################################################## sim = np.zeros((Nsim, 25)) # Start the simulations for j in np.arange(Nsim): losvd = losvdpert[j] noise = np.random.normal(0, pp.noise, bestfit_unbroad.size) galaxy = bestfit + noise lick, lerr = lector.lector(wssps, galaxy, noise, bands, vel=losvd[0]) lick_bestfit, err = lector.lector(wssps, bestfit, noise, bands, vel=losvd[0]) lick_bestfit_unbroad, err = lector.lector(wssps, bestfit_unbroad, noise, bands, vel=losvd[0]) lickc, err = correct_indices(lick, lerr, lick_bestfit_unbroad, lick_bestfit) sim[j] = lickc print "Saving table ", outname with open(outname, "w") as f: np.savetxt(f, sim) return
def run_mc(spec, i): """ Run MC routine in single spectrum. """ outname = "mc_logs/{0}_nsim{1}.txt".format(spec.replace(".fits", ""), Nsim) output = os.path.join(wdir, outname) if os.path.exists(output): return print "{0} ({1}/{2})".format(spec, i+1, len(specs)) pp = pPXF(spec, velscale) sn = pp.calc_sn() ##################################################################### # Extracting emission line spectra and subtracting from data ##################################################################### if pp.has_emission: em_weights = pp.weights[-3:] em_matrix = pp.matrix[:,-3:] em = em_matrix.dot(em_weights) else: em = np.zeros_like(pp.bestfit) ######################################################################### # Handle cases where more than one component is used if pp.ncomp > 1: sol = pp.sol[0] error = pp.error[0] else: sol = pp.sol error = pp.error ########################################################################## if error[1] == 0.0: print "Skiped galaxy: unconstrained sigma." return ########################################################################## lick_sim = np.zeros((Nsim, 25)) vpert = np.random.normal(sol[0], error[0], Nsim) sigpert = np.random.normal(sol[1], error[1], Nsim) for j in np.arange(Nsim): noise_sim = np.random.normal(0, pp.noise, len(pp.bestfit)) obs_sim = lector.broad2lick(pp.w, pp.bestfit + noise_sim - em, 2.54, vel=vpert[j]) l, err = lector.lector(pp.w, obs_sim, noise_sim, bands, vel = vpert[j], cols=(0,8,2,3,4,5,6,7), keeplog=0) lick_sim[j] = l * bcorr(sigpert[j], l) with open(output, "w") as f: np.savetxt(f, lick_sim) print "Finished MC for {0}.".format(spec) return
names = [ r'Hd$_A$', r'Hd$_F$', r'CN$_1$', r'CN$_2$', 'Ca4227', r'G4300', r'Hg_A', r'Hg_F', r'Fe4383', r'Ca4455', r'Fe4531', r'C4668', r'H$\beta$', r'Fe5015', r'Mg$_1$', r'Mg$_2', r'Mg $b$', r'Fe5270', r'Fe5335', r'Fe5406', r'Fe5709', r'Fe5782', r'Na_D', r'TiO_1', r'TiO_2' ] textsize = 16 if save: outfile = PdfPages("ppxf_results.pdf") for i, spec in enumerate(specs): print spec name = spec.replace(".fits", '').replace("n3311", "").split("_") name = name[1] + name[2] name = r"{0}".format(name) plt.minorticks_on() pp = pPXF(spec, velscale, pklfile=spec.replace(".fits", ".pkl")) pp.calc_sn() pp.calc_arrays_emission() if pp.ncomp > 1: sol = pp.sol[0] error = pp.error[0] sol2 = pp.sol[1] error2 = pp.error[1] else: sol = pp.sol error = pp.error if pp.sky != None: pp.galaxy -= pp.sky[0] * pp.weights[-1] pp.bestfit -= pp.sky[0] * pp.weights[-1] plt.plot(pp.w_log, pp.galaxy, "-k")
def run_lick(group, specs=None, logdir="ppxf_mom4_bias0.7"): """ Calculates the Lick indices using the reduced spectra. """ global velscale wdir = os.path.join(data_dir, group) os.chdir(wdir) if specs == None: specs = sorted([x for x in os.listdir(wdir) if x.endswith(".fits")]) bandsfile = os.path.join(home, "tables/bands.txt") bands = np.loadtxt(bandsfile, usecols=np.arange(2, 8)) types = np.loadtxt(bandsfile, usecols=(8, )) tempfile = os.path.join( home, "MILES/templates/" "templates_w3540.5_7409.6_res4.7.fits") ssps = pf.getdata(tempfile, 0).T wssps = np.exp(wavelength_array(tempfile, axis=1, extension=0)) for spec in specs: data = pf.getdata(spec) w = wavelength_array(spec, axis=1, extension=0) pp = ppload("{}/{}".format(logdir, spec.replace(".fits", ""))) pp = pPXF(pp, velscale) ################################################################## # Produce bestfit templates convolved with LOSVD/redshifted # Make the combination ssps_unbroad_v0 = ssps.dot(pp.w_ssps) ssps_broad = losvd_convolve(ssps_unbroad_v0, pp.sol[0]) ssps_unbroad = losvd_convolve( ssps_unbroad_v0, np.array([pp.sol[0][0], 0.1 * velscale, 0, 0])) ####################################################################### # get approximate multiplicative polynomial ftempl = interp1d(wssps, ssps_broad, kind="linear", fill_value="extrapolate", bounds_error=False) goodpixels = np.arange(len(w), dtype=float) gaps = set_badpixels(group) for gap in gaps: idx = np.where((w > gap[0]) & (w < gap[1]))[0] goodpixels[idx] = np.nan goodpixels = goodpixels[~np.isnan(goodpixels)].astype(int) fdata = interp1d(w[goodpixels], data[goodpixels], kind="linear", fill_value="extrapolate", bounds_error=False) poly = np.poly1d(np.polyfit(w, fdata(w) / ftempl(w), 10))(w) ####################################################################### # Interpolate bestfit templates to obtain linear dispersion b0 = interp1d(wssps, ssps_unbroad, kind="linear", fill_value="extrapolate", bounds_error=False) b1 = interp1d(wssps, ssps_broad, kind="linear", fill_value="extrapolate", bounds_error=False) best_unbroad = b0(w) * poly best_broad = b1(w) * poly ################################################################## # Correct for emission lines em = interp1d(pp.w, pp.gas, kind="linear", bounds_error=False, fill_value=0.) emission = em(w) data -= emission ################################################################### # Get goodpixels # badpix = np.zeros(len(pp.galaxy)) # badpix[pp.goodpixels] = 1 # bp = interp1d(pp.w, badpix, kind="linear", bounds_error=False, # fill_value=1.) # badpixels = bp(w) ################################################################### # Run the lector l = Lick(w, data, bands, vel=pp.sol[0][0]) lunb = Lick(w, best_unbroad, bands, vel=pp.sol[0][0]) lbest = Lick(w, best_broad, bands, vel=pp.sol[0][0]) ################################################################## # LOSVD correction using best fit templates ################################################################## lickc = correct_lick(l, lunb, lbest, types) # plt.plot(np.arange(25) + 0.3, l.classic, "ro", label="data") # plt.plot(np.arange(25), lunb.classic, "bo", label="unconvolved") # plt.plot(np.arange(25), lbest.classic, "ro", label="bestfit") # plt.plot(np.arange(25) + 0.3, lickc, "bo", label="corrected") # plt.axhline(y=0, c="k", ls="--") # plt.legend() # plt.title("{} : {}".format(group, spec)) # plt.show(block=True) ################################################################ # Convert to string ################################################################ lick = "".join( ["{0:14}".format("{0:.5f}".format(x)) for x in l.classic]) lickc = "".join(["{0:14}".format("{0:.5f}".format(x)) for x in lickc]) # # Append to output logfile1 = "{}/lick_{}_raw.txt".format(logdir, spec.replace(".fits", "")) logfile2 = "{}/lick_{}_losvd_corrected.txt".format( logdir, spec.replace(".fits", "")) with open(logfile1, "w") as f: f.write("{0:16s}".format(pp.name) + lick + "\n") with open(logfile2, "w") as f: f.write("{0:16s}".format(pp.name) + lickc + "\n") return
def mc_lick(group, specs=None, logdir="ppxf_mom4_bias0.7", nsim=100): """ Perform Monte Carlo simulations to calculate uncertainties. """ global velscale wdir = os.path.join(data_dir, group) os.chdir(wdir) if specs == None: specs = sorted([x for x in os.listdir(wdir) if x.endswith(".fits")]) bandsfile = os.path.join(home, "tables/bands.txt") bands = np.loadtxt(bandsfile, usecols=np.arange(2, 8)) types = np.loadtxt(bandsfile, usecols=(8, )) tempfile = os.path.join( home, "MILES/templates/" "templates_w3540.5_7409.6_res4.7.fits") ssps = pf.getdata(tempfile, 0).T wssps = np.exp(wavelength_array(tempfile, axis=1, extension=0)) for spec in specs: print "Simulating errors for spectrum {} of system {}".format( spec, group) pp = ppload("{}/{}".format(logdir, spec.replace(".fits", ""))) pp = pPXF(pp, velscale) sol = pp.sol[0] error = pp.error[0] w = wavelength_array(spec, axis=1, extension=0) data = pf.getdata(spec) ####################################################################### # get approximate multiplicative polynomial ssps_unbroad_v0 = ssps.dot(pp.w_ssps) ssps_broad = losvd_convolve(ssps_unbroad_v0, pp.sol[0]) ftempl = interp1d(wssps, ssps_broad, kind="linear", fill_value="extrapolate", bounds_error=False) goodpixels = np.arange(len(w), dtype=float) gaps = set_badpixels(group) for gap in gaps: idx = np.where((w > gap[0]) & (w < gap[1]))[0] goodpixels[idx] = np.nan goodpixels = goodpixels[~np.isnan(goodpixels)].astype(int) fdata = interp1d(w[goodpixels], data[goodpixels], kind="linear", fill_value="extrapolate", bounds_error=False) poly = np.poly1d(np.polyfit(w, fdata(w) / ftempl(w), 10))(w) ################################################################## # Produce composite stellar population csp = ssps.dot(pp.w_ssps) ################################################################## # Setup simulations vpert = np.random.normal(sol[0], np.maximum(error[0], 10), nsim) sigpert = np.random.normal(sol[1], np.maximum(error[1], 10), nsim) h3pert = np.random.normal(sol[2], np.maximum(error[2], 0.02), nsim) h4pert = np.random.normal(sol[3], np.maximum(error[3], 0.02), nsim) losvdsim = np.column_stack((vpert, sigpert, h3pert, h4pert)) licksim = np.zeros((nsim, 25)) ################################################################## for i, losvd in enumerate(losvdsim): noise = np.random.normal(0., np.median(pp.noise), len(wssps)) bestsim = losvd_convolve(csp, losvd) bestsim_unb = losvd_convolve( csp, np.array([losvd[0], 0.1 * losvd[1], 0, 0])) galsim = bestsim + noise ############################################################## # Run the lector lsim = Lick(wssps, galsim, bands, vel=losvd[0]) lunb = Lick(wssps, bestsim_unb, bands, vel=losvd[0]) lbest = Lick(wssps, bestsim, bands, vel=losvd[0]) ############################################################## # LOSVD correction using best fit templates ############################################################## licksim[i] = correct_lick(lsim, lunb, lbest, types) stds = np.zeros(25) for i in range(25): if np.all(np.isnan(licksim[:, i])): stds[i] = np.nan else: stds[i] = np.nanstd(sigma_clip(licksim[:, i], sigma=5)) errors = ["{0:.5g}".format(x) for x in stds] errors = "".join(["{0:12s}".format(x) for x in errors]) ################################################################### # Storing results logfile = "{}/lick_mcerr_{}_nsim{}.txt".format( logdir, spec.replace(".fits", ""), nsim) with open(logfile, "w") as f: f.write("{0:16s}".format(pp.name) + errors + "\n")
lick_indices = np.genfromtxt(bands, usecols=(0, ), dtype=None).tolist() header = "# Spectra\t" + "\t".join(lick_indices) # Initiating outputss results, results_err = [header], [header] results5, results5_err = [header], [header] bcorr = BroadCorr(os.path.join(tables_dir, "lickcorr_m.txt")) offset = np.loadtxt(os.path.join(tables_dir, "LICK_OFFSETS.dat"), usecols=(1, )).T broad2lick = False for i, spec in enumerate(specs): setupfile = os.path.join(home, "single1/{0}.setup".format(spec)) if not os.path.exists(setupfile): print "Setup file not found: ", setupfile continue # Read the spectrum file and pPXF results pp = pPXF(spec, velscale) pp.calc_arrays_emission() pp.sky_sub() if pp.ncomp > 1: v, s, h3, h4 = pp.sol[0] else: v, s, h3, h4 = pp.sol print spec, v, s goodindices = check_intervals(setupfile, bands, v) ###################################################################### # Check problem with broadening bf = interp1d(pp.w_log, pp.bestfit, bounds_error=False, fill_value="extrapolate") bestfit = bf(pp.w)
lick_indices = np.genfromtxt(bands, usecols=(0,), dtype=None).tolist() header = "# Spectra\t" + "\t".join(lick_indices) # Initiating outputss results, results_err = [header], [header] results5, results5_err = [header], [header] bcorr = BroadCorr(os.path.join(tables_dir, "lickcorr_m.txt")) offset = np.loadtxt(os.path.join(tables_dir,"LICK_OFFSETS.dat"), usecols=(1,)).T broad2lick = False for i, spec in enumerate(specs): setupfile = os.path.join(home, "single1/{0}.setup".format(spec)) if not os.path.exists(setupfile): print "Setup file not found: ", setupfile continue # Read the spectrum file and pPXF results pp = pPXF(spec, velscale) pp.calc_arrays_emission() pp.sky_sub() if pp.ncomp > 1: v, s, h3, h4 = pp.sol[0] else: v, s, h3, h4 = pp.sol print spec, v, s goodindices = check_intervals(setupfile, bands, v) ###################################################################### # Check problem with broadening bf = interp1d(pp.w_log, pp.bestfit, bounds_error=False, fill_value="extrapolate") bestfit = bf(pp.w) bfu = interp1d(pp.w_log, pp.bestfit_unbroad, bounds_error=False, fill_value="extrapolate")
def run(fields, targetSN, w1, w2): global velscale bandsfile = os.path.join(tables_dir, "bands.txt") bands = np.loadtxt(bandsfile, usecols=np.arange(2, 8)) types = np.loadtxt(bandsfile, usecols=(8,)) tempfile = os.path.join(home, "MILES10.0/templates/templates_w{0}_{1}_res2.95.fits".format(w1, w2)) ssps = pf.getdata(tempfile, 0).T wssps = np.exp(wavelength_array(tempfile, axis=1, extension=0)) for field in fields: os.chdir(os.path.join(data_dir, "combined_{0}".format(field))) specs = "binned_sn{0}.fits".format(targetSN) data = pf.getdata(specs) w = wavelength_array(specs, axis=1, extension=0) bins = wavelength_array(specs, axis=2, extension=0) for bin in bins: print "Working on bin {0}".format(bin) spec = data[bin - 1] pp = ppload("logs_sn{0}_w{3}_{4}/{1}_bin{2:04d}".format(targetSN, field, bin, w1, w2)) pp = pPXF(pp, velscale) ################################################################## # Produce bestfit templates convolved with LOSVD/redshifted # Make the combination ssps_unbroad_v0 = ssps.dot(pp.w_ssps) ssps_broad = losvd_convolve(ssps_unbroad_v0, pp.sol[0]) ssps_unbroad = losvd_convolve(ssps_unbroad_v0, np.array([pp.sol[0][0], 0.1 * velscale, 0, 0])) ################################################################## # Interpolate bestfit templates to obtain linear dispersion b0 = interp1d(wssps, ssps_unbroad, kind="linear", fill_value="extrapolate", bounds_error=False) b1 = interp1d(wssps, ssps_broad, kind="linear", fill_value="extrapolate", bounds_error=False) best_unbroad = b0(w) best_broad = b1(w) ################################################################## # Correct for emission lines em = interp1d(pp.w, pp.gas, kind="linear", bounds_error=False, fill_value=0.0) emission = em(w) spec -= emission ################################################################### # Correct for extinction # spec /= reddening_curve(w, pp.reddening) ################################################################### # Run the lector l = Lick(w, spec, bands, vel=pp.sol[0][0]) lunb = Lick(w, best_unbroad, bands, vel=pp.sol[0][0]) lbest = Lick(w, best_broad, bands, vel=pp.sol[0][0]) ################################################################## # LOSVD correction using best fit templates ################################################################## lickc = correct_lick(types, l.classic, lunb.classic, lbest.classic) ################################################################ # Convert to string ################################################################ lick = "".join(["{0:14}".format("{0:.5f}".format(x)) for x in l.classic]) lickc = "".join(["{0:14}".format("{0:.5f}".format(x)) for x in lickc]) # # Append to output logfile1 = "logs_sn{0}_w{3}_{4}/lick_{1}_bin{2:04d}" "_raw.txt".format(targetSN, field, bin, w1, w2) logfile2 = "logs_sn{0}_w{3}_{4}/lick_{1}_bin{2:04d}" "_corr.txt".format(targetSN, field, bin, w1, w2) with open(logfile1, "w") as f: f.write("{0:16s}".format(pp.name) + lick + "\n") with open(logfile2, "w") as f: f.write("{0:16s}".format(pp.name) + lickc + "\n") return
def run_mc(fields, targetSN, w1, w2, nsim=100, redo=False): """ Perform Monte Carlo simulations to calculate uncertainties. """ global velscale bandsfile = os.path.join(tables_dir, "bands.txt") bands = np.loadtxt(bandsfile, usecols=np.arange(2, 8)) types = np.loadtxt(bandsfile, usecols=(8,)) tempfile = os.path.join(home, "MILES10.0/templates/templates_w{0}_{1}_res2.95.fits".format(w1, w2)) ssps = pf.getdata(tempfile, 0).T wssps = np.exp(wavelength_array(tempfile, axis=1, extension=0)) for field in fields: os.chdir(os.path.join(data_dir, "combined_{0}".format(field))) specs = "binned_sn{0}.fits".format(targetSN) w = wavelength_array(specs, axis=1, extension=0) bins = wavelength_array(specs, axis=2, extension=0) for bin in bins: print "Working on {1} bin {0}".format(bin, field) logfile = "logs_sn{0}_w{3}_{4}/lick_{1}_bin{2:04d}" "_mcerr.txt".format(targetSN, field, bin, w1, w2) if os.path.exists(logfile) and not redo: continue try: pp = ppload("logs_sn{0}_w{3}_{4}/{1}_bin{2:04d}".format(targetSN, field, bin, w1, w2)) pp = pPXF(pp, velscale) sol = pp.sol[0] error = pp.error[0] ################################################################## # Produce composite stellar population csp = ssps.dot(pp.w_ssps) ################################################################## # Setup simulations vpert = np.random.normal(sol[0], np.maximum(error[0], 10), nsim) sigpert = np.random.normal(sol[1], np.maximum(error[1], 10), nsim) h3pert = np.random.normal(sol[2], np.maximum(error[2], 0.02), nsim) h4pert = np.random.normal(sol[3], np.maximum(error[3], 0.02), nsim) losvdsim = np.column_stack((vpert, sigpert, h3pert, h4pert)) licksim = np.zeros((nsim, 25)) ################################################################## for i, losvd in enumerate(losvdsim): noise = np.random.normal(0.0, pp.noise, len(wssps)) bestsim = losvd_convolve(csp, losvd) bestsim_unb = losvd_convolve(csp, np.array([losvd[0], 0.1 * losvd[1], 0, 0])) galsim = bestsim + noise ############################################################## # Run the lector lsim = Lick(wssps, galsim, bands, vel=losvd[0]) lunb = Lick(wssps, bestsim_unb, bands, vel=losvd[0]) lbest = Lick(wssps, bestsim, bands, vel=losvd[0]) ############################################################## # LOSVD correction using best fit templates ############################################################## licksim[i] = correct_lick(types, lsim.classic, lunb.classic, lbest.classic) stds = np.zeros(25) for i in range(25): if np.all(np.isnan(licksim[:, i])): stds[i] = np.nan else: stds[i] = np.nanstd(sigma_clip(licksim[:, i], sigma=5)) errors = ["{0:.5g}".format(x) for x in stds] errors = "".join(["{0:12s}".format(x) for x in errors]) ################################################################### # Storing results with open(logfile, "w") as f: f.write("{0:16s}".format(pp.name) + errors + "\n") except: print "Problem on bin {0}".format(bin) continue
r"Fe5709", r"Fe5782", r"Na_D", r"TiO_1", r"TiO_2", ] textsize = 16 if save: outfile = PdfPages("ppxf_results.pdf") for i, spec in enumerate(specs): print spec name = spec.replace(".fits", "").replace("n3311", "").split("_") name = name[1] + name[2] name = r"{0}".format(name) plt.minorticks_on() pp = pPXF(spec, velscale, pklfile=spec.replace(".fits", ".pkl")) pp.calc_sn() pp.calc_arrays_emission() if pp.ncomp > 1: sol = pp.sol[0] error = pp.error[0] sol2 = pp.sol[1] error2 = pp.error[1] else: sol = pp.sol error = pp.error if pp.sky != None: pp.galaxy -= pp.sky[0] * pp.weights[-1] pp.bestfit -= pp.sky[0] * pp.weights[-1] plt.plot(pp.w_log, pp.galaxy, "-k")
def run_candidates_mc(velscale, bands, nsim=50): """ Run MC to calculate errors on Lick indices. """ wdir = os.path.join(home, "data/candidates") os.chdir(wdir) specs = sorted([x for x in os.listdir(wdir) if x.endswith(".fits")]) offset, offerr = lick_offset() lickout = [] for spec in specs: try: ppfile = "logs_ssps/{0}".format(spec.replace(".fits", "")) if not os.path.exists(ppfile + ".pkl"): print "Skiping spectrum: ", spec continue print ppfile pp = ppload("logs_ssps/{0}".format(spec.replace(".fits", ""))) pp = pPXF(spec, velscale, pp) ppkin = ppload("logs/{0}".format(spec.replace(".fits", ""))) ppkin = pPXF(spec, velscale, ppkin) w = wavelength_array(spec, axis=1, extension=0) if pp.ncomp > 1: sol = ppkin.sol[0] error = ppkin.error[0] else: sol = ppkin.sol error = ppkin.error ################################################################### # Produces composite stellar population of reference if pp.ncomp == 1: csp = pp.star.dot(pp.w_ssps) else: csp = pp.star[:,:-pp.ngas].dot(pp.w_ssps) ################################################################### # Make unbroadened bestfit and measure Lick on it best_unbroad_ln = pp.poly + pp.mpoly * losvd_convolve(csp, np.array([sol[0], velscale/10.]), velscale) b0 = interp1d(pp.w, best_unbroad_ln, kind="linear", fill_value="extrapolate", bounds_error=False) best_unbroad_lin = b0(w) best_unbroad_lin = lector.broad2lick(w, best_unbroad_lin, 3.6, vel=sol[0]) lick_unb, tmp = lector.lector(w, best_unbroad_lin, np.ones_like(w), bands, vel=sol[0]) ################################################################### # Setup simulations vpert = np.random.normal(sol[0], error[0], nsim) sigpert = np.random.normal(sol[1], error[1], nsim) h3pert = np.random.normal(sol[2], error[2], nsim) h4pert = np.random.normal(sol[3], error[3], nsim) licksim = np.zeros((nsim, 25)) ################################################################### for i, (v,s,h3,h4) in enumerate(zip(vpert, sigpert, h3pert, h4pert)): solpert = np.array([v,s,h3,h4]) noise = np.random.normal(0., pp.noise, len(w)) best_broad_ln = pp.poly + pp.mpoly * losvd_convolve(csp, solpert, velscale) b1 = interp1d(pp.w, best_broad_ln, kind="linear", fill_value="extrapolate", bounds_error=False) best_broad_lin = b1(w) ############################################################### # Broadening to Lick system best_broad_lin = lector.broad2lick(w, best_broad_lin, 3.6, vel=solpert[0]) lick_br, tmp = lector.lector(w, best_broad_lin, np.ones_like(w), bands, vel=solpert[0]) lick, lickerr = lector.lector(w, best_broad_lin + noise, np.ones_like(w), bands, vel=sol[0]) licksim[i] = correct_lick(bands, lick, lick_unb, lick_br) + \ offset stds = np.zeros(25) for i in range(25): stds[i] = np.std(sigma_clip(licksim[:,i], sigma=5)) stds = np.sqrt(stds**2 + offerr**2) ################################################################### # Storing results lickc = ["{0:.5g}".format(x) for x in stds] line = "".join(["{0:35s}".format(spec)] + \ ["{0:12s}".format(x) for x in lickc]) lickout.append(line) ################################################################### except: print "Problem with spectrum", spec continue # Saving to file with open("lickerr_mc{0}.txt".format(nsim), "w") as f: f.write("\n".join(lickout))
def run_candidates(velscale, bands): """ Run lector on candidates. """ wdir = os.path.join(home, "data/candidates") os.chdir(wdir) specs = sorted([x for x in os.listdir(wdir) if x.endswith(".fits")]) obsres = hydra_resolution() offset, offerr = lick_offset() lickout = [] for spec in specs: ppfile = "logs_ssps/{0}".format(spec.replace(".fits", "")) if not os.path.exists(ppfile + ".pkl"): print "Skiping spectrum: ", spec continue print ppfile pp = ppload("logs_ssps/{0}".format(spec.replace(".fits", ""))) pp = pPXF(spec, velscale, pp) galaxy = pf.getdata(spec) w = wavelength_array(spec, axis=1, extension=0) if pp.ncomp > 1: sol = pp.sol[0] else: sol = pp.sol if pp.ncomp == 1: csp = pp.star.dot(pp.w_ssps) # composite stellar population else: csp = pp.star[:,:-pp.ngas].dot(pp.w_ssps) ###################################################################### # Produce bestfit templates convolved with LOSVD/redshifted best_unbroad = pp.poly + pp.mpoly * losvd_convolve(csp, np.array([sol[0], velscale/10.]), velscale) best_broad = pp.poly + pp.mpoly * losvd_convolve(csp, sol, velscale) ################################################################## # Interpolate bestfit templates to obtain linear dispersion b0 = interp1d(pp.w, best_unbroad, kind="linear", fill_value="extrapolate", bounds_error=False) b1 = interp1d(pp.w, best_broad, kind="linear", fill_value="extrapolate", bounds_error=False) sky = interp1d(pp.w, pp.bestsky, kind="linear", fill_value="extrapolate", bounds_error=False) emission = interp1d(pp.w, pp.gas, kind="linear", fill_value="extrapolate", bounds_error=False) best_unbroad = b0(w) best_broad = b1(w) ###################################################################### # Test plot # plt.plot(w, best_unbroad, "-b") # plt.plot(w, best_broad, "-r") # plt.plot(w, galaxy - sky(w), "-k") # plt.show() ####################################################################### # Broadening to Lick system galaxy = lector.broad2lick(w, galaxy - sky(w) - emission(w), obsres(w), vel=sol[0]) best_unbroad = lector.broad2lick(w, best_unbroad, 3.7, vel=sol[0]) best_broad = lector.broad2lick(w, best_broad, 3.7, vel=sol[0]) ################################################################## lick, lickerr = lector.lector(w, galaxy, np.ones_like(w), bands, vel=sol[0]) lick_unb, tmp = lector.lector(w, best_unbroad, np.ones_like(w), bands, vel=sol[0]) lick_br, tmp = lector.lector(w, best_broad, np.ones_like(w), bands, vel=sol[0]) lickc = correct_lick(bands, lick, lick_unb, lick_br) + offset ###################################################################### # Plot to check if corrections make sense if False: fig = plt.figure(1) ax = plt.subplot(111) ax.plot(lick, "ok") ax.plot(lick_unb, "xb") ax.plot(lick_br, "xr") ax.plot(lick - (lick_br - lick_unb), "+k", ms=10) ax.plot(lick * lick_unb / lick_br, "xk", ms=10) ax.plot(lickc - offset, "o", c="none", markersize=10, mec="y") ax.set_xticks(np.arange(25)) ax.set_xlim(-1, 25) labels = np.loadtxt(bands, usecols=(0,), dtype=str).tolist() labels = [x.replace("_", " ") for x in labels] ax.set_xticklabels(labels, rotation=90) plt.show() ###################################################################### # Storing results lickc = ["{0:.5g}".format(x) for x in lickc] line = "".join(["{0:30s}".format(spec)] + \ ["{0:12s}".format(x) for x in lickc]) lickout.append(line) ###################################################################### # Saving to file with open("lick.txt", "w") as f: f.write("\n".join(lickout))
def run_standard_stars(velscale, bands): """ Run lector on standard stars to study instrumental dependencies. """ stars_dir = os.path.join(home, "data/standards") table = os.path.join(tables_dir, "lick_standards.txt") ids = np.loadtxt(table, usecols=(0,), dtype=str).tolist() lick_ref = np.loadtxt(table, usecols=np.arange(1,26)) ref, obsm, obsa = [], [], [] res = hydra_resolution() for night in nights: os.chdir(os.path.join(stars_dir, night)) stars = [x for x in os.listdir(".") if x.endswith(".fits")] for star in stars: ppfile = "logs/{0}".format(star.replace(".fits", "")) if not os.path.exists(ppfile + ".pkl"): continue name = star.split(".")[0].upper() if name not in ids: continue print name idx = ids.index(name) lick_star = lick_ref[idx] pp = ppload("logs/{0}".format(star.replace(".fits", ""))) pp = pPXF(star, velscale, pp) mpoly = np.interp(pp.wtemp, pp.w, pp.mpoly) spec = pf.getdata(star) w = wavelength_array(star, axis=1, extension=0) best_unbroad_v0 = mpoly * pp.star.dot(pp.w_ssps) best_broad_v0 = losvd_convolve(best_unbroad_v0, np.array([0., pp.sol[1]]), velscale) ################################################################## # Interpolate bestfit templates to obtain linear dispersion b0 = interp1d(pp.wtemp, best_unbroad_v0, kind="linear", fill_value="extrapolate", bounds_error=False) b1 = interp1d(pp.wtemp, best_broad_v0, kind="linear", fill_value="extrapolate", bounds_error=False) best_unbroad_v0 = b0(w) best_broad_v0 = b1(w) ################################################################# # Broadening to Lick system spec = lector.broad2lick(w, spec, res(w), vel=pp.sol[0]) best_unbroad_v0 = lector.broad2lick(w, best_unbroad_v0, 3.6, vel=0.) best_broad_v0 = lector.broad2lick(w, best_broad_v0, 3.6, vel=0.) # plt.plot(w, spec, "-k") # plt.plot(w, best_broad_v0, "-r") # plt.show() ################################################################## lick, lickerr = lector.lector(w, spec, np.ones_like(w), bands, vel=pp.sol[0]) lick_unb, tmp = lector.lector(w, best_unbroad_v0, np.ones_like(w), bands, vel=0.) lick_br, tmp = lector.lector(w, best_broad_v0, np.ones_like(w), bands, vel=0.) lickm = multi_corr(lick, lick_unb, lick_br) licka = add_corr(lick, lick_unb, lick_br) ref.append(lick_star) obsm.append(lickm) obsa.append(licka) with open(os.path.join(tables_dir, "stars_lick_val_corr.txt"), "w") as f: np.savetxt(f, np.array(ref)) with open(os.path.join(tables_dir, "stars_lick_obs_mcorr.txt"), "w") as f: np.savetxt(f, np.array(obsm)) with open(os.path.join(tables_dir, "stars_lick_obs_acorr.txt"), "w") as f: np.savetxt(f, np.array(obsa)) return