示例#1
0
def c60_example():

    file1 = "/HD169454/RED_860/HD169454_w860_redu_20160808_O8.fits"
    # file2 = "/HD54662/RED_860/HD54662_w860_redu_20160221_O8.fits"

    xmin = 9555
    xmax = 9600

    sp1 = EdiblesSpectrum(file1)
    sp1.getSpectrum(xmin=xmin, xmax=xmax)
    sp1.flux = sp1.flux / np.max(sp1.flux)
    plt.plot(sp1.wave, sp1.flux, "k")

    plt.title("HD 169454", fontsize=14)
    plt.xlabel(r"Wavelength ($\AA$)", fontsize=14)
    plt.ylabel("Normalized Flux", fontsize=14)
    plt.xticks(fontsize=12)
    plt.yticks(fontsize=12)

    plt.hlines(1, xmin=9574, xmax=9579, color='r')
    plt.hlines(1, xmin=9578, xmax=9580, color='r', linestyle=':')

    plt.text(9577.0, 1.01, r"C$_{60}^+$", fontsize=12)

    plt.show()
示例#2
0
def testBasicFit(filename="tests/HD170740_w860_redl_20140915_O12.fits"):

    sp1 = EdiblesSpectrum(filename, noDATADIR=True)
    xmin = 7661.5
    xmax = 7669.0

    subset = sp1.getSpectrum(xmin=xmin, xmax=xmax)
    data = (subset["wave"], subset["flux"])

    cont = createCont(data, n_points=4)
    sightline = Sightline(star_name=sp1.target, cont=cont)
    sightline.addSource(source_name="Telluric", b=0.001, d=0.05)
    sightline.addLine(name="tell1", lam_0=7664.5, tau_0=0.6)
    sightline.addLine(name="tell2", lam_0=7666, tau_0=0.6)

    sightline.addSource(source_name="Interstellar", b=0.01, d=0.02)
    sightline.addLine(name="int1", lam_0=7665.2, tau_0=0.2)
    sightline.addLine(name="int2", lam_0=7665.3, tau_0=0.01)

    fit_model = fit(sp1.target,
                    data,
                    sightline.model,
                    breakdown=False,
                    silent=True)

    return fit_model
def testEdiblesSpectrum(filename="tests/HD170740_w860_redl_20140915_O12.fits"):

    sp = EdiblesSpectrum(filename=filename, noDATADIR=True)
    assert isinstance(sp.header, astropy.io.fits.header.Header)
    assert isinstance(sp.target, str)
    assert isinstance(sp.date, str)
    assert isinstance(sp.v_bary, float)

    assert isinstance(sp.df, pandas.core.frame.DataFrame)
    assert isinstance(sp.wave, pandas.core.frame.Series)
    assert isinstance(sp.wave_units, str)
    assert isinstance(sp.bary_wave, pandas.core.frame.Series)
    assert isinstance(sp.flux, pandas.core.frame.Series)
    assert isinstance(sp.flux_units, str)

    subset1 = sp.getSpectrum()
    assert isinstance(subset1, pandas.core.frame.DataFrame)
    assert isinstance(subset1.wave, pandas.core.frame.Series)
    assert isinstance(subset1.flux, pandas.core.frame.Series)
    assert sp.df.equals(subset1)

    xmin = 7660
    xmax = 7680
    subset2 = sp.getSpectrum(xmin=xmin, xmax=xmax)
    assert isinstance(subset2, pandas.core.frame.DataFrame)
    assert isinstance(subset2.wave, pandas.core.frame.Series)
    assert isinstance(subset2.flux, pandas.core.frame.Series)

    assert np.min(subset2.wave) > xmin
    assert np.max(subset2.wave) < xmax
示例#4
0
def coadding():

    file1 = "/HD170740/BLUE_437/HD170740_w437_blue_20140915_O15.fits"
    file2 = "/HD170740/BLUE_437/HD170740_w437_blue_20140916_O15.fits"
    file3 = "/HD170740/BLUE_437/HD170740_w437_blue_20150626_O15.fits"
    file4 = "/HD170740/BLUE_437/HD170740_w437_blue_20160613_O15.fits"
    file5 = "/HD170740/BLUE_437/HD170740_w437_blue_20170705_O15.fits"

    xmin = 4220
    xmax = 4227.5

    sp1 = EdiblesSpectrum(file1)
    sp2 = EdiblesSpectrum(file2)
    sp3 = EdiblesSpectrum(file3)
    sp4 = EdiblesSpectrum(file4)
    sp5 = EdiblesSpectrum(file5)

    observations = [sp1, sp2, sp3, sp4, sp5]

    fig, axs = plt.subplots(1, 2)

    shift = 0
    for sp in observations:
        shift += 0.01
        sp.getSpectrum(xmin=xmin, xmax=xmax)
        sp.interp_flux = sp.interp_flux / np.max(sp.interp_flux)
        axs[0].plot(sp.grid, sp.interp_flux + shift)

    superspectrum = np.ones_like(observations[0].interp_flux)
    for sp in observations:
        superspectrum = superspectrum * sp.interp_flux

    axs[0].plot(observations[0].grid, superspectrum, 'k')
    axs[0].set_xlabel(r"Wavelength ($\AA$)", fontsize=14)
    axs[0].set_title("Geocentric Reference Frame", fontsize=14)

    axs[0].tick_params(axis='both', labelsize=12)
    # axs[0].set_yticklabels(fontsize=12)

    shift = 0
    for sp in observations:
        shift += 0.01
        sp.getSpectrum(xmin=xmin, xmax=xmax)
        sp.interp_bary_flux = sp.interp_bary_flux / np.max(sp.interp_bary_flux)
        axs[1].plot(sp.grid, sp.interp_bary_flux + shift)

    superspectrum = np.ones_like(observations[0].interp_bary_flux)
    for sp in observations:
        superspectrum = superspectrum * sp.interp_bary_flux

    axs[1].plot(observations[0].grid, superspectrum, 'k')
    axs[1].set_xlabel(r"Wavelength ($\AA$)", fontsize=14)
    axs[1].set_title("Barycentric Reference Frame", fontsize=14)

    axs[1].tick_params(axis='both', labelsize=12)
    # axs[0].set_yticklabels(fontsize=12)

    plt.show()
