Exemple #1
0
def plot_spectrum(ax, spec, complist=None, plot_res=True, fwhm=3):
    """ Plots a spectrum in a given axis

    Parameters
    ----------
    ax : axis
        matplotlib axis
    spec: XSpectrum1D
        Spectrum to plot
    complist : list of AbsComponents, optional
        If given, a model is computed and plotted
    plot_res : bool, optional
        Whether to plot residuals, only works if components
        is not None.
    fwhm : int, optional
        FWHM in pixels

    Returns
    -------
    ax : axis
        Axis with the plot.
    """
    # checks
    if not isinstance(spec, XSpectrum1D):
        raise IOError('Input spec must be XSpectrum1D object.')
    if complist is not None:
        if not isinstance(complist[0], AbsComponent):
            raise IOError('components must be a list of AbsComponent objects.')
        plot_model = True
    else:
        plot_model = False

    if plot_model:
        # create a model
        model = lav.voigt_from_components(spec.wavelength, complist, fwhm=fwhm)

    if spec.co_is_set:
        spec.normalize(co=spec.co)
    ax.plot(model.wavelength, spec.flux, '-', color=COLOR_FLUX, lw=1)
    if spec.sig_is_set:
        ax.plot(model.wavelength, spec.sig, '-', color=COLOR_SIG, lw=0.5)
    if plot_model:
        ax.plot(model.wavelength, model.flux, '-', color=COLOR_MODEL, lw=0.5)
        if plot_res:
            residual = spec.flux - model.flux
            ax.plot(spec.wavelength, residual, '.', color=COLOR_RESIDUAL, ms=2)
            ax.plot(spec.wavelength,
                    -1 * spec.sig,
                    '-',
                    drawstyle='steps-mid',
                    color=COLOR_SIG,
                    lw=0.5)
Exemple #2
0
def plot_spectrum(ax, spec, complist=None, plot_res=True, fwhm=3):
    """ Plots a spectrum in a given axis

    Parameters
    ----------
    ax : axis
        matplotlib axis
    spec: XSpectrum1D
        Spectrum to plot
    complist : list of AbsComponents, optional
        If given, a model is computed and plotted
    plot_res : bool, optional
        Whether to plot residuals, only works if components
        is not None.
    fwhm : int, optional
        FWHM in pixels

    Returns
    -------
    ax : axis
        Axis with the plot.
    """
    # checks
    if not isinstance(spec, XSpectrum1D):
        raise IOError('Input spec must be XSpectrum1D object.')
    if complist is not None:
        if not isinstance(complist[0], AbsComponent):
            raise IOError('components must be a list of AbsComponent objects.')
        plot_model = True
    else:
        plot_model = False

    if plot_model:
        # create a model
        model = lav.voigt_from_components(spec.wavelength, complist, fwhm=fwhm)

    if spec.co_is_set:
        spec.normalize(co=spec.co)
    ax.plot(model.wavelength, spec.flux, '-', color=COLOR_FLUX, lw=1)
    if spec.sig_is_set:
        ax.plot(model.wavelength, spec.sig, '-', color=COLOR_SIG, lw=0.5)
    if plot_model:
        ax.plot(model.wavelength, model.flux, '-', color=COLOR_MODEL, lw=0.5)
        if plot_res:
            residual = spec.flux - model.flux
            ax.plot(spec.wavelength, residual, '.', color=COLOR_RESIDUAL, ms=2)
            ax.plot(spec.wavelength, -1*spec.sig, '-', drawstyle='steps-mid', color=COLOR_SIG, lw=0.5)
