예제 #1
0
    label = ""
    vals = {}

    for SNRtarget in SNRtargets:
        if curve.min() < SNRtarget and curve.max() > SNRtarget:
            ifn = interp1d(curve, exp_times, kind='linear')
            label += "S/N %.1f: %.2g s " % (SNRtarget, ifn(SNRtarget))
            vals[SNRtarget] = ifn(SNRtarget)
    return label, vals


print "Reading PSFs..."
TTel = float(sys.argv[2])
psfnm = "WebbPSF_WFC"

PSFs = initialize_PSFs(pixel_scales=[22], slice_scales=[22], PSF_source=psfnm)
WFI_args = {
    "PSFs": PSFs,
    "source_dir": wfirst_data_path + "/pixel-level/input/",
    "pixel_scale": 0.11,
    "dark_current": 0.015,
    "IPC": 0.02,
    "TTel": TTel,
    "zodi_fl":
    file_to_fn(wfirst_data_path + "/pixel-level/input/aldering.txt"),
    "bad_pixel_rate": 0.01,
    "waves": arange(6000., 23000.1, 25.)
}

redshifts = [0.4, 0.8, 1.7, 2.0]
phases = [-10, 0]
예제 #2
0
def generate_data(SN_data):
    print "Reading PSFs..."

    PSFs = initialize_PSFs(
        pixel_scales=[10, 15, 30],
        slice_scales=[30, 30, 30],
        PSF_source=SN_data["survey_parameters"]
        ["PSFs"])  # Native in units of 5 mas, so this is 0".075 and 0".15

    args = {
        "pixel_scale":
        SN_data["survey_parameters"]["IFU_pixel_scale"],
        "slice_scale":
        SN_data["survey_parameters"]["IFU_slice_in_pixels"] *
        SN_data["survey_parameters"]["IFU_pixel_scale"],
        "offset_par":
        int(
            around(SN_data["survey_parameters"]["IFU_pixel_scale"] * 0.25 /
                   0.005)),
        "offset_perp":
        0,
        "dark_current":
        opts.IFCdark,
        "mdl":
        'hsiao',
        "PSFs":
        PSFs,
        "IFURfl":
        SN_data["survey_parameters"]["IFU_resolution"],
        "IPC":
        opts.IFCIPC,
        "bad_pixel_rate":
        SN_data["survey_parameters"]["bad_pixel_rate"],
        "min_wave":
        opts.IFCminwave,
        "max_wave":
        opts.IFCmaxwave,
        "read_noise_floor":
        opts.IFCRNfloor,
        "effareafl":
        SN_data["survey_parameters"]["IFU_effective_area"],
        "TTel":
        opts.TTel,
        "source_dir":
        wfirst_path + "/scripts/pixel-level/input/"
    }

    ETC_result = get_spec_with_err(redshift=0.1,
                                   exp_time=1,
                                   show_plots=0,
                                   phase=0,
                                   **args)
    args["waves"] = ETC_result["obs_waves"]
    high_res_obs_waves = arange(ETC_result["obs_waves"].min() - 10.,
                                ETC_result["obs_waves"].max() + 11., 10.)

    if not all(args["waves"] == SN_data["IFC_waves"]):

        for j in range(len(SN_data["SN_observations"])):
            SN_data["SN_observations"][j]["gal_background"] = interp1d(
                SN_data["IFC_waves"],
                SN_data["SN_observations"][j]["gal_background"],
                kind='linear',
                bounds_error=False,
                fill_value=median(
                    SN_data["SN_observations"][j]["gal_background"]))(
                        args["waves"])
        SN_data["IFC_waves"] = args["waves"]

    args["PSFs"] = ETC_result["PSFs"]

    print "Setting up..."

    eigen_fns = get_eigen()

    has_IFS_mask = zeros(len(SN_data["SN_observations"]))

    for i in range(len(SN_data["SN_observations"])):
        if len(SN_data["SN_observations"][i]["IFS_dates"]) > 0:
            phases = [
                (IFS_date - SN_data["SN_table"]["daymaxes"][i]) /
                (1. + SN_data["SN_table"]["redshifts"][i])
                for IFS_date in SN_data["SN_observations"][i]["IFS_dates"]
            ]
            phases = array(phases)
            if any(abs(phases) < 5):
                has_IFS_mask[i] = 1
            else:
                print "IFS observations found, but not with 5 rest-frame days of max!", phases

    has_IFS_inds = where(has_IFS_mask)[0]
    inds_in_order_of_z = argsort(
        array(SN_data["SN_table"]["redshifts"][has_IFS_inds]))
    has_IFS_inds = has_IFS_inds[inds_in_order_of_z]

    assert sum(has_IFS_inds) == sum(where(has_IFS_mask)[0])

    print has_IFS_mask
    print len(has_IFS_mask)

    redshifts = sort(
        unique(array(SN_data["SN_table"]["redshifts"][has_IFS_inds])))
    redshifts = concatenate(([0.05], redshifts))
    print "redshifts ", redshifts

    nred = len(redshifts)

    print "neigen", opts.neigen

    nsyscoeff = 10

    nsys = (
        nsyscoeff +  # Fundamental
        nsyscoeff +  # CRNL
        4)  # MW norm, zeropoint, RV, IG extinction

    params = dict(mw_norm=opts.mwnorm,
                  mw_zeropoint=opts.mwZP,
                  mw_RV_uncertainty=opts.mwRV,
                  ig_extinction=opts.IGext,
                  crnl=opts.crnl,
                  fund_cal=opts.fund)

    crnl_fns = get_sys_coeff_fns(xmin=-3,
                                 xmax=1.,
                                 xscale=1.,
                                 ncoeff=nsyscoeff,
                                 scale=params["crnl"],
                                 xsteps=50)
    fund_cal_fns = get_sys_coeff_fns(xmin=2999.9,
                                     xmax=21000.1,
                                     xscale=5000.,
                                     ncoeff=nsyscoeff,
                                     scale=params["fund_cal"],
                                     xsteps=50)
    if opts.nredcoeff > 2:
        scale_factor_fns = get_sys_coeff_fns(xmin=1. / (1. + 2.0),
                                             xmax=1.,
                                             xscale=0.5,
                                             ncoeff=opts.nredcoeff,
                                             scale=1.0,
                                             xsteps=50)
    elif opts.nredcoeff == 2:
        scale_factor_fns = [lambda x: 1., lambda x: (x - 1.)]
    elif opts.nredcoeff == 1:
        scale_factor_fns = [lambda x: 1.]
    else:
        raise Exception("Other number of nredcoeff! " + str(opts.nredcoeff))

    rest_waves = exp(
        linspace(log(opts.bluewave), log(opts.redwave), opts.nrestlamb))

    spectral_resolution = (log(opts.redwave) -
                           log(opts.bluewave)) / (opts.nrestlamb / 2.)

    rest_waves_pad = concatenate(
        ([rest_waves[0] / (rest_waves[1] / rest_waves[0])], rest_waves,
         [rest_waves[-1] * (rest_waves[-1] / rest_waves[-2])]))
    print "rest_waves_pad", rest_waves_pad

    scale_factors = 1. / (1 + redshifts)

    redshift_coeffs = zeros([nred, opts.nredcoeff], dtype=float64)

    for i in range(opts.nredcoeff):
        redshift_coeffs[:, i] = scale_factor_fns[i](scale_factors)
    plt.plot(redshifts, redshift_coeffs)
    plt.savefig("redshift_coeffs.pdf")
    plt.close()

    zinds = [0] * opts.nnearby
    for ind in has_IFS_inds:
        zinds.append(
            list(redshifts).index(SN_data["SN_table"]["redshifts"][ind]))

    plt.plot(zinds)
    plt.savefig("zinds.pdf")
    plt.close()

    nsne = len(zinds)
    print "nsne ", nsne

    true_EBVs = random.exponential(size=nsne) * 0.1
    redshift_vector = array([redshifts[zind] for zind in zinds])

    true_mags = random.normal(
        size=nsne) * sqrt(opts.gray**2. + 0.055**2. * redshift_vector**2. +
                          0.00217147**2. / redshift_vector**2.)
    true_RVs = random.normal(size=nsne) * 0.31 + 3.1

    rest_mod, NA, NA, NA = get_sncosmo('hsiao',
                                       1.,
                                       rest_waves * 2.,
                                       array([1e4]),
                                       phase=0.)
    rest_mod /= sum(
        rest_mod
    )  # This is just for intializing the model; it's not used anywhere else.

    true_projs = random.normal(size=(nsne, opts.neigen))

    true_fluxes = []
    fluxes = []
    dfluxes = []

    dflux_dsys = zeros((nsne, opts.nrestlamb, nsys), dtype=float64)

    for i in range(nsne):
        this_redshift = redshifts[zinds[i]]

        print "*" * 20 + " this_redshift ", this_redshift

        if this_redshift > 0.1:
            this_rest_lambs = args["waves"] / (1. + this_redshift)
        else:
            this_rest_lambs = exp(arange(log(3200.), log(9000.), 0.005))

        if this_redshift > 0.1:
            # WFIRST
            f_lamb, NA, NA, NA = get_sncosmo('hsiao',
                                             this_redshift,
                                             high_res_obs_waves,
                                             array([1e4]),
                                             phase=0.,
                                             absmag=-19.08 -
                                             3.1 * median(true_EBVs))
            this_eigen = array([
                item(high_res_obs_waves / (1 + this_redshift))
                for item in eigen_fns
            ])
            F99_31, F99_dAdRV = get_F99(high_res_obs_waves /
                                        (1 + this_redshift))

        else:
            # Nearby
            f_lamb, NA, NA, NA = get_sncosmo(
                'hsiao',
                this_redshift,
                this_rest_lambs * (1 + this_redshift),
                array([1e4]),
                phase=0.,
                absmag=-19.08 - 3.1 * median(true_EBVs))
            this_eigen = array([item(this_rest_lambs) for item in eigen_fns])
            F99_31, F99_dAdRV = get_F99(this_rest_lambs)

        f_lamb *= 10**(-0.4 * (true_mags[i] + true_EBVs[i] *
                               ((true_RVs[i] - 3.1) * F99_dAdRV + F99_31) +
                               dot(true_projs[i], this_eigen)))

        IFUR_FN = interpfile(args["source_dir"] + "/" + args["IFURfl"])

        if this_redshift > 0.1:
            at_max_mdl = spectrum_to_matched_resolution(
                high_res_obs_waves,
                f_lamb,
                min_wave=args["min_wave"],
                max_wave=args["max_wave"],
                IFUR=IFUR_FN,
                pixel_scale=args["pixel_scale"])
            args["mdl"] = at_max_mdl

            orig_ind = has_IFS_inds[i - opts.nnearby]

            assert len(SN_data["SN_observations"][orig_ind]["IFS_dates"]) > 0

            total_spec_StoN = 0.

            for j in range(
                    len(SN_data["SN_observations"][orig_ind]["IFS_dates"])):
                phase = (SN_data["SN_observations"][orig_ind]["IFS_dates"][j] -
                         SN_data["SN_table"]["daymaxes"][orig_ind]) / (
                             1. + SN_data["SN_table"]["redshifts"][orig_ind])
                if abs(phase) < 5:
                    print SN_data["SN_observations"][orig_ind]["IFS_dates"][
                        j], SN_data["SN_table"]["daymaxes"][orig_ind], SN_data[
                            "SN_table"]["redshifts"][orig_ind], phase

                    this_ETC_result = get_spec_with_err(
                        redshift=0.,
                        exp_time=SN_data["SN_observations"][orig_ind]
                        ["IFS_exptimes"][j],
                        show_plots=0,
                        gal_flamb=interp1d(
                            SN_data["IFC_waves"],
                            SN_data["SN_observations"][j]["gal_background"],
                            kind='linear'),
                        phase=0,
                        **args)
                    total_spec_StoN += this_ETC_result["spec_S/N"]**2.
                    print "S/N", j, median(this_ETC_result["spec_S/N"])
                    exp_time = SN_data["SN_observations"][orig_ind][
                        "IFS_exptimes"][j]

            total_spec_StoN = sqrt(total_spec_StoN)
            print "Total S/N, no ref", median(total_spec_StoN)

            ETC_result = {
                "f_lamb_SN": at_max_mdl,
                "PSF_wghtd_e_allbutdark":
                this_ETC_result["PSF_wghtd_e_allbutdark"],
                "exp_time": exp_time
            }
            noise_in_SN_active = ETC_result["f_lamb_SN"] / total_spec_StoN

            args[
                "mdl"] = at_max_mdl / 100.  # For the reference, the SN is faint
            phase_ref = (SN_data["SN_observations"][orig_ind]["IFS_dates"][-1]
                         - SN_data["SN_table"]["daymaxes"][orig_ind]) / (
                             1. + SN_data["SN_table"]["redshifts"][orig_ind])
            assert phase_ref > 60, "No reference, or it's not the last observation!"

            ref_SN = 0
            total_refs_found = 0

            for j in range(
                    len(SN_data["SN_observations"][orig_ind]["IFS_dates"])):
                phase = (SN_data["SN_observations"][orig_ind]["IFS_dates"][j] -
                         SN_data["SN_table"]["daymaxes"][orig_ind]) / (
                             1. + SN_data["SN_table"]["redshifts"][orig_ind])
                if phase > 60:
                    ETC_result_ref = get_spec_with_err(
                        redshift=0.,
                        exp_time=SN_data["SN_observations"][orig_ind]
                        ["IFS_exptimes"][j],
                        show_plots=0,
                        phase=0,
                        gal_flamb=interp1d(
                            SN_data["IFC_waves"],
                            SN_data["SN_observations"][j]["gal_background"],
                            kind='linear'),
                        **args)
                    ref_SN += ETC_result_ref["spec_S/N"]**2.
                    total_refs_found += 1

            assert total_refs_found == SN_data["survey_parameters"][
                "number_of_reference_dithers"], "Found %i refs, but number_of_reference_dithers %i %s" % (
                    total_refs_found, SN_data["survey_parameters"]
                    ["number_of_reference_dithers"],
                    str(SN_data["SN_observations"][orig_ind]["IFS_dates"]))

            ref_SN = sqrt(ref_SN)
            noise_in_reference = args["mdl"] / ref_SN

            total_noise = sqrt(noise_in_SN_active**2 + noise_in_reference**2)
            total_spec_StoN = at_max_mdl / total_noise

            ETC_result["spec_S/N"] = total_spec_StoN
            print "Total S/N, with ref", median(total_spec_StoN)

        else:
            resl = median(0.5 * (this_rest_lambs[1:] + this_rest_lambs[:-1]) /
                          (2 * (this_rest_lambs[1:] - this_rest_lambs[:-1])))

            print resl
            print len(f_lamb), len(this_rest_lambs)

            ETC_result = {
                "f_lamb_SN": f_lamb,
                "PSF_wghtd_e_allbutdark": [NaN] * len(f_lamb),
                "spec_S/N":
                ones(len(f_lamb), dtype=float64) * sqrt(resl / 150.) / 0.12,
                "exp_time": 1.
            }

        print "Binning ", len(this_rest_lambs), len(
            ETC_result["f_lamb_SN"]), len(
                ETC_result["spec_S/N"]), len(rest_waves_pad)

        bin_flux, bin_err = bin_vals_fixed_bins(
            this_rest_lambs, ETC_result["f_lamb_SN"] +
            random.normal(size=len(this_rest_lambs)) *
            ETC_result["f_lamb_SN"] / ETC_result["spec_S/N"],
            ETC_result["f_lamb_SN"] / ETC_result["spec_S/N"], rest_waves_pad)
        bin_flux = bin_flux[1:-1]  # Eliminate the padding
        bin_err = bin_err[1:-1]  # Eliminate the padding

        bin_e_per_sec, NA = bin_vals_fixed_bins(
            this_rest_lambs,
            log10(
                array(ETC_result["PSF_wghtd_e_allbutdark"]) /
                ETC_result["exp_time"]),
            ones(len(this_rest_lambs), dtype=float64), rest_waves_pad)
        bin_e_per_sec = bin_e_per_sec[1:-1]

        jacobian, jacobian_names = get_jacobian(rest_waves,
                                                redshifts[zinds[i]], params,
                                                bin_e_per_sec, fund_cal_fns,
                                                crnl_fns)

        inds = where(isnan(jacobian))
        jacobian[inds] = 0

        inds = where(isnan(bin_flux))
        bin_flux[inds] = 0
        bin_err[inds] = max(bin_flux) * 100

        dflux_dsys[i] = jacobian
        fluxes.append(bin_flux)
        dfluxes.append(bin_err)

    fluxes = array(fluxes) * 1e16
    dfluxes = array(dfluxes) * 1e16

    save_img(fluxes, "fluxes.fits")
    save_img(dfluxes, "dfluxes.fits")
    save_img(fluxes / dfluxes, "SNRs.fits")
    save_img(dflux_dsys, "dflux_dsys.fits")

    F99_31, F99_dAdRV = get_F99(rest_waves)

    plt.plot(rest_waves, F99_31)
    plt.plot(rest_waves, F99_dAdRV)
    plt.savefig("F99.pdf")
    plt.close()

    for i in range(nsne):
        ratio = median(
            fluxes[i, opts.nrestlamb / 4:opts.nrestlamb / 2]) / median(
                fluxes[i, opts.nrestlamb / 2:3 * opts.nrestlamb / 4])
        plt.plot(true_EBVs[i], ratio, '.', color='k')
    plt.savefig("EBV_check.pdf")
    plt.close()

    eigen_vecs = array([item(rest_waves) for item in eigen_fns])

    save_img(eigen_vecs, "sampled_eigen_vecs.fits")

    stan_data = dict(nrestlamb=opts.nrestlamb,
                     nsne=nsne,
                     nred=nred,
                     neigen=opts.neigen,
                     nsys=nsys,
                     ncoeff=opts.nredcoeff,
                     fluxes=fluxes,
                     fluxerrs=dfluxes,
                     eigen_vecs=eigen_vecs,
                     dflux_dsys=dflux_dsys,
                     CCM_31=F99_31,
                     CCM_dAdRV=F99_dAdRV,
                     redshift_coeffs=redshift_coeffs,
                     zinds=zinds,
                     gray_variance=0.055**2. * redshift_vector**2. +
                     0.00217147**2. / redshift_vector**2.)

    other_inputs = dict(true_fluxes=true_fluxes,
                        true_projs=true_projs,
                        true_mags=true_mags,
                        true_EBVs=true_EBVs,
                        true_RVs=true_RVs,
                        rest_mod=rest_mod,
                        jacobian_names=jacobian_names,
                        redshifts=redshifts)
    return stan_data, other_inputs