示例#5
0
def data():

    file1 = "/HD170740/RED_860/HD170740_w860_redl_20160613_O12.fits"

    sp1 = EdiblesSpectrum(file1)
    sp1.getSpectrum(xmin=7585, xmax=7680)
    plt.plot(sp1.wave, sp1.flux, "k")

    plt.title("HD 170740", fontsize=14)
    plt.xlabel(r"Wavelength ($\AA$)", fontsize=14)
    plt.ylabel("Flux", fontsize=14)
    plt.xticks(fontsize=12)
    plt.yticks(fontsize=12)
    plt.show()
示例#6
0
def testAdvancedFit(
    filename1="tests/HD170740_w860_redl_20140915_O12.fits",
    filename2="tests/HD148937_w346_blue_20150817_O11.fits",
):

    star_name = "HD170740"
    xmin = 7661.0
    xmax = 7670.0
    sp = EdiblesSpectrum(filename1, noDATADIR=True)
    subset = sp.getSpectrum(xmin, xmax)
    data = (subset["wave"], subset["flux"])

    # Cont parameters
    n_points = 4
    cont = createCont(data, n_points)

    slightline = Sightline(star_name=star_name, cont=cont)

    slightline.addSource(source_name="Telluric", b=1.07, d=0.046)
    slightline.addLine(name="tell_1", lam_0=7664.8, tau_0=0.75)
    slightline.addLine(name="tell_2", lam_0=7666, tau_0=0.75)

    slightline.addSource(source_name="Interstellar", b=1.0, d=0.001)
    slightline.addLine(name="KI_1", lam_0=7665.3, tau_0=0.1)
    slightline.addLine(name="KI_2", lam_0=7665.35, tau_0=0.05)

    fit_model = fit(star_name, data, slightline.model, silent=True)

    star = "HD148937"
    subset2 = EdiblesSpectrum(filename2, noDATADIR=True).getSpectrum(3301.5, 3304)
    data2 = (subset2["wave"], subset2["flux"])

    cont = createCont(data2, n_points=3)
    slightline2 = Sightline(star_name=star, cont=cont)
    slightline2.addSource("Source 1", 1.42631e-07, 0.036356)
    slightline2.addLine("NaI_1", lam_0=3302.46, tau_0=0.06)
    slightline2.addLine("NaI_2", lam_0=3303.1, tau_0=0.03)

    slightline2.dupSource("Source 1", "Source 2", 1.00005)
    slightline2.dupSource("Source 1", "Source 3", 0.99995)

    fit_model2 = fit(star, data2, slightline2.model, silent=True)

    return fit_model, fit_model2
示例#7
0
def testModels(filename="tests/HD170740_w860_redl_20140915_O12.fits"):

    method = 'least_squares'

    sp = EdiblesSpectrum(filename, noDATADIR=True)
    sp.getSpectrum(xmin=7661, xmax=7670)

    n_anchors = 4

    cont_model = ContinuumModel(n_anchors=n_anchors)
    assert len(cont_model.param_names) == n_anchors * 2
    assert cont_model.n_anchors == n_anchors

    with pytest.raises(TypeError):
        assert ContinuumModel()
    with pytest.raises(TypeError):
        assert ContinuumModel(n_anchors=11)

    cont_pars = cont_model.guess(sp.flux, x=sp.wave)
    assert len(cont_pars) == len(cont_model.param_names)

    for name in cont_model.param_names:
        assert cont_pars[name].value is not None

    voigt = VoigtModel(prefix='voigt_')
    assert voigt.prefix == 'voigt_'
    assert len(voigt.param_names) == 4

    voigt_pars = voigt.guess(sp.flux, x=sp.wave)
    assert len(voigt_pars) == len(voigt.param_names)

    result = voigt.fit(data=sp.flux,
                       params=voigt_pars,
                       x=sp.wave,
                       method=method)
    assert len(result.params) == n_anchors

    out = voigt.eval(data=sp.flux, params=result.params, x=sp.wave)
    assert len(out) == len(sp.flux)
示例#8
0
def line_examples():
    file = "/HD170740/RED_860/HD170740_w860_redl_20160613_O6.fits"

    xmin = 7055
    xmax = 7130

    # GEOCENTRIC
    sp1 = EdiblesSpectrum(file)
    sp1.getSpectrum(xmin=xmin, xmax=xmax)
    sp1.flux = sp1.flux / np.max(sp1.flux)
    plt.plot(sp1.wave, sp1.flux, "k")
    plt.title("HD 170740", fontsize=14)
    plt.xlabel(r"Wavelength ($\AA$)", fontsize=14)
    plt.ylabel("Normalized Flux", fontsize=14)
    plt.xticks(fontsize=12)
    plt.yticks(fontsize=12)

    plt.vlines(7065, 0.9, 1, color='r')
    plt.text(7065.2, 0.995, 'He I')
    plt.vlines(7119.5, 0.9, 1, color='r')
    plt.text(7119.7, 0.995, 'DIB')

    plt.show()
示例#9
0
import matplotlib.pyplot as plt

from edibles.sightline import Sightline
from edibles.utils.edibles_spectrum import EdiblesSpectrum

FILE1 = "/HD170740/RED_860/HD170740_w860_redl_20140915_O12.fits"
xmin = 7661.5
xmax = 7669

sp1 = EdiblesSpectrum(FILE1)
sp1.getSpectrum(xmin=7661, xmax=7670)

sightline = Sightline(sp1)

# Add source
sightline.add_source('telluric')

# Add line with auto-guessed params
sightline.add_line(name='line1', source='telluric')

# Add line with user defined params
d = {'d': 0.01, 'tau_0': 0.6, 'lam_0': 7664.8}
sightline.add_line(name='line2', pars=d, source='telluric')

# Add line with undefined source
d = {'d': 0.01, 'tau_0': 0.1, 'lam_0': 7665.2}

sightline.add_line(name='line3', source='interstellar')

