plt.suptitle(
    "BT-XXX Relative Synthethic Differences\nArtucus-like\nTeff=5200, logg=4.5, Fe/H= 0.0"
)
plt.xlabel("Wavelength(nm)")
plt.ylabel("Flux")
plt.legend()

plt.show()

# In[ ]:

# ARTUCUS 1000nm
artucus_1 = "/home/jneal/Phd/data/artucus/10097-10155_s-obs.fits"
data, hdr = fits.getdata(artucus_1, header=True)
artucus_1 = Spectrum(xaxis=get_wavelength(hdr) / 10, flux=data, header=hdr)
artucus_1 = artucus_1.normalize("linear")

# In[ ]:

#limits = [2100, 2200]
limits = [artucus_1.xaxis[0] - 2, artucus_1.xaxis[-1] + 2]
print(limits)
next_spec.wav_select(*limits)
next_spec = next_spec.normalize("exponential")
dusty_spec.wav_select(*limits)
dusty_spec = dusty_spec.normalize("exponential")
settl_spec.wav_select(*limits)
settl_spec = settl_spec.normalize("exponential")
cond_spec.wav_select(*limits)
cond_spec = cond_spec.normalize("exponential")
aces_spec.wav_select(*limits)
Example #2
0
plt.plot(w_cond, f_cond / max(f_cond), label="BT-COND")
plt.plot(w_aces, f_aces / max(f_aces), label="PHOENIX ACES")
plt.plot(w_dusty_fits, f_dusty_fits / max(f_dusty_fits), label="Dusty fits")
plt.plot(w_next, f_next / max(f_next), label="NEXTGEN")
plt.title("Sun - 5800K")
plt.xlabel("Wavelength(nm)")
plt.ylabel("Flux")
plt.legend()
plt.show()

# In[ ]:

limits = [2116, 2120]
next_spec.wav_select(*limits)
#print(next_spec.xaxis, next_spec.flux)
next_spec = next_spec.normalize("exponential")
dusty_spec.wav_select(*limits)
dusty_spec = dusty_spec.normalize("exponential")
settl_spec.wav_select(*limits)
settl_spec = settl_spec.normalize("exponential")
cond_spec.wav_select(*limits)
cond_spec = cond_spec.normalize("exponential")
aces_spec.wav_select(*limits)
aces_spec = aces_spec.normalize("exponential")
dusty_fits_spec.wav_select(*limits)
dusty_fits_spec = dusty_fits_spec.normalize("exponential")

# In[ ]:

next_spec.plot(label="nextgen")
dusty_spec.plot(label="dusty_spec")
Example #3
0
def compare_spectra(table, params):
    """Plot the min chi2 result against the observations."""
    extractor = DBExtractor(table)
    for ii, chi2_val in enumerate(chi2_names[0:-2]):
        df = extractor.minimum_value_of(chi2_val)
        df = df[["teff_1", "logg_1", "feh_1", "gamma",
                 "teff_2", "logg_2", "feh_2", "rv", chi2_val]]

        params1 = [df["teff_1"].values[0], df["logg_1"].values[0], df["feh_1"].values[0]]
        params2 = [df["teff_2"].values[0], df["logg_2"].values[0], df["feh_2"].values[0]]

        params1 = [float(param1) for param1 in params1]
        params2 = [float(param2) for param2 in params2]

        gamma = df["gamma"].values
        rv = df["rv"].values

        from simulators.iam_module import iam_helper_function
        obs_name, obs_params, output_prefix = iam_helper_function(params["star"], params["obsnum"], ii + 1)

        obs_spec = load_spectrum(obs_name)

        # Mask out bad portion of observed spectra
        # obs_spec = spectrum_masking(obs_spec, params["star"], params["obsnum"], ii + 1)

        # Barycentric correct spectrum
        _obs_spec = barycorr_crires_spectrum(obs_spec, extra_offset=None)
        normalization_limits = [obs_spec.xaxis[0] - 5, obs_spec.xaxis[-1] + 5]
        # models
        # print("params for models", params1, params2)
        mod1 = load_starfish_spectrum(params1, limits=normalization_limits,
                                      hdr=True, normalize=False, area_scale=True,
                                      flux_rescale=True)

        mod2 = load_starfish_spectrum(params2, limits=normalization_limits,
                                      hdr=True, normalize=False, area_scale=True,
                                      flux_rescale=True)

        iam_grid_func = inherent_alpha_model(mod1.xaxis, mod1.flux, mod2.flux,
                                             rvs=rv, gammas=gamma)

        iam_grid_model = iam_grid_func(obs_spec.xaxis).squeeze()
        iam_grid_model_full = iam_grid_func(mod1.xaxis).squeeze()

        model_spec_full = Spectrum(flux=iam_grid_model_full, xaxis=mod1.xaxis)
        model_spec = Spectrum(flux=iam_grid_model, xaxis=obs_spec.xaxis)

        model_spec = model_spec.remove_nans()
        model_spec = model_spec.normalize(method="exponential")
        model_spec_full = model_spec_full.remove_nans()
        model_spec_full = model_spec_full.normalize(method="exponential")

        fig, ax = plt.subplots(1, 1)
        plt.plot(obs_spec.xaxis, obs_spec.flux, label="Observation")
        plt.plot(model_spec.xaxis, model_spec.flux, label="Minimum \chi^2 model")
        plt.plot(model_spec_full.xaxis, model_spec_full.flux, "--", label="Model_full_res")

        plt.legend()

        fig.tight_layout()
        name = "{0}-{1}_{2}_{3}_min_chi2_spectrum_comparison_{4}.png".format(
            params["star"], params["obsnum"], params["chip"], chi2_val, params["suffix"])
        plt.savefig(os.path.join(params["path"], "plots", name))
        plt.close()

        plt.plot(obs_spec.xaxis, obs_spec.flux, label="Observation")
        plt.show()
