コード例 #1
0
ファイル: mc_lick.py プロジェクト: kadubarbosa/hydramuse
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
コード例 #2
0
ファイル: run_lector.py プロジェクト: kadubarbosa/hydramuse
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
コード例 #3
0
ファイル: run_lector.py プロジェクト: kadubarbosa/hydramuse
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
コード例 #4
0
ファイル: run_lector.py プロジェクト: kadubarbosa/groups
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))
コード例 #5
0
ファイル: run_lector.py プロジェクト: kadubarbosa/groups
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))
コード例 #6
0
ファイル: run_lector.py プロジェクト: kadubarbosa/groups
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