# Add line with no source & user defined pars
d = {'d': 0.01, 'tau_0': 0.1, 'lam_0': 7662}
示例#10
0
    pars_list = convert(pars_list)

    xmin = 7661.75
    xmax = 7669
    # Set tau_cutoff (0.4, 0.02, or 0.0)
    tau_cutoff = 0.02

    # Create linelist
    linelist = []
    for pars in pars_list:
        if (xmin < pars["lam_0"]) and (pars["lam_0"] < xmax):
            if pars["tau_0"] > tau_cutoff:
                linelist.append(pars)
    linelist.reverse()
    print(linelist)

    FILE1 = "/HD170740/RED_860/HD170740_w860_redl_20140915_O12.fits"

    from edibles.utils.edibles_spectrum import EdiblesSpectrum
    import matplotlib.pyplot as plt

    sp1 = EdiblesSpectrum(FILE1)
    sp1.getSpectrum(xmin=xmin, xmax=xmax)

    plt.plot(sp1.wave, sp1.flux)

    plt.vlines([line['lam_0'] for line in linelist], 0, 1000)

    plt.show()
示例#11
0
def CreateAverageSpectrum(DIB,
                          Target,
                          save_to_file=False,
                          save_figure=False,
                          verbose=True):
    """Create a DIB profile with an average spectrum for a sightline.

    Args:
        DIB (int):
            Center wavelength of DIB.
        Target (str):
            Sightline of observation. Usually "HD {integer number}"
        save_to_file (bool, optional):
            If True, it saves the resulting profile. Defaults to False.
        save_figure (bool, optional):
            If True, it creates a plot of the resulting profile and saves it.
            This plot contains the SN ratio. Defaults to False.
        verbose (bool, optional):
            If True, print the target date and resulting profile. Defaults to True.

    Returns:
        2darray:
            Resulting average profile in the form of (wavelength, intensity).
    """
    # Get Edibles files of that sightline.
    oracle = EdiblesOracle()
    List = oracle.getFilteredObsList([Target], Wave=DIB, MergedOnly=True)

    # Dataframe to save the data.
    df = pd.DataFrame()

    # Offset for plotting
    offset = 0.05

    # Check if data is found.
    if len(List) == 0:
        print("Sightline files not found!")
        return np.array([[0], [0]])

    # Iterate over found datafiles.
    for file in List:

        # Get Edibles data.
        sp = EdiblesSpectrum(file)

        # Get target observation date and print it.
        target_date = str(sp.datetime.date()).replace('-', '_')
        if verbose:
            print(target_date)

        # Obtain data in wavelength range of interest.
        sp.getSpectrum(xmin=DIB - 4, xmax=DIB + 4)

        # Get wavelenth and flux
        DIB_wavelength = np.asarray(sp.grid.byteswap().newbyteorder(),
                                    dtype='float64')
        DIB_flux = np.asarray(sp.interp_bary_flux.byteswap().newbyteorder(),
                              dtype='float64')
        DIB_flux = DIB_flux / np.max(DIB_flux)

        # Select datapoints to compute SN ratio.
        cont_x1 = np.array(DIB_wavelength[-20:])
        cont_y1 = np.array(DIB_flux[-20:])
        cont_x2 = np.array(DIB_wavelength[:15])
        cont_y2 = np.array(DIB_flux[:15])

        # Get SN ratio
        SN1, Fit1 = Signal_Noise_Calculator(cont_x1, cont_y1)
        SN2, Fit2 = Signal_Noise_Calculator(cont_x2, cont_y2)

        # Save data unvertainty
        Signal_Noise = 0.5 * (SN1 + SN2)
        New_Noise = DIB_flux / Signal_Noise
        uncertainty = np.full(DIB_wavelength.shape,
                              np.mean(New_Noise),
                              dtype=float)

        # Save results to dataframe
        df[target_date + "_data"] = DIB_flux
        df[target_date + "_error"] = uncertainty

        # Add observation to plot.
        if save_figure:
            plt.plot(DIB_wavelength, DIB_flux + offset, label=target_date)
            offset = offset + 0.05

    # List to save stacking results.
    weig_avg = []
    weig_avg_err = []

    # Print all the data obtained.
    if verbose:
        print(df)

    # Compute weighted average.
    for i in range(len(df.index)):
        values = df[df.columns[::2]].iloc[i].values
        error = df[df.columns[1::2]].iloc[i].values
        avg_weig = weighted_average(values, error)
        weig_avg.append(avg_weig[0])
        weig_avg_err.append(avg_weig[1])

    # Normalization
    weig_avg = weig_avg / np.max(weig_avg)

    # Save to dataframe
    df["Weighted_Average"] = weig_avg
    df["Weighted_Average_Error"] = weig_avg_err

    # Add resulting profile to plot.
    if save_figure:
        plt.plot(DIB_wavelength, weig_avg, label='weighted_average')
        plt.legend()
        plt.savefig("AverageSpectra_" + str(DIB) + ".pdf")
        plt.close()

    # Save restul to file.
    if save_to_file:
        final = pd.DataFrame({
            "Wavelength": DIB_wavelength,
            "Flux": df["Weighted_Average"].to_numpy()
        })
        final.to_csv("Data/AverageSpectraData/" + str(DIB) + "/" + Target +
                     "_avg_spectra.csv")

    # Return resulting profile.
    return (DIB_wavelength, df["Weighted_Average"].to_numpy())
示例#12
0
        header = line1 + "\n" + line2
        with open(csv_file, mode="a") as f:
            np.savetxt(f, (x_points, y_points),
                       delimiter=",",
                       header=header,
                       comments="# ")
            f.write("\n")

        if self.verbose > 0:
            print("Appended to file!")
        if self.verbose > 1:
            print("File appended to: " + csv_file)


if __name__ == "__main__":

    sp = EdiblesSpectrum(
        "/HD23466/BLUE_346/HD23466_w346_blue_20180731_O11.fits")
    subset = sp.getSpectrum(xmin=3270, xmax=3305)

    cont = Continuum(sp, method="spline", n_anchors=5, plot=True, verbose=2)

    print("X names: ", cont.model.xnames)
    print("X values: ",
          [cont.result.params[param].value for param in cont.model.xnames])
    print("Y names: ", cont.model.ynames)
    print("Y values: ",
          [cont.result.params[param].value for param in cont.model.ynames])

    # cont.add_to_csv(user="******", comments="These are test points and should not be used.")