Exemple #3
0
def plot_vel(ax, spec, iline, z, dvlims, complist=None, fwhm=3, min_ew_blends=0.01*u.AA):

    # first normalize
    if not spec.normed:
        spec.normalize(co=spec.co)

    # first identify good components
    good_comps = []
    good_comps_aux = ltiu.get_components_at_z(complist, z, dvlims)
    for comp in good_comps_aux:
        for aline in comp._abslines:
            if aline.name == iline['name']:
                good_comps += [comp]
                break
    # bad comps will be those unrelated to the given redshift/transition
    bad_comps = []
    for comp in complist:
        if comp not in good_comps:
            bad_comps += [comp]
    # only good comps will have a model
    if len(good_comps) > 0:
        # import pdb; pdb.set_trace()
        model_spec = lav.voigt_from_components(spec.wavelength, good_comps, fwhm=fwhm)
    else:
        model_spec = XSpectrum1D.from_tuple((spec.wavelength, np.ones(spec.npix)))

    # main plot
    velo = ltu.dv_from_z(spec.wavelength/iline['wrest'] - 1. , z)
    ax.plot(velo, spec.flux, drawstyle='steps-mid', color=COLOR_FLUX)
    plot_spec_complist(ax, velo, spec, bad_comps, min_ew = min_ew_blends, color=COLOR_BLEND, lw=2, drawstyle='steps-mid')
    plot_spec_complist(ax, velo, model_spec, good_comps, min_ew = None, color=COLOR_MODEL, lw=1, ls='-')

    if spec.sig_is_set:
        ax.plot(velo, spec.sig, drawstyle='steps-mid', color=COLOR_SIG, lw=0.5)

    # Zero velocity line
    ax.plot([0., 0.], [-1e9, 1e9], ':', color='gray')
    # Unity flux level line
    ax.plot([-1e9, 1e9], [1, 1], ':', color='b', lw=0.5)
    # Zero flux level line
    ax.plot([-1e9, 1e9], [0, 0], '--', color='k', lw=1)
Exemple #4
0
def test_voigt_from_components():
    from linetools.isgm.tests.test_use_abscomp import mk_comp
    wv_array = np.arange(900, 1250, 0.01) * u.AA
    comp1, HIlines = mk_comp('HI', zcomp=0.01, vlim=[-10, 10] * u.km / u.s)
    comp2, HIlines = mk_comp('HI', zcomp=0.05, vlim=[-10, 10] * u.km / u.s)
    model = lav.voigt_from_components(wv_array, [comp1, comp2])
Exemple #5
0
def plot_vel(ax,
             spec,
             iline,
             z,
             dvlims,
             complist=None,
             fwhm=3,
             min_ew_blends=0.01 * u.AA):

    # first normalize
    if not spec.normed:
        spec.normalize(co=spec.co)

    # first identify good components
    good_comps = []
    good_comps_aux = ltiu.get_components_at_z(complist, z, dvlims)
    for comp in good_comps_aux:
        for aline in comp._abslines:
            if aline.name == iline['name']:
                good_comps += [comp]
                break
    # bad comps will be those unrelated to the given redshift/transition
    bad_comps = []
    for comp in complist:
        if comp not in good_comps:
            bad_comps += [comp]
    # only good comps will have a model
    if len(good_comps) > 0:
        # import pdb; pdb.set_trace()
        model_spec = lav.voigt_from_components(spec.wavelength,
                                               good_comps,
                                               fwhm=fwhm)
    else:
        model_spec = XSpectrum1D.from_tuple(
            (spec.wavelength, np.ones(spec.npix)))

    # main plot
    velo = ltu.dv_from_z(spec.wavelength / iline['wrest'] - 1., z)
    ax.plot(velo, spec.flux, drawstyle='steps-mid', color=COLOR_FLUX)
    plot_spec_complist(ax,
                       velo,
                       spec,
                       bad_comps,
                       min_ew=min_ew_blends,
                       color=COLOR_BLEND,
                       lw=2,
                       drawstyle='steps-mid')
    plot_spec_complist(ax,
                       velo,
                       model_spec,
                       good_comps,
                       min_ew=None,
                       color=COLOR_MODEL,
                       lw=1,
                       ls='-')

    if spec.sig_is_set:
        ax.plot(velo, spec.sig, drawstyle='steps-mid', color=COLOR_SIG, lw=0.5)

    # Zero velocity line
    ax.plot([0., 0.], [-1e9, 1e9], ':', color='gray')
    # Unity flux level line
    ax.plot([-1e9, 1e9], [1, 1], ':', color='b', lw=0.5)
    # Zero flux level line
    ax.plot([-1e9, 1e9], [0, 0], '--', color='k', lw=1)
Exemple #6
0
def test_voigt_from_components():
    from linetools.isgm.tests.test_use_abscomp import mk_comp
    wv_array = np.arange(900, 1250, 0.01) * u.AA
    comp1, HIlines = mk_comp('HI', zcomp=0.01, vlim=[-10,10]*u.km/u.s)
    comp2, HIlines = mk_comp('HI', zcomp=0.05, vlim=[-10,10]*u.km/u.s)
    model = lav.voigt_from_components(wv_array, [comp1,comp2])