def broad_binned(): import pyfits as pf from run_ppxf import wavelength_array for field in fields: print field os.chdir(os.path.join(data_dir, "combined_{0}".format(field))) filename = "binned_sn70.fits" specs = pf.getdata(filename) w = wavelength_array(filename, extension=0, axis=1) f = get_muse_fwhm() obsres = f(w) new = broad2res(w, specs, obsres) header = pf.getheader(filename) pf.writeto("binned_sn70_res2.95.fits", new, header, clobber=True) return
def check_ppxf(spec, velscale): """ Checking if velocity os star is zero""" templates, logLam2, delta, miles= stellar_templates(velscale) FWHM_dif = np.sqrt(FWHM_tem**2 - FWHM_spec**2) sigma = FWHM_dif/2.355/delta # Sigma difference in pixels star = pf.getdata(spec) star1 = ndimage.gaussian_filter1d(star, sigma) w = wavelength_array(spec) lamRange= np.array([w[0], w[-1]]) galaxy, logLam1, velscale = util.log_rebin(lamRange, star1, \ velscale=velscale) sn = snr(star1) noise = np.ones_like(galaxy) / sn dv = (logLam2[0]-logLam1[0])*c pp = ppxf(templates, galaxy, noise, velscale, [0,50], plot=True, moments=2, degree=5, mdegree=-1, vsyst=dv) plt.show() return
def example(): import os import pyfits as pf from config import data_dir, tables_dir from run_ppxf import wavelength_array os.chdir(data_dir) filename = "hcg22_023_blanco11an2.fits" v = 2.74319141e+03 # km/s bandsfile = os.path.join(tables_dir, "bands.txt") bands = np.loadtxt(bandsfile, usecols=np.arange(2,8)) spec = pf.getdata(filename) noise = np.ones_like(spec) wave = wavelength_array(filename) lick, err = lector(wave, spec, noise, bandsfile, vel=v) print lick ll = Lick(wave, spec, bands, vel=v) print ll.classic return
def set_coords(spec): fig = plt.figure(1) ax = plt.subplot(111) data = pf.getdata(spec) wave = wavelength_array(spec) coords = [wave[0], wave[-1]] plt.cla() ax.plot(wave, data, "-k") for x in coords: ax.axvline(x, ls="--", c="r") plt.pause(0.001) plt.show() accept = raw_input("Accept x0={0[0]}, x1={0[1]}? (Y/n)".format(coords)) if accept.lower().strip() in ["", "y", "ye", "yes"]: return coords x0 = raw_input("New x0: ") x1 = raw_input("New x1: ") coords = [float(x0), float(x1)] return coords
def test_star(): os.chdir(os.path.join(home, "data/star")) specs = ['HD102070_spectra.fits', "HD102070_noflux.fits"] a = np.zeros((2,25)) cs = ["k", "r"] fs = [] for i, spec in enumerate(specs): star = pf.getdata(spec) w = wavelength_array(spec) # plt.plot(w, star/np.nanmedian(star), "-{0}".format(cs[i])) # check_ppxf(spec, velscale) # Velocity of stars is zero flux = lector.broad2lick(w, star, 2.1, vel=0.) sn = snr(flux) noise = np.ones_like(flux) * np.median(flux) / sn lick, lickerrs = lector.lector(w, flux, noise, bands, vel = 0., cols=(0,8,2,3,4,5,6,7)) a[i] = lick fs.append(interp1d(w, star/np.nanmedian(star), bounds_error=False, fill_value=0)) # plt.show() # plt.clf() w = np.linspace(4850, 5780, 1000) p = np.poly1d(np.polyfit(w, fs[0](w) / fs[1](w), 20)) # plt.plot(w, fs[0](w) / fs[1](w), "-k" ) # plt.plot(w, p(w), "-r") # plt.show() cols = ["Index ", "Calib", "Raw", "Sch07", "Delta", "%", "offset"] cols = ["{0:10s}".format(x) for x in cols] sch = data_schiavon07() model_table = os.path.join(tables_dir, \ "models_thomas_2010_metal_extrapolated.dat") lims, ranges = get_model_lims(model_table, factor=0) lims = np.diff(lims).T[0] print "Results for the test on standard star HD 102070" print "".join(cols) for j,index in enumerate(indices): if j < 12 or j>20: continue print "{0:6s}{1:10.2f}{2:10.2f}{6:10.2f}{3:10.2f}{4:10.2f}{5:10.2f}".format(index, \ a[0][j], a[1][j], (a[0][j] - a[1][j]), (a[0][j] - a[1][j])/ a[0][j], offset[j], sch[j]) return w,p
def test_galaxy(wpoly, poly): os.chdir(os.path.join(home, "single1")) specs = ["fin1_n3311cen1_s27.fits", "fin1_n3311cen2_s30.fits", "fin1_n3311inn1_s28.fits", "fin1_n3311inn2_s28.fits"] kfile = "ppxf_results.dat" kspecs = np.loadtxt(kfile, usecols=(0,), dtype=str) v = np.loadtxt(kfile, usecols=(1,)) vs = dict([(x,y) for x,y in zip(kspecs, v)]) araw = np.zeros((len(specs), 25)) aflux = np.zeros_like(araw) for i,spec in enumerate(specs): galaxy = pf.getdata(spec) w = wavelength_array(spec) flux = lector.broad2lick(w, galaxy, 2.1, vel=vs[spec]) sn = snr(flux) noise = np.ones_like(flux) * np.median(flux) / sn lick, lickerrs = lector.lector(w, flux, noise, bands, vel = vs[spec], cols=(0,8,2,3,4,5,6,7)) araw[i] = lick galaxy2 = galaxy * poly(w) flux2 = lector.broad2lick(w, galaxy2, 2.1, vel=0.) lick2, lickerrs2 = lector.lector(w, flux2, noise, bands, vel = vs[spec], cols=(0,8,2,3,4,5,6,7)) aflux[i] = lick2 araw = araw.T aflux = aflux.T for j,index in enumerate(indices): if j < 12 or j>20: continue print indices[j], # print aflux[j], # print araw[j], # print lims[j] print "{0:10.2f}".format(np.median(aflux[j])), print "{0:10.2f}".format(np.median(araw[j])), print "{0:10.2f}".format(np.median((aflux[j] - araw[j])/(aflux[j])))
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")
# -*- coding: utf-8 -*- """ Created on 22/02/16 @author: Carlos Eduardo Barbosa Combine spectra of standard star HD 102070 """ import os import numpy as np import pyfits as pf import matplotlib.pyplot as plt from config import * from run_ppxf import wavelength_array if __name__ == "__main__": os.chdir(os.path.join(home, "data/star")) spec2d = pf.getdata("HD102070_2d.fits") w = wavelength_array("HD102070_2d.fits") star = np.mean(spec2d[1188:1231,:], axis=0) h = pf.getheader("HD102070_2d.fits") h["NAXIS"] = 1 del h["NAXIS2"] pf.writeto("HD102070_noflux.fits", star, h, clobber=True)
filenames = ", ".join(goodlis) output = os.path.join(outdir, obj) if len(goodlis) == 1: shutil.copy(goodlis[0], os.path.join(outdir2, goodlis[0].replace("_hcg_", "_").replace("_h62_", "_"))) continue if os.path.exists(output): continue iraf.scombine(input = filenames, output = output, group = 'all', combine = 'sum', reject="none", weight="none", w1 = 4000., w2=6500.,dw=1.25) ax = plt.subplot(111) for l in lis: w = wavelength_array(l) intens = pf.getdata(l) c = "k" if l in goodlis else "0.5" ax.plot(w, intens, "-", color=c, lw=0.4) w = wavelength_array(output) intens = pf.getdata(output) ax.plot(w, intens, "-r", lw=2) ax.set_xlabel("Wavelength (Angstrom)") ax.set_ylabel("Counts") plt.title(obj.replace("_", "-")) plt.pause(0.1) plt.show(block=0) outfile.savefig() plt.clf() outfile.close()
# -*- coding: utf-8 -*- """ Created on 22/02/16 @author: Carlos Eduardo Barbosa Combine spectra of standard star HD 102070 """ import os import numpy as np import pyfits as pf import matplotlib.pyplot as plt from config import * from run_ppxf import wavelength_array if __name__ == "__main__": os.chdir(os.path.join(home, "data/star")) spec2d = pf.getdata("HD102070_2d.fits") w = wavelength_array("HD102070_2d.fits") star = np.mean(spec2d[1188:1231, :], axis=0) h = pf.getheader("HD102070_2d.fits") h["NAXIS"] = 1 del h["NAXIS2"] pf.writeto("HD102070_noflux.fits", star, h, clobber=True)
def make_map(group): """ Make the plot""" obs, candidates = get_observational_data() masks_labels = { "hcg22": ["HCG 22 Mask 1"], "hcg42": ["HCG 42 Mask 1"], "hcg62": ["HCG 62 Centre", "HCG 62 Outskirt 2", "HCG 62 Outskirt 1"], "hcg90": ["HCG 90 Mask 1", "HCG 90 Mask 2"], "ngc193": ["NGC 193 Mask 1", "NGC 193 Mask 2"], "ngc7619": ["NGC 7619 Mask 1", "NGC 7619 Mask 2"], } ####################################################################### # Display image imgfile = os.path.join(home, "images/{0}/{0}_dss.fits".format(group)) im = pf.getdata(imgfile) ydim, xdim = im.shape wcs = pywcs.WCS(pf.getheader(imgfile)) fig, ax = plt.subplots(figsize=(6, 5.5)) plt.subplots_adjust(left=0.15, right=0.98, top=0.98) ax.minorticks_on() ra_im = wavelength_array(imgfile, axis=1) dec_im = wavelength_array(imgfile, axis=2) plt.imshow( im, origin="bottom", cmap="cubehelix_r", vmin=6000, vmax=8000, extent=[ra_im[0], ra_im[-1], dec_im[0], dec_im[-1]], ) ####################################################################### # Show all observed fibers idx = np.where(obs[1] == group) data = list(set(zip(obs[2][idx], obs[3][idx], obs[5][idx]))) ras, decs, masks = np.array(list(data)).T ras = ras.astype(float) decs = decs.astype(float) colors = ["r", "b", "y"] for j, mask in enumerate(set(masks)): idx = np.where(masks == mask) x, y = wcs.wcs_sky2pix(np.column_stack((ras[idx], decs[idx])), 1).T r = x / xdim * (ra_im[-1] - ra_im[0]) + ra_im[0] d = y / ydim * (dec_im[-1] - dec_im[0]) + dec_im[0] ax.plot(r, d, "o", c="none", ms=10, mec=colors[j], label=masks_labels[group][j]) ax.set_xlabel("RA J2000 (deg)") ax.set_ylabel("DEC J2000 (deg)") ######################################################################### # Indicate candidates idx = np.where(candidates[1] == group) x, y = wcs.wcs_sky2pix(np.column_stack((candidates[2][idx], candidates[3][idx])), 1).T r = x / xdim * (ra_im[-1] - ra_im[0]) + ra_im[0] d = y / ydim * (dec_im[-1] - dec_im[0]) + dec_im[0] ax.plot(r, d, "s", c="none", ms=10, mec="k", label="This work members") ########################################################################## # Plot galaxies from Z&M 98 if group in ["hcg62", "hcg42", "hcg90"]: objs, ra, dec, vel = get_zm98(group) idx = np.where((vel < v0s[group] + 2000.0) & (vel > v0s[group] - 2000)) x, y = wcs.wcs_sky2pix(np.column_stack((ra.degree[idx], dec.degree[idx])), 1).T r = x / xdim * (ra_im[-1] - ra_im[0]) + ra_im[0] d = y / ydim * (dec_im[-1] - dec_im[0]) + dec_im[0] ax.plot(r, d, "^", c="none", ms=10, mec="g", label="ZM98 members") ########################################################################## outdir = os.path.join(home, "plots") output = os.path.join(outdir, "obs_{0}.png".format(group)) ax.set_xlim(ra_im[0], ra_im[-1]) ax.set_ylim(dec_im[0], dec_im[-1]) ax.get_xaxis().get_major_formatter().set_useOffset(False) plt.legend(loc=2, prop={"size": 10}, frameon=True) plt.savefig(output) # plt.show() # plt.close() return
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
def plot(): table = os.path.join(data_dir, "results.tab") data = np.loadtxt(table, usecols=(0,73,74,1,2,3,4,64,65,66,67,68,69,70,71, 72,73), dtype=str) specs = data[:,0] ras = coord.Angle(data[:,1], unit=units.hour) decs = coord.Angle(data[:,2], unit=units.degree) # download_images() reddir = os.path.join(wdir, "red") os.chdir(reddir) filenames = os.listdir(".") for d, ra, dec in zip(data, ras, decs): spec = d[0] print spec ###################################################################### galaxy = "_".join(spec.split("_")[:2]) run = obsrun[spec.replace(".fits", "").split("_")[2]] imgfile = [x for x in filenames if x.startswith(galaxy)][0] img = pf.getdata(imgfile) raim = wavelength_array(imgfile, axis=1) decim = wavelength_array(imgfile, axis=2) wcs = pywcs.WCS(pf.getheader(imgfile)) xsize = np.abs(raim[-1] - raim[0]) * 60 fig = plt.figure(1, figsize=(5.5,5)) ax = plt.subplot(111) ax.minorticks_on() plt.imshow(img, origin="bottom", cmap="cubehelix", extent=[raim[0], raim[-1], decim[0], decim[-1]], aspect="equal") # labels = [str(float(x)/15) for x in ax.get_xticks().tolist()] # xlabels = coord.Angle(labels, unit=units.hour) # ax.set_xticklabels(xlabels) # plt.setp(ax.xaxis.get_majorticklabels(), rotation=45) ax.tick_params(axis='both', which='major', labelsize=9) ax.set_xlabel("RA J2000 (degree)") xlim = ax.get_xlim() ylim = ax.get_ylim() plt.ylabel("DEC J2000 (degree)") xy = lambda r, phi: (r * np.cos(phi), r * np.sin(phi)) xcirc, ycirc = xy(2./60 / 2/60, np.arange(0,6.28,0.1)) plt.plot(raim.mean()+ xcirc, decim.mean() + ycirc, "-y") plt.xlim(raim[0] - 2./60, raim[-1] + 2./60) plt.ylim(decim[0] + 2./60, decim[-1]- 2./60) ax.get_xaxis().get_major_formatter().set_useOffset(False) ax.get_yaxis().get_major_formatter().set_useOffset(False) ####################################################################### # Include text v = round(float(d[3])) dv = round(float(d[4])) vstr = "$V={0:.0f}\pm{1:.0f}$ km/s".format(v,dv) sig = round(float(d[5])) dsig = round(float(d[6])) sigstr = "$\sigma={0:.0f}\pm{1:.0f}$ km/s".format(sig,dsig) prop = ["Age", "[Z/H]", r"[$\alpha$/Fe]"] stpop = [] for i in range(3): val = float(d[7+3*i]) lerr = np.abs(float(d[8+3*i])) uerr = np.abs(float(d[9+3*i])) ndig = np.minimum(np.log10(lerr), np.log10(uerr)) ndig = int(np.ceil(np.abs(ndig))) ndig = ".{0}".format(ndig) valstr = "{4}=${0:{3}f}_{{-{1:{3}f}}}^{{+{2:{3}f}}}$".format(val, lerr, uerr, ndig, prop[i]) stpop.append(valstr) line1 = "{0} ({1})".format(galaxy.replace("_", "\_").upper(), run) line2 = "{0:18s}, {1}".format(vstr, sigstr) plt.figtext(0.20, 0.9, line1, color="w", fontsize=20) plt.figtext(0.20, 0.84, line2, color="w", fontsize=15) plt.figtext(0.20, 0.25, stpop[0]+"Gyr", color="w", fontsize=15) plt.figtext(0.60, 0.18, stpop[1], color="w", fontsize=15) plt.figtext(0.20, 0.18, stpop[2], color="w", fontsize=15) plt.locator_params(nbins=7) ####################################################################### output = os.path.join(plots_dir, "stamps/{0}.png".format( spec.replace(".fits", ""))) plt.subplots_adjust(bottom=0.09, right=0.96, top=0.99, left=0.19) plt.savefig(output) # plt.show() plt.close(1)