示例#13
0
            f.write("# n_anchors=" + str(self.model.n_anchors) + "\n")
            f.write("# datetime=" + str(datetime.now()) + "\n")
            f.write("# user="******"\n")
            f.write("# comments=" + str(comments) + "\n")
            np.savetxt(f, (x_points, y_points), delimiter=",")
            f.write("\n")

        if self.verbose > 0:
            print("Appended to file!")
        if self.verbose > 1:
            print("File appended to: " + csv_file)


if __name__ == "__main__":

    sp = EdiblesSpectrum("//HD164073/RED_564/HD164073_w564_redl_20180830_O22.fits")
    # sp.getSpectrum(xmin=3270, xmax=3305)

    # build a 4 anchor points spline
    cont = Continuum(sp, method="spline", n_anchors=4, plot=False, verbose=2)
    # Guess the model parameters
    params = cont.model.guess(sp.flux, x=sp.wave)
    # Fit the model
    result = cont.model.fit(data=sp.flux, params=params, x=sp.wave)
    # Get the output of the fit model
    out = result.eval(params=result.params, x=sp.wave)
    # Print the result parameters
    print(result.params)
    # Plot
    plt.plot(sp.wave, sp.flux)
    plt.plot(sp.wave, out)
示例#14
0
}

# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# IMPORT DATA

file1 = "/HD170740/RED_860/HD170740_w860_redl_20140915_O12.fits"
file2 = "/HD170740/RED_860/HD170740_w860_redl_20140916_O12.fits"
file3 = "/HD170740/RED_860/HD170740_w860_redl_20150626_O12.fits"
file4 = "/HD170740/RED_860/HD170740_w860_redl_20160613_O12.fits"
file5 = "/HD170740/RED_860/HD170740_w860_redl_20170705_O12.fits"

xmin = 7640
xmax = 7680

sp1 = EdiblesSpectrum(file1)
sp2 = EdiblesSpectrum(file2)
sp3 = EdiblesSpectrum(file3)
sp4 = EdiblesSpectrum(file4)
sp5 = EdiblesSpectrum(file5)
observations = [sp1, sp2, sp3, sp4, sp5]

# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# TELLURIC WAVELENGTH CORRECTION

zoom_xmin = 7661.5
zoom_xmax = 7669

# Shift spectra
observations = telluric_shift(observations,
                              xmin=xmin,
示例#15
0
    if centwave == 437:
        order_number = [32]
    if centwave == 564:
        order_number = [25, 17]
    if centwave == 860:
        order_number = [21, 14]

    wavemin = float(obs['WaveMin'])
    wavemax = float(obs['WaveMax'])
    spectrum_length = wavemax - wavemin
    min_cutoff = spectrum_length * tr_min
    max_cutoff = spectrum_length * tr_max
    wavemin = wavemin + min_cutoff
    wavemax = wavemax - max_cutoff

    sp = EdiblesSpectrum(obs['Filename'])

    idx = np.where(np.logical_and(sp.wave > wavemin, sp.wave < wavemax))
    sp.wave = sp.wave[idx]
    sp.flux = sp.flux[idx]

    # build a 4 anchor points spline
    cont = Continuum(sp, method="spline", n_anchors=4, plot=False, verbose=0)
    # Guess the model parameters
    params = cont.model.guess(sp.flux, x=sp.wave)
    # Fit the model
    result = cont.model.fit(data=sp.flux, params=params, x=sp.wave)
    # Get the output of the fit model
    out = result.eval(params=result.params, x=sp.wave)

    resid = sp.flux - out
示例#16
0
            zorder=10,
            label='Fit params')
plt.scatter(init_x,
            init_y,
            marker='x',
            s=80,
            color='k',
            label='Initial params')
plt.legend()
plt.show()

# #################################################################################
# Example 2
# #################################################################################

sp = EdiblesSpectrum("/HD23466/BLUE_346/HD23466_w346_blue_20180731_O11.fits")

xmin = 3270
xmax = 3305

sp.getSpectrum(xmin=xmin, xmax=xmax)

cont_model = ContinuumModel(n_anchors=4)
cont_pars = cont_model.guess(sp.flux, x=sp.wave)

# ##############################
# Show initial model

out = cont_model.eval(data=sp.flux, params=cont_pars, x=sp.wave)

