.. 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_template_spectral.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_template_spectral.py: .. _template-spectral-model: Template spectral model ======================= This model is defined by custom tabular values. The units returned will be the units of the values array provided at initialization. The model will return values interpolated in log-space, returning 0 for energies outside of the limits of the provided energy array. The class implementation follows closely what has been done in `naima.models.TemplateSpectralModel` .. GENERATED FROM PYTHON SOURCE LINES 19-22 Example plot ------------ Here is an example plot of the model: .. GENERATED FROM PYTHON SOURCE LINES 22-40 .. code-block:: Python import numpy as np from astropy import units as u import matplotlib.pyplot as plt from gammapy.modeling.models import ( Models, PowerLawNormSpectralModel, SkyModel, TemplateSpectralModel, ) energy_bounds = [0.1, 1] * u.TeV energy = np.array([1e6, 3e6, 1e7, 3e7]) * u.MeV values = np.array([4.4e-38, 2.0e-38, 8.8e-39, 3.9e-39]) * u.Unit("MeV-1 s-1 cm-2") template = TemplateSpectralModel(energy=energy, values=values) template.plot(energy_bounds) plt.grid(which="both") .. image-sg:: /user-guide/model-gallery/spectral/images/sphx_glr_plot_template_spectral_001.png :alt: plot template spectral :srcset: /user-guide/model-gallery/spectral/images/sphx_glr_plot_template_spectral_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 41-45 Spectral correction Corrections to templates can be applied by multiplication with a normalized spectral model, for example `gammapy.modeling.models.PowerLawNormSpectralModel`. This operation create a new `gammapy.modeling.models.CompoundSpectralModel` .. GENERATED FROM PYTHON SOURCE LINES 45-50 .. code-block:: Python new_model = template * PowerLawNormSpectralModel(norm=2, tilt=0) print(new_model) .. rst-class:: sphx-glr-script-out .. code-block:: none CompoundSpectralModel Component 1 : TemplateSpectralModel Component 2 : PowerLawNormSpectralModel type name value unit error min max frozen is_norm link prior ---- --------- ---------- ---- --------- --- --- ------ ------- ---- ----- norm 2.0000e+00 0.000e+00 nan nan False True tilt 0.0000e+00 0.000e+00 nan nan True False reference 1.0000e+00 TeV 0.000e+00 nan nan True False Operator : mul .. GENERATED FROM PYTHON SOURCE LINES 51-54 YAML representation ------------------- Here is an example YAML file using the model: .. GENERATED FROM PYTHON SOURCE LINES 54-59 .. code-block:: Python model = SkyModel(spectral_model=template, name="template-model") models = Models([model]) print(models.to_yaml()) .. rst-class:: sphx-glr-script-out .. code-block:: none components: - name: template-model type: SkyModel spectral: type: TemplateSpectralModel parameters: [] energy: data: - 1000000.0 - 3000000.0 - 10000000.0 - 30000000.0 unit: MeV values: data: - 4.4e-38 - 2.0e-38 - 8.8e-39 - 3.9e-39 unit: 1 / (cm2 MeV s) metadata: creator: Gammapy 1.3.dev414+ge9b22cdff date: '2024-05-17T14:33:36.443123' origin: null .. _sphx_glr_download_user-guide_model-gallery_spectral_plot_template_spectral.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_template_spectral.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_template_spectral.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_template_spectral.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_