.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "user-guide/model-gallery/spectral/plot_absorbed.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_user-guide_model-gallery_spectral_plot_absorbed.py: .. _absorption-spectral-model: EBL absorption spectral model ============================= This model evaluates absorbed spectral model. The EBL absorption factor given by .. math:: \exp{ \left ( -\alpha \times \tau(E, z) \right )} where :math:`\tau(E, z)` is the optical depth predicted by the model (`~gammapy.modeling.models.EBLAbsorptionNormSpectralModel`), which depends on the energy of the gamma-rays and the redshift z of the source, and :math:`\alpha` is a scale factor (default: 1) for the optical depth. The available EBL models are defined in `~gammapy.modeling.models.EBL_DATA_BUILTIN`. .. GENERATED FROM PYTHON SOURCE LINES 23-26 Example plot ------------ Here is an example plot of the model: .. GENERATED FROM PYTHON SOURCE LINES 26-97 .. code-block:: Python from astropy import units as u import matplotlib.pyplot as plt from gammapy.modeling.models import ( EBL_DATA_BUILTIN, EBLAbsorptionNormSpectralModel, Models, PowerLawSpectralModel, SkyModel, ) # Print the available EBL models print(EBL_DATA_BUILTIN.keys()) # Here we illustrate how to create and plot EBL absorption models for a redshift of 0.5 # sphinx_gallery_thumbnail_number = 1 redshift = 0.5 dominguez = EBLAbsorptionNormSpectralModel.read_builtin("dominguez", redshift=redshift) franceschini = EBLAbsorptionNormSpectralModel.read_builtin( "franceschini", redshift=redshift ) finke = EBLAbsorptionNormSpectralModel.read_builtin("finke", redshift=redshift) franceschini17 = EBLAbsorptionNormSpectralModel.read_builtin( "franceschini17", redshift=redshift ) saldana21 = EBLAbsorptionNormSpectralModel.read_builtin( "saldana-lopez21", redshift=redshift ) fig, (ax_ebl, ax_model) = plt.subplots( nrows=1, ncols=2, figsize=(10, 4), gridspec_kw={"left": 0.08, "right": 0.96} ) energy_bounds = [0.08, 3] * u.TeV opts = dict(energy_bounds=energy_bounds, xunits=u.TeV) franceschini.plot(ax=ax_ebl, label="Franceschini 2008", **opts) finke.plot(ax=ax_ebl, label="Finke 2010", **opts) dominguez.plot(ax=ax_ebl, label="Dominguez 2011", **opts) franceschini17.plot(ax=ax_ebl, label="Franceschni 2017", **opts) saldana21.plot(ax=ax_ebl, label="Saldana-Lopez 2021", **opts) ax_ebl.set_ylabel(r"Absorption coefficient [$\exp{(-\tau(E))}$]") ax_ebl.set_xlim(energy_bounds.value) ax_ebl.set_ylim(1e-4, 2) ax_ebl.set_title(f"EBL models (z={redshift})") ax_ebl.grid(which="both") ax_ebl.legend(loc="best") # Spectral model corresponding to PKS 2155-304 (quiescent state) index = 3.53 amplitude = 1.81 * 1e-12 * u.Unit("cm-2 s-1 TeV-1") reference = 1 * u.TeV pwl = PowerLawSpectralModel(index=index, amplitude=amplitude, reference=reference) # The power-law model is multiplied by the EBL norm spectral model redshift = 0.117 absorption = EBLAbsorptionNormSpectralModel.read_builtin("dominguez", redshift=redshift) model = pwl * absorption energy_bounds = [0.1, 100] * u.TeV model.plot(energy_bounds, ax=ax_model) ax_model.grid(which="both") ax_model.set_ylim(1e-24, 1e-8) ax_model.set_title("Absorbed Power Law") plt.show() .. image-sg:: /user-guide/model-gallery/spectral/images/sphx_glr_plot_absorbed_001.png :alt: EBL models (z=0.5), Absorbed Power Law :srcset: /user-guide/model-gallery/spectral/images/sphx_glr_plot_absorbed_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none dict_keys(['franceschini', 'dominguez', 'finke', 'franceschini17', 'saldana-lopez21']) .. GENERATED FROM PYTHON SOURCE LINES 98-101 YAML representation ------------------- Here is an example YAML file using the model: .. GENERATED FROM PYTHON SOURCE LINES 101-106 .. code-block:: Python model = SkyModel(spectral_model=model, name="absorbed-model") models = Models([model]) print(models.to_yaml()) .. rst-class:: sphx-glr-script-out .. code-block:: none components: - name: absorbed-model type: SkyModel spectral: type: CompoundSpectralModel model1: type: PowerLawSpectralModel parameters: - name: index value: 3.53 - name: amplitude value: 1.81e-12 unit: cm-2 s-1 TeV-1 - name: reference value: 1.0 unit: TeV model2: type: EBLAbsorptionNormSpectralModel parameters: - name: alpha_norm value: 1.0 - name: redshift value: 0.117 filename: /home/runner/work/gammapy-docs/gammapy-docs/gammapy-datasets/dev/ebl/ebl_dominguez11.fits.gz operator: mul metadata: creator: Gammapy 1.3.dev241+g0271bebfc date: '2024-05-03T15:02:22.539492' origin: null .. _sphx_glr_download_user-guide_model-gallery_spectral_plot_absorbed.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/gammapy/gammapy-webpage/main?urlpath=lab/tree/notebooks/dev/user-guide/model-gallery/spectral/plot_absorbed.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_absorbed.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_absorbed.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_