y_param_names = []
示例#17
0
def testEdiblesSpectrum(filename="tests/HD170740_w860_redl_20140915_O12.fits"):

    # Spectrum information
    sp = EdiblesSpectrum(filename=filename, noDATADIR=True)
    assert isinstance(sp.header, astropy.io.fits.header.Header)
    assert isinstance(sp.target, str)
    assert isinstance(sp.date, str)
    assert isinstance(sp.datetime, datetime.datetime)
    assert isinstance(sp.v_bary, float)
    assert isinstance(sp.wave_units, str)
    assert isinstance(sp.flux_units, str)

    # Raw
    assert isinstance(sp.raw_wave, np.ndarray)
    assert isinstance(sp.raw_bary_wave, np.ndarray)
    assert isinstance(sp.raw_flux, np.ndarray)
    assert len(sp.raw_wave) == len(sp.raw_bary_wave)
    assert len(sp.raw_wave) == len(sp.raw_flux)

    assert isinstance(sp.raw_grid, np.ndarray)
    assert len(sp.raw_grid) == 200443  # print(len(sp.raw_grid))

    assert isinstance(sp.raw_sky_wave, np.ndarray)
    assert isinstance(sp.raw_sky_flux, np.ndarray)
    assert len(sp.raw_sky_wave) == len(sp.raw_sky_flux)

    assert isinstance(sp.wave, np.ndarray)
    assert isinstance(sp.bary_wave, np.ndarray)
    assert isinstance(sp.flux, np.ndarray)

    # getSpectrum
    xmin = 7660
    xmax = 7680
    sp.getSpectrum(xmin=xmin, xmax=xmax)
    assert xmin == sp.xmin
    assert xmax == sp.xmax
    assert isinstance(sp.wave, np.ndarray)
    assert isinstance(sp.flux, np.ndarray)
    assert len(sp.wave) == len(sp.flux)
    assert np.min(sp.wave) > sp.xmin
    assert np.max(sp.wave) < sp.xmax

    assert isinstance(sp.bary_wave, np.ndarray)
    assert isinstance(sp.bary_flux, np.ndarray)
    assert len(sp.bary_wave) == len(sp.bary_flux)
    assert np.min(sp.bary_wave) > sp.xmin
    assert np.max(sp.bary_wave) < sp.xmax

    assert isinstance(sp.grid, np.ndarray)
    assert isinstance(sp.interp_flux, np.ndarray)
    assert isinstance(sp.interp_bary_flux, np.ndarray)
    assert len(sp.grid) == len(sp.interp_flux)
    assert len(sp.grid) == len(sp.interp_bary_flux)
    assert np.min(sp.grid) > sp.xmin
    assert np.max(sp.grid) < sp.xmax

    assert isinstance(sp.sky_wave, np.ndarray)
    assert isinstance(sp.sky_flux, np.ndarray)
    assert len(sp.sky_wave) == len(sp.sky_flux)
    assert np.min(sp.sky_wave) > sp.xmin
    assert np.max(sp.sky_wave) < sp.xmax

    # shift
    zoom_xmin = 7661
    zoom_xmax = 7679
    shift = 0.05
    sp.shift(shift=shift, zoom_xmin=zoom_xmin, zoom_xmax=zoom_xmax)

    assert isinstance(sp.wave, np.ndarray)
    assert isinstance(sp.flux, np.ndarray)
    assert len(sp.wave) == len(sp.flux)
    assert np.min(sp.wave) > sp.xmin
    assert np.max(sp.wave) < sp.xmax

    assert isinstance(sp.bary_wave, np.ndarray)
    assert isinstance(sp.bary_flux, np.ndarray)
    assert len(sp.bary_wave) == len(sp.bary_flux)
    assert np.min(sp.bary_wave) > sp.xmin
    assert np.max(sp.bary_wave) < sp.xmax

    assert isinstance(sp.grid, np.ndarray)
    assert isinstance(sp.interp_flux, np.ndarray)
    assert isinstance(sp.interp_bary_flux, np.ndarray)
    assert len(sp.grid) == len(sp.interp_flux)
    assert len(sp.grid) == len(sp.interp_bary_flux)
    assert np.min(sp.grid) > sp.xmin
    assert np.max(sp.grid) < sp.xmax

    assert isinstance(sp.sky_wave, np.ndarray)
    assert isinstance(sp.sky_flux, np.ndarray)
    assert len(sp.sky_wave) == len(sp.sky_flux)
    assert np.min(sp.sky_wave) > sp.xmin
    assert np.max(sp.sky_wave) < sp.xmax
示例#18
0
    plt.scatter(peak_wavelengths, peak_fluxes, marker='x')
    plt.show()


if __name__ == "__main__":

    file1 = "/HD170740/RED_860/HD170740_w860_redl_20140915_O12.fits"
    file2 = "/HD170740/RED_860/HD170740_w860_redl_20140916_O12.fits"
    file3 = "/HD170740/RED_860/HD170740_w860_redl_20150626_O12.fits"
    file4 = "/HD170740/RED_860/HD170740_w860_redl_20160613_O12.fits"
    file5 = "/HD170740/RED_860/HD170740_w860_redl_20170705_O12.fits"

    xmin = 7661.5
    xmax = 7669.0

    sp1 = EdiblesSpectrum(file1)
    sp1.getSpectrum(xmin, xmax)
    sp2 = EdiblesSpectrum(file2)
    sp2.getSpectrum(xmin, xmax)
    sp3 = EdiblesSpectrum(file3)
    sp3.getSpectrum(xmin, xmax)
    sp4 = EdiblesSpectrum(file4)
    sp4.getSpectrum(xmin, xmax)
    sp5 = EdiblesSpectrum(file5)
    sp5.getSpectrum(xmin, xmax)
    observations = [sp1, sp2, sp3, sp4, sp5]

    observations = correct(observations)

    sightlines = []
    for spec in observations:
示例#19
0
        # Do we have to filter out merged or single-order spectra? Note that if both
        # MergedOnly and OrdersOnly are True, only the Merged spectra will be returned.

        if MergedOnly and OrdersOnly:
            print("ONLY RETURNING MERGED SPECTRA")

        bool_order = self.obslog.Order != "Z"
        if OrdersOnly is True:
            bool_order = self.obslog.Order != "ALL"
        if MergedOnly is True:
            bool_order = self.obslog.Order == "ALL"

        ind = np.where(bool_target_matches & bool_order)
        # print(ind)
        return self.obslog.iloc[ind].Filename


if __name__ == "__main__":
    # print("Main")
    pythia = EdiblesOracle()
    List = pythia.getObsListByWavelength(5000, MergedOnly=True)
    # print(List)
    for filename in List:
        sp = EdiblesSpectrum(filename)
        plt.figure()
        plt.title(filename)
        plt.xlabel("Wavelength (" + r"$\AA$" + ")")
        plt.xlim(5000, 5100)
        plt.plot(sp.wave, sp.flux)
        plt.show()
示例#20
0
            i_flux = f(i_wave)
            spec.wave = i_wave
            spec.flux = i_flux

    return data