Example #4
0
def compare_spectra(table, params):
    """Plot the min chi2 result against the observations."""
    extractor = DBExtractor(table)
    gamma_df = extractor.simple_extraction(columns=["gamma"])
    extreme_gammas = [min(gamma_df.gamma.values), max(gamma_df.gamma.values)]
    for ii, chi2_val in enumerate(chi2_names[0:-2]):
        df = extractor.ordered_extraction(
            columns=["teff_1", "logg_1", "feh_1", "gamma", chi2_val],
            order_by=chi2_val,
            limit=1,
            asc=True)

        params1 = [
            df["teff_1"].values[0], df["logg_1"].values[0],
            df["feh_1"].values[0]
        ]

        params1 = [float(param1) for param1 in params1]

        gamma = df["gamma"].values

        obs_name, obs_params, output_prefix = bhm_helper_function(
            params["star"], params["obsnum"], ii + 1)
        print(obs_name)
        obs_spec = load_spectrum(obs_name)

        # Mask out bad portion of observed spectra
        # obs_spec = spectrum_masking(obs_spec, params["star"], params["obsnum"], ii + 1)

        # Barycentric correct spectrum
        _obs_spec = barycorr_crires_spectrum(obs_spec, extra_offset=None)
        normalization_limits = [obs_spec.xaxis[0] - 5, obs_spec.xaxis[-1] + 5]
        # models
        # print("params for models", params1)
        mod1 = load_starfish_spectrum(params1,
                                      limits=normalization_limits,
                                      hdr=True,
                                      normalize=False,
                                      area_scale=True,
                                      flux_rescale=True)

        bhm_grid_func = one_comp_model(mod1.xaxis, mod1.flux, gammas=gamma)
        bhm_upper_gamma = one_comp_model(mod1.xaxis,
                                         mod1.flux,
                                         gammas=extreme_gammas[1])
        bhm_lower_gamma = one_comp_model(mod1.xaxis,
                                         mod1.flux,
                                         gammas=extreme_gammas[0])

        bhm_grid_model = bhm_grid_func(obs_spec.xaxis).squeeze()
        bhm_grid_model_full = bhm_grid_func(mod1.xaxis).squeeze()
        bhm_upper_gamma = bhm_upper_gamma(obs_spec.xaxis).squeeze()
        bhm_lower_gamma = bhm_lower_gamma(obs_spec.xaxis).squeeze()

        model_spec_full = Spectrum(flux=bhm_grid_model_full, xaxis=mod1.xaxis)
        model_spec = Spectrum(flux=bhm_grid_model, xaxis=obs_spec.xaxis)
        bhm_upper_gamma = Spectrum(flux=bhm_upper_gamma, xaxis=obs_spec.xaxis)
        bhm_lower_gamma = Spectrum(flux=bhm_lower_gamma, xaxis=obs_spec.xaxis)

        model_spec = model_spec.remove_nans()
        model_spec = model_spec.normalize(method="exponential")
        model_spec_full = model_spec_full.remove_nans()
        model_spec_full = model_spec_full.normalize(method="exponential")
        bhm_lower_gamma = bhm_lower_gamma.remove_nans()
        bhm_lower_gamma = bhm_lower_gamma.normalize(method="exponential")
        bhm_upper_gamma = bhm_upper_gamma.remove_nans()
        bhm_upper_gamma = bhm_upper_gamma.normalize(method="exponential")

        from mingle.utilities.chisqr import chi_squared
        chisqr = chi_squared(obs_spec.flux, model_spec.flux)

        print("Recomputed chi^2 = {0}".format(chisqr))
        print("Database chi^2 = {0}".format(df[chi2_val]))
        fig, ax = plt.subplots(1, 1, figsize=(15, 8))
        plt.plot(obs_spec.xaxis,
                 obs_spec.flux + 0.01,
                 label="0.05 + Observation, {}".format(obs_name))
        plt.plot(model_spec.xaxis,
                 model_spec.flux,
                 label="Minimum \chi^2 model")
        plt.plot(model_spec_full.xaxis,
                 model_spec_full.flux,
                 "--",
                 label="Model_full_res")
        plt.plot(bhm_lower_gamma.xaxis,
                 bhm_lower_gamma.flux,
                 "-.",
                 label="gamma={}".format(extreme_gammas[0]))
        plt.plot(bhm_upper_gamma.xaxis,
                 bhm_upper_gamma.flux,
                 ":",
                 label="gamma={}".format(extreme_gammas[1]))
        plt.title("bhm spectrum")
        plt.legend()

        fig.tight_layout()
        name = "{0}-{1}_{2}_{3}_bhm_min_chi2_spectrum_comparison_{4}.png".format(
            params["star"], params["obsnum"], params["chip"], chi2_val,
            params["suffix"])
        plt.savefig(os.path.join(params["path"], "plots", name))
        plt.close()

        plt.plot(obs_spec.xaxis, obs_spec.flux, label="Observation")
        plt.show()