예제 #3
0


spectra, sntypes = get_modjaz_templates()
spectra2, sntypes2 = get_CfA_templates()
spectra += spectra2
sntypes += sntypes2


commands.getoutput("rm -fr simulated_observations")
for sntype in set(sntypes):
    commands.getoutput("mkdir -p simulated_observations/" + sntype)


#[spectra, types] = readcol("selected_spectra.txt", 'aa')
PSFs = initialize_PSFs(pixel_scales = [15, 30], slice_scales = [30, 30], PSF_source = "WebbPSF", path = wfirst_data_path + "/pixel-level/")

args = {"pixel_scale": 0.075, "slice_scale": 0.15, "source_dir": wfirst_data_path + "/pixel-level/input/", "IFURfl": "IFU_R_160720.txt", "min_wave": 4200.,
        "max_wave": 20000., "PSFs": PSFs, "phase": 0, "redshift": 1.0}


first_ETC_run = get_spec_with_err(exp_time = 100., **args)
args["waves"] = first_ETC_run["obs_waves"]

rest_waves = args["waves"]/(1 + args["redshift"])

for spectrum, sntype in zip(spectra, sntypes):
    print spectrum

    [orig_waves, orig_fluxes] = readcol(spectrum, 'ff')
    orig_fluxes /= max(orig_fluxes)
예제 #4
0


spectra, sntypes = get_modjaz_templates()
spectra2, sntypes2 = get_CfA_templates()
spectra += spectra2
sntypes += sntypes2


commands.getoutput("rm -fr simulated_observations")
for sntype in set(sntypes):
    commands.getoutput("mkdir -p simulated_observations/" + sntype)


#[spectra, types] = readcol("selected_spectra.txt", 'aa')
PSFs = initialize_PSFs(pixel_scales = [15, 30], slice_scales = [30, 30], PSF_source = "WebbPSF", path = "../pixel-level/")

args = {"pixel_scale": 0.075, "slice_scale": 0.15, "source_dir": "../pixel-level/input/", "IFURfl": "IFU_R_160720.txt", "min_wave": 4200.,
        "max_wave": 20000., "PSFs": PSFs, "phase": 0, "redshift": 1.0}


first_ETC_run = get_spec_with_err(exp_time = 100., **args)
args["waves"] = first_ETC_run["obs_waves"]

rest_waves = args["waves"]/(1 + args["redshift"])

for spectrum, sntype in zip(spectra, sntypes):
    print spectrum

    [orig_waves, orig_fluxes] = readcol(spectrum, 'ff')
    orig_fluxes /= max(orig_fluxes)