if __name__ == "__main__":

    FILE1 = "/HD170740/RED_860/HD170740_w860_redl_20140915_O12.fits"
    FILE2 = "/HD170740/RED_860/HD170740_w860_redl_20140916_O12.fits"
    FILE3 = "/HD170740/RED_860/HD170740_w860_redl_20150626_O12.fits"
    FILE4 = "/HD170740/RED_860/HD170740_w860_redl_20160613_O12.fits"
    FILE5 = "/HD170740/RED_860/HD170740_w860_redl_20170705_O12.fits"

    sp1 = EdiblesSpectrum(FILE1)
    subset1 = sp1.getSpectrum(xmin=7661.5, xmax=7669)

    sp2 = EdiblesSpectrum(FILE2)
    subset2 = sp2.getSpectrum(xmin=7661.5, xmax=7669)

    sp3 = EdiblesSpectrum(FILE3)
    subset3 = sp3.getSpectrum(xmin=7661.5, xmax=7669)

    sp4 = EdiblesSpectrum(FILE4)
    subset4 = sp4.getSpectrum(xmin=7661.5, xmax=7669)

    sp5 = EdiblesSpectrum(FILE5)
    subset5 = sp5.getSpectrum(xmin=7661.5, xmax=7669)

    data = [sp1, sp2, sp3, sp4, sp5]
示例#21
0
def telluric_shift_plots():

    file1 = "/HD170740/RED_860/HD170740_w860_redl_20140915_O12.fits"
    file2 = "/HD170740/RED_860/HD170740_w860_redl_20140916_O12.fits"
    file3 = "/HD170740/RED_860/HD170740_w860_redl_20150626_O12.fits"
    file4 = "/HD170740/RED_860/HD170740_w860_redl_20160613_O12.fits"
    file5 = "/HD170740/RED_860/HD170740_w860_redl_20170705_O12.fits"

    xmin = 7640
    xmax = 7680
    zoom_xmin = 7661.5
    zoom_xmax = 7669

    sp1 = EdiblesSpectrum(file1)
    sp2 = EdiblesSpectrum(file2)
    sp3 = EdiblesSpectrum(file3)
    sp4 = EdiblesSpectrum(file4)
    sp5 = EdiblesSpectrum(file5)
    observations = [sp1, sp2, sp3, sp4, sp5]

    pars_list = convert(read_hitran("telluric_lines_HITRAN.txt",
                                    molecule='O2'))

    linevals = []
    for pars in pars_list:
        if (zoom_xmin < pars["lam_0"]) and (pars["lam_0"] < zoom_xmax):
            if pars['tau_0'] > 0.4:
                linevals.append(pars["lam_0"])

    fig, axs = plt.subplots(1, 2)
    for sp in observations:
        sp.getSpectrum(xmin=zoom_xmin, xmax=zoom_xmax)
        axs[0].plot(sp.wave,
                    sp.flux / np.max(sp.flux),
                    label=sp.datetime.date())

    axs[0].scatter(linevals,
                   np.zeros_like(linevals),
                   c='k',
                   label='HITRAN data')
    axs[0].legend()
    axs[0].set_xlabel(r'Wavelength ($\AA$)', fontsize=14)
    axs[0].set_ylabel('Normalized Flux', fontsize=14)
    axs[0].tick_params(axis='both', labelsize=12)

    sp1 = EdiblesSpectrum(file1)
    sp2 = EdiblesSpectrum(file2)
    sp3 = EdiblesSpectrum(file3)
    sp4 = EdiblesSpectrum(file4)
    sp5 = EdiblesSpectrum(file5)
    observations = [sp1, sp2, sp3, sp4, sp5]

    observations = telluric_shift(observations,
                                  xmin=xmin,
                                  xmax=xmax,
                                  zoom_xmin=zoom_xmin,
                                  zoom_xmax=zoom_xmax,
                                  molecule='O2',
                                  plot=False)

    for sp in observations:
        axs[1].plot(sp.wave,
                    sp.flux / np.max(sp.flux),
                    label=sp.datetime.date())

    axs[1].scatter(linevals,
                   np.zeros_like(linevals),
                   c='k',
                   label='HITRAN data')
    axs[1].legend()
    axs[1].set_xlabel(r'Wavelength ($\AA$)', fontsize=14)
    axs[1].set_ylabel('Normalized Flux', fontsize=14)
    axs[1].tick_params(axis='both', labelsize=12)

    plt.show()
def createObsList(dryrun=True):
    """Set up of the list of FITS files in the data directory with the necessary
    information that we need to supply to the oracle to work. Essentially, we
    will recursively list all of the FITS files first, then open each of them
    to read the header and extract the information. Finally, store everything
    in a text file.

    :param dryrun: If True, dont save list. Default=True
    :type drtyrun: bool


    """

    print("Data Release in " + DATADIR)
    allfitsfiles = []
    for path, dirs, files in os.walk(DATADIR):
        for file in files:
            if file.endswith(".fits"):
                fullfilename = os.path.join(path, file)
                relative_filename = fullfilename[len(DATADIR):]
                # print(relative_filename)
                allfitsfiles.append(relative_filename)

    print(len(allfitsfiles))

    class obsfile:
        def __init__(self):
            self.filename = ""
            self.object = ""
            self.date_obs = ""
            self.setting = ""
            self.order = ""
            self.wave_min = ""
            self.wave_max = ""
            self.ra = ""
            self.dec = ""
            self.exptime = ""
            # self.order=0
            # self.merged=False

    n_files = len(allfitsfiles)
    full_list = [obsfile() for i in range(n_files)]

    for count in range(len(allfitsfiles)):
        print(count, n_files, allfitsfiles[count])
        full_list[count].filename = allfitsfiles[count]
        # print(DATADIR + full_list[count].filename)
        spec = EdiblesSpectrum(full_list[count].filename)
        # print(spec.header)
        full_list[count].object = spec.header["OBJECT"]
        full_list[count].date_obs = spec.header["DATE-OBS"]
        full_list[count].ra = spec.header["RA"]
        full_list[count].dec = spec.header["DEC"]
        full_list[count].exptime = spec.header["EXPTIME"]
        idx_O = allfitsfiles[count].find("_O")
        if idx_O != -1:
            # print(idx)
            idx_dot = allfitsfiles[count].find(".")
            full_list[count].order = (allfitsfiles[count])[idx_O + 2: idx_dot]
        else:
            full_list[count].order = "ALL"
        if "HIERARCH ESO INS GRAT1 WLEN" in spec.header:
            full_list[count].setting = int(spec.header["HIERARCH ESO INS GRAT1 WLEN"])
        if "HIERARCH ESO INS GRAT2 WLEN" in spec.header:
            full_list[count].setting = int(spec.header["HIERARCH ESO INS GRAT2 WLEN"])
        struct = spec.getSpectrum()
        wave = struct["wave"]
        full_list[count].wave_min = "{:.1f}".format(np.min(wave))
        full_list[count].wave_max = "{:.1f}".format(np.max(wave))
        del spec

    # Create arrays of formatted strings to print to a csv file now.
    pstrings = [
        [
            "Object",
            "RA",
            "DEC",
            "DateObs",
            "Setting",
            "Order",
            "WaveMin",
            "WaveMax",
            "Filename",
        ]
    ]
    for count in range(n_files):
        pstrings.append(
            [
                full_list[count].object,
                full_list[count].ra,
                full_list[count].dec,
                full_list[count].date_obs,
                full_list[count].setting,
                full_list[count].order,
                full_list[count].wave_min,
                full_list[count].wave_max,
                full_list[count].filename,
            ]
        )

    # Time to print things out! Let's use csv format to do that.
    outfile = PYTHONDIR + "/edibles/data/" + DATARELEASE + "_ObsLog.csv"
    # length_checker = np.vectorize(len)
    # all_lengths = length_checker(allfitsfiles)
    # print(np.max(all_lengths))

    if dryrun is False:
        with open(outfile, "w") as csvFile:
            writer = csv.writer(csvFile)
            writer.writerows(pstrings)
            print('Wrote to file!')

    return