Example #5
0
def main(star,
         obsnum,
         teff_1,
         logg_1,
         feh_1,
         teff_2,
         logg_2,
         feh_2,
         gamma,
         rv,
         plot_name=None):
    fig, axis = plt.subplots(2, 2, figsize=(15, 8), squeeze=False)

    for chip, ax in zip(range(1, 5), axis.flatten()):
        # Get observation data
        obs_name, params, output_prefix = iam_helper_function(
            star, obsnum, chip)

        # Load observed spectrum
        obs_spec = load_spectrum(obs_name)

        # Mask out bad portion of observed spectra
        obs_spec = spectrum_masking(obs_spec, star, obsnum, chip)

        # Barycentric correct spectrum
        _obs_spec = barycorr_crires_spectrum(obs_spec, extra_offset=None)

        # Determine Spectrum Errors
        # error_off = False
        # errors = spectrum_error(star, obsnum, chip, error_off=error_off)

        # Create model with given parameters
        host = load_starfish_spectrum([teff_1, logg_1, feh_1],
                                      limits=[2110, 2165],
                                      area_scale=True,
                                      hdr=True)
        if teff_2 is None:
            assert (logg_2 is None) and (feh_2 is None) and (
                rv == 0), "All must be None for bhm case."
            companion = Spectrum(xaxis=host.xaxis,
                                 flux=np.zeros_like(host.flux))
        else:
            companion = load_starfish_spectrum([teff_2, logg_2, feh_2],
                                               limits=[2110, 2165],
                                               area_scale=True,
                                               hdr=True)

        joint_model = inherent_alpha_model(host.xaxis,
                                           host.flux,
                                           companion.flux,
                                           gammas=gamma,
                                           rvs=rv)

        model_spec = Spectrum(xaxis=host.xaxis,
                              flux=joint_model(host.xaxis).squeeze())
        model_spec = model_spec.remove_nans()
        model_spec = model_spec.normalize("exponential")

        # plot
        obs_spec.plot(axis=ax, label="{}-{}".format(star, obsnum))
        model_spec.plot(axis=ax, linestyle="--", label="Chi-squared model")
        ax.set_xlim([obs_spec.xmin() - 0.5, obs_spec.xmax() + 0.5])

        ax.set_title("{} obs {} chip {}".format(star, obsnum, chip))
        ax.legend()
    plt.tight_layout()

    if plot_name is None:
        fig.show()
    else:
        if not (plot_name.endswith(".png") or plot_name.endswith(".pdf")):
            raise ValueError("plot_name does not end with .pdf or .png")
        fig.savefig(plot_name)

    return 0