Commit d1eeb716c64e12e4381c87c6cfd2dc3f726ccdd8
1 parent
f871c5c8
Add script to show spectral model
Showing
2 changed files
with
92 additions
and
0 deletions
examples/README
| @@ -59,6 +59,9 @@ pipeline_unbinned_mem.py | @@ -59,6 +59,9 @@ pipeline_unbinned_mem.py | ||
| 59 | show_butterfly.py | 59 | show_butterfly.py |
| 60 | Displays a butterfly generated using the ctbutterfly tool. | 60 | Displays a butterfly generated using the ctbutterfly tool. |
| 61 | 61 | ||
| 62 | +show_model.py | ||
| 63 | + Displays a spectral model. | ||
| 64 | + | ||
| 62 | show_pha.py | 65 | show_pha.py |
| 63 | Displays a spectrum in PHA format. | 66 | Displays a spectrum in PHA format. |
| 64 | 67 |
examples/show_model.py
0 → 100755
| 1 | +#! /usr/bin/env python | ||
| 2 | +# ========================================================================== | ||
| 3 | +# This script shows a model | ||
| 4 | +# | ||
| 5 | +# Copyright (C) 2015 Juergen Knoedlseder | ||
| 6 | +# | ||
| 7 | +# This program is free software: you can redistribute it and/or modify | ||
| 8 | +# it under the terms of the GNU General Public License as published by | ||
| 9 | +# the Free Software Foundation, either version 3 of the License, or | ||
| 10 | +# (at your option) any later version. | ||
| 11 | +# | ||
| 12 | +# This program is distributed in the hope that it will be useful, | ||
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | +# GNU General Public License for more details. | ||
| 16 | +# | ||
| 17 | +# You should have received a copy of the GNU General Public License | ||
| 18 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | +# | ||
| 20 | +# ========================================================================== | ||
| 21 | +import gammalib | ||
| 22 | +import sys | ||
| 23 | +try: | ||
| 24 | + import matplotlib.pyplot as plt | ||
| 25 | +except: | ||
| 26 | + sys.exit("This script needs matplotlib") | ||
| 27 | + | ||
| 28 | + | ||
| 29 | +# ======================= # | ||
| 30 | +# Plot spectral component # | ||
| 31 | +# ======================= # | ||
| 32 | +def plot_spectrum(model, emin=0.01, emax=100.0, enumbins=100): | ||
| 33 | + """ | ||
| 34 | + """ | ||
| 35 | + # Get spectral component | ||
| 36 | + spectrum = model.spectral() | ||
| 37 | + | ||
| 38 | + # Setup energy axis | ||
| 39 | + e_min = gammalib.GEnergy(emin, "TeV") | ||
| 40 | + e_max = gammalib.GEnergy(emax, "TeV") | ||
| 41 | + ebounds = gammalib.GEbounds(enumbins, e_min, e_max) | ||
| 42 | + | ||
| 43 | + # Setup model plot | ||
| 44 | + x = [] | ||
| 45 | + y = [] | ||
| 46 | + for i in range(enumbins): | ||
| 47 | + energy = ebounds.elogmean(i) | ||
| 48 | + x.append(energy.TeV()) | ||
| 49 | + y.append(spectrum.eval(energy, gammalib.GTime())) | ||
| 50 | + | ||
| 51 | + # Show spectrum | ||
| 52 | + plt.figure() | ||
| 53 | + plt.title(model.name()) | ||
| 54 | + plt.loglog() | ||
| 55 | + plt.grid() | ||
| 56 | + plt.loglog(x, y, color='red') | ||
| 57 | + plt.xlabel("Energy [TeV]") | ||
| 58 | + plt.ylabel(r"dN/dE [ph s$^{-1}$ cm$^{-2}$ MeV$^{-1}$]") | ||
| 59 | + plt.show() | ||
| 60 | + | ||
| 61 | + # Return | ||
| 62 | + return | ||
| 63 | + | ||
| 64 | + | ||
| 65 | +# ============= # | ||
| 66 | +# Script entry # | ||
| 67 | +# ============= # | ||
| 68 | +if __name__ == "__main__": | ||
| 69 | + """ | ||
| 70 | + """ | ||
| 71 | + # Check for model filename | ||
| 72 | + if len(sys.argv) < 2: | ||
| 73 | + sys.exit("Usage: show_model.py model.xml [index]") | ||
| 74 | + | ||
| 75 | + # Get optional model index | ||
| 76 | + if len(sys.argv) == 3: | ||
| 77 | + index = int(sys.argv[2]) | ||
| 78 | + else: | ||
| 79 | + index = 0 | ||
| 80 | + | ||
| 81 | + # Read models XML file | ||
| 82 | + filename = sys.argv[1] | ||
| 83 | + models = gammalib.GModels(filename) | ||
| 84 | + | ||
| 85 | + # Extract relevant model | ||
| 86 | + model = models[index] | ||
| 87 | + | ||
| 88 | + # Plot spectrum | ||
| 89 | + plot_spectrum(model) |