示例#23
0
        for i, coef in enumerate(y_anchors[::1]):
            pars['%sy_%i' % (self.prefix, i)].set(value=coef,
                                                  min=0,
                                                  max=2 * np.max(data))

        return update_param_vals(pars, self.prefix, **kwargs)


if __name__ == "__main__":
    import matplotlib.pyplot as plt

    from edibles.utils.edibles_spectrum import EdiblesSpectrum

    filename = "/HD170740/RED_860/HD170740_w860_redl_20140915_O12.fits"
    sp = EdiblesSpectrum(filename)
    print(sp.target)
    sp.getSpectrum(xmin=7661, xmax=7670)
    # sp.flux = sp.flux / np.median(sp.flux)

    cont_model = ContinuumModel(n_anchors=4)
    cont_pars = cont_model.guess(sp.flux, x=sp.wave)

    result = cont_model.fit(data=sp.flux, params=cont_pars, x=sp.wave)
    result.plot_fit()
    plt.show()

    voigt_model = VoigtModel()
    voigt_pars = voigt_model.guess(sp.flux, x=sp.wave)

    model = cont_model * voigt_model
示例#24
0
def reference_frames():

    file1 = "/HD170740/RED_860/HD170740_w860_redl_20140915_O12.fits"
    file2 = "/HD170740/RED_860/HD170740_w860_redl_20140916_O12.fits"
    file3 = "/HD170740/RED_860/HD170740_w860_redl_20150626_O12.fits"
    file4 = "/HD170740/RED_860/HD170740_w860_redl_20160613_O12.fits"
    file5 = "/HD170740/RED_860/HD170740_w860_redl_20170705_O12.fits"

    xmin = 7661.5
    xmax = 7669

    # GEOCENTRIC
    sp1 = EdiblesSpectrum(file1)
    sp1.getSpectrum(xmin=xmin, xmax=xmax)
    sp1.flux = sp1.flux / np.max(sp1.flux)
    plt.plot(sp1.wave, sp1.flux, "k")
    plt.text(7661.6, 0.8, sp1.datetime.date(), fontsize=12)

    sp2 = EdiblesSpectrum(file2)
    sp2.getSpectrum(xmin=xmin, xmax=xmax)
    sp2.flux = sp2.flux / np.max(sp2.flux) + 1
    plt.plot(sp2.wave, sp2.flux, "k")
    plt.text(7661.6, 1.8, sp2.datetime.date(), fontsize=12)

    sp3 = EdiblesSpectrum(file3)
    sp3.getSpectrum(xmin=xmin, xmax=xmax)
    sp3.flux = sp3.flux / np.max(sp3.flux) + 2
    plt.plot(sp3.wave, sp3.flux, "k")
    plt.text(7661.6, 2.8, sp3.datetime.date(), fontsize=12)

    sp4 = EdiblesSpectrum(file4)
    sp4.getSpectrum(xmin=xmin, xmax=xmax)
    sp4.flux = sp4.flux / np.max(sp4.flux) + 3
    plt.plot(sp4.wave, sp4.flux, "k")
    plt.text(7661.6, 3.8, sp4.datetime.date(), fontsize=12)

    sp5 = EdiblesSpectrum(file5)
    sp5.getSpectrum(xmin=xmin, xmax=xmax)
    sp5.flux = sp5.flux / np.max(sp5.flux) + 4
    plt.plot(sp5.wave, sp5.flux, "k")
    plt.text(7661.6, 4.8, sp5.datetime.date(), fontsize=12)

    plt.title("HD 170740", fontsize=14)
    plt.xlabel(r"Wavelength ($\AA$)", fontsize=14)
    plt.ylabel("Normalized Flux + Offset", fontsize=14)
    plt.xticks(fontsize=12)
    plt.yticks(fontsize=12)
    plt.show()

    # BARYCENTRIC
    sp1.bary_flux = sp1.bary_flux / np.max(sp1.bary_flux)
    plt.plot(sp1.bary_wave, sp1.bary_flux, "k")
    plt.text(7661.6, 0.8, sp1.datetime.date(), fontsize=12)

    sp2.bary_flux = sp2.bary_flux / np.max(sp2.bary_flux) + 1
    plt.plot(sp2.bary_wave, sp2.bary_flux, "k")
    plt.text(7661.6, 1.8, sp2.datetime.date(), fontsize=12)

    sp3.bary_flux = sp3.bary_flux / np.max(sp3.bary_flux) + 2
    plt.plot(sp3.bary_wave, sp3.bary_flux, "k")
    plt.text(7661.6, 2.8, sp3.datetime.date(), fontsize=12)

    sp4.bary_flux = sp4.bary_flux / np.max(sp4.bary_flux) + 3
    plt.plot(sp4.bary_wave, sp4.bary_flux, "k")
    plt.text(7661.6, 3.8, sp4.datetime.date(), fontsize=12)

    sp5.bary_flux = sp5.bary_flux / np.max(sp5.bary_flux) + 4
    plt.plot(sp5.bary_wave, sp5.bary_flux, "k")
    plt.text(7661.6, 4.8, sp5.datetime.date(), fontsize=12)

    plt.title("HD 170740", fontsize=14)
    plt.xlabel(r"Wavelength ($\AA$)", fontsize=14)
    plt.ylabel("Normalized Flux + Offset", fontsize=14)
    plt.xticks(fontsize=12)
    plt.yticks(fontsize=12)
    plt.show()
