def _vega_source(mag=0, x=0, y=0):
    specs = [vega_spectrum(mag)]
    tbl = Table(names=["x", "y", "ref", "weight"],
                data=[[x] * u.arcsec, [y] * u.arcsec, [0], [1]])
    tbl_source = Source(table=tbl, spectra=specs)

    return tbl_source
def test_all_zero_spectra_line_up():
    mag = 0
    vega = src_ts.vega_spectrum(mag)
    ab = src_ts.ab_spectrum(mag)
    st = src_ts.st_spectrum(mag)

    wave = 0.55 * u.um
    assert st(wave).value == approx(vega(wave).value, rel=0.03)
    assert ab(wave).value == approx(vega(wave).value, rel=0.03)
def get_zero_mag_spectrum(system_name="AB"):
    if system_name.lower() in ["vega"]:
        spec = vega_spectrum()
    elif system_name.lower() in ["ab"]:
        spec = ab_spectrum()
    elif system_name.lower() in ["st", "hst"]:
        spec = st_spectrum()

    return spec
    def test_scales_vega_spectrum_to_vega_ab_or_jansky(self):
        spec = src_ts.vega_spectrum()
        vega_185 = ter_utils.scale_spectrum(spec, "Ks", -1.85 * u.mag)
        ab_0 = ter_utils.scale_spectrum(spec, "Ks", 0 * u.ABmag)
        jy_3630 = ter_utils.scale_spectrum(spec, "Ks", 3630 * u.Jy)

        wave = np.linspace(1.8, 2.5, 1000) * u.um
        assert vega_185(wave).value == approx(ab_0(wave).value, rel=1e-2)
        assert vega_185(wave).value == approx(jy_3630(wave).value, rel=1e-2)

        if PLOTS:
            plt.plot(wave, spec(wave), "b")
            plt.plot(wave, vega_185(wave), "r:")
            plt.plot(wave, ab_0(wave), "g--")
            plt.plot(wave, jy_3630(wave), "y-.")
            plt.semilogy()
            plt.show()
Exemple #5
0
    def photons_in_vega_spectrum(self):
        for filter_name in ["J", "H", "Ks", "Lp", "Mp"]:
            vega = vega_spectrum()
            kwargs = {
                "observatory": "Paranal",
                "instrument": "NACO",
                "filter_name": filter_name
            }
            filt = sim.effects.SpanishVOFilterCurve(**kwargs)
            wave = filt.surface.table["wavelength"]
            trans = filt.surface.table["transmission"]
            dwave = 0.5 * (np.r_[[0], np.diff(wave)] +
                           np.r_[np.diff(wave), [0]]) * u.AA

            flux = vega(wave)  # ph/s/cm2/AA
            flux *= trans * dwave  # ph/s
            sum_flux = np.sum(flux.to(u.ph / u.s / u.m**2).value)

            print(f"\nVega spectrum over the {filter_name} band "
                  f"({filter_name}=0mag) has a flux of "
                  f"{int(sum_flux * 1e-6)}e6 ph/s/m2")
def scale_spectrum(spectrum, filter_name, amplitude):
    """
    Scales a SourceSpectrum to a value in a filter

    Parameters
    ----------
    spectrum : synphot.SourceSpectrum

    filter_name : str
        Name of a filter from
        - a local instrument package (available in ``rc.__search_path__``)
        - a generic filter name (see ``ter_curves_utils.FILTER_DEFAULTS``)
        - a spanish-vo filter service reference (e.g. ``"Paranal/HAWKI.Ks"``)

    amplitude : ``astropy.Quantity``, float
        The value that the spectrum should have in the given filter. Acceptable
        astropy quantities are:
        - u.mag : Vega magnitudes
        - u.ABmag : AB magnitudes
        - u.STmag : HST magnitudes
        - u.Jy : Jansky per filter bandpass
        Additionally the ``FLAM`` and ``FNU`` units from ``synphot.units`` can
        be used when passing the quantity for ``amplitude``:

    Returns
    -------
    spectrum : synphot.SourceSpectrum
        Input spectrum scaled to the given amplitude in the given filter

    Examples
    --------
    ::

        >>> from scopesim.source.source_templates import vega_spectrum
        >>> from scopesim.effects.ter_curves_utils as ter_utils
        >>>
        >>> spec = vega_spectrum()
        >>> vega_185 = ter_utils.scale_spectrum(spec, "Ks", -1.85 * u.mag)
        >>> ab_0 = ter_utils.scale_spectrum(spec, "Ks", 0 * u.ABmag)
        >>> jy_3630 = ter_utils.scale_spectrum(spec, "Ks", 3630 * u.Jy)

    """

    if isinstance(amplitude, u.Quantity):
        if amplitude.unit.physical_type == "spectral flux density":
            if amplitude.unit != u.ABmag:
                amplitude = amplitude.to(u.ABmag)
            ref_spec = ab_spectrum(amplitude.value)

        elif amplitude.unit.physical_type == "spectral flux density wav":
            if amplitude.unit != u.STmag:
                amplitude = amplitude.to(u.STmag)
            ref_spec = st_spectrum(amplitude.value)

        elif amplitude.unit == u.mag:
            ref_spec = vega_spectrum(amplitude.value)

        else:
            raise ValueError(f"Units of amplitude must be one of "
                             f"[u.mag, u.ABmag, u.STmag, u.Jy]: {amplitude}")
    else:
        ref_spec = vega_spectrum(amplitude)

    filt = get_filter(filter_name)
    ref_flux = Observation(ref_spec, filt).effstim(flux_unit=PHOTLAM)

    real_flux = Observation(spectrum, filt).effstim(flux_unit=PHOTLAM)
    scale_factor = ref_flux / real_flux
    spectrum *= scale_factor.value

    return spectrum