Exemple #1
0
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))
Exemple #2
0
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))
Exemple #3
0
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