示例#25
0
def fit_NaI_Lines(target, date):
    """A function to fit Na I 2S-2P doublet lines - still very basic


    NaI1 = 3302.368 -> from edibles_linelist_atoms
    NaI2 = 3302.978 -> from edibles_linelist_atoms
    NaI_blue_diff = 0.61 -> lab data from https://www.pa.uky.edu/~peter/newpage/

    NaI3 = 5889.951 -> from edibles_linelist_atoms
    NaI4 = 5895.924 -> from edibles_linelist_atoms
    NaI_red_diff = 5.975 -> lab data from https://www.pa.uky.edu/~peter/newpage/

    blue_red_diff = 2587.583 -> NaI3 - NaI1

    """

    wavelength = 3300

    pythia = EdiblesOracle()
    bluelist = pythia.GetObsListByWavelength(wavelength, OrdersOnly=True)

    files = []
    for filename in bluelist:
        if target in filename:
            if date in filename:
                files.append(filename)

    print(files)

    sp = EdiblesSpectrum(files[0])
    print(sp.target)
    sp.getSpectrum(xmin=3300, xmax=3305)

    sigma = np.std(sp.flux)
    prominence = sigma
    peaks, _ = find_peaks(-sp.flux, prominence=prominence)
    peak_wavelengths = [sp.wave[i] for i in peaks]

    # #########################################################################

    cont = ContinuumModel(n_anchors=4)
    cont_pars = cont.guess(sp.flux, x=sp.wave)

    # #########################################################################

    voigt1 = VoigtModel(prefix='v1_')
    # voigt1_pars = voigt1.make_params(lam_0=3302, b=1, d=0.001, tau_0=0.01)
    voigt1_pars = voigt1.guess(sp.flux, x=sp.wave)

    # #########################################################################

    voigt2 = VoigtModel(prefix='v2_')
    voigt2_pars = voigt2.make_params(lam_0=peak_wavelengths[1],
                                     b=1,
                                     d=0.001,
                                     tau_0=0.01)

    voigt2_pars['v2_lam_0'].set(expr='v1_lam_0 + 0.61')
    voigt2_pars['v2_b'].set(expr='v1_b')

    # #########################################################################

    model = cont * voigt1 * voigt2
    pars = cont_pars + voigt1_pars + voigt2_pars

    result = model.fit(data=sp.flux, params=pars, x=sp.wave)

    # #########################################################################

    result.params.pretty_print()
    print('Ratio: ', result.params['v1_tau_0'] / result.params['v2_tau_0'])
    print(result.fit_report())

    plt.subplot(121)
    result.plot_fit()
    plt.title('Na I 3303')

    print()
    print()

    # #########################################################################
    # #########################################################################

    wavelength = 5890

    pythia = EdiblesOracle()
    redlist = pythia.GetObsListByWavelength(wavelength, OrdersOnly=True)

    files = []
    for filename in redlist:
        if target in filename:
            if date in filename:
                files.append(filename)

    print(files)

    sp = EdiblesSpectrum(files[1])
    print(sp.target)
    sp.getSpectrum(xmin=5885, xmax=5900)

    # #########################################################################

    cont = ContinuumModel(n_anchors=4)
    cont_pars = cont.guess(sp.flux, x=sp.wave)

    # #########################################################################

    prominence = (np.max(sp.flux) - np.min(sp.flux)) * 0.5
    peaks, _ = find_peaks(-sp.flux, prominence=prominence)
    peak_wavelengths = [sp.wave[i] for i in peaks]

    voigt3 = VoigtModel(prefix='v3_')
    voigt3_pars = voigt3.make_params(lam_0=peak_wavelengths[0],
                                     b=1,
                                     d=0.001,
                                     tau_0=0.4)
    # voigt3_pars = voigt3.guess(sp.flux, x=sp.wave)

    # #########################################################################

    voigt4 = VoigtModel(prefix='v4_')
    voigt4_pars = voigt4.make_params(lam_0=5896, b=1, d=0.001, tau_0=0.1)

    voigt4_pars['v4_lam_0'].set(expr='v3_lam_0 + 5.975')
    voigt4_pars['v4_b'].set(expr='v3_b')

    # #########################################################################

    model = cont * voigt3 * voigt4
    pars = cont_pars + voigt3_pars + voigt4_pars

    result = model.fit(data=sp.flux, params=pars, x=sp.wave)

    # #########################################################################

    result.params.pretty_print()
    print('Ratio: ', result.params['v3_tau_0'] / result.params['v4_tau_0'])
    print(result.fit_report())

    plt.subplot(122)
    result.plot_fit()
    plt.title('Na I 5890')
    plt.show()
示例#26
0
# # #########################################################################
# # Find files

# %%%%%%%
# GUI!!!!
# %%%%%%%


# #########################################################################
# Create a spectrum from a fits file

file = "/HD170740/RED_860/HD170740_w860_redl_20140915_O12.fits"

print()
sp = EdiblesSpectrum(file)

# print(sp.target)
# print(sp.date)
# print(sp.v_bary)
# print(sp.wave_units)
# print(sp.flux_units)
# print(sp.wave)
# print(sp.bary_wave)
# print(sp.flux)

# ##############################
# Plotting

# plt.plot(sp.wave, sp.flux, label='Geocentric')
# plt.plot(sp.bary_wave, sp.flux, label='Barycentric')