show_spectrum_old.py
1 |
#! /usr/bin/env python
|
---|---|
2 |
# ==========================================================================
|
3 |
# Display spectrum generated by csspec
|
4 |
#
|
5 |
# Copyright (C) 2015-2019 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 sys |
22 |
try:
|
23 |
import matplotlib.pyplot as plt |
24 |
plt.figure() |
25 |
plt.close() |
26 |
except (ImportError, RuntimeError): |
27 |
print('This script needs the "matplotlib" module')
|
28 |
sys.exit() |
29 |
import gammalib |
30 |
import cscripts |
31 |
|
32 |
|
33 |
# ============= #
|
34 |
# Plot spectrum #
|
35 |
# ============= #
|
36 |
def plot_spectrum(filename, plotfile): |
37 |
"""
|
38 |
Plot spectrum
|
39 |
|
40 |
Parameters
|
41 |
----------
|
42 |
filename : str
|
43 |
Name of spectrum FITS file
|
44 |
plotfile : str
|
45 |
Plot file name
|
46 |
"""
|
47 |
# Read spectrum file
|
48 |
fits = gammalib.GFits(filename) |
49 |
table = fits.table(1)
|
50 |
c_energy = table['Energy']
|
51 |
c_ed = table['ed_Energy']
|
52 |
c_eu = table['eu_Energy']
|
53 |
c_flux = table['Flux']
|
54 |
c_eflux = table['e_Flux']
|
55 |
c_ts = table['TS']
|
56 |
c_upper = table['UpperLimit']
|
57 |
|
58 |
# Initialise arrays to be filled
|
59 |
energies = [] |
60 |
flux = [] |
61 |
ed_engs = [] |
62 |
eu_engs = [] |
63 |
e_flux = [] |
64 |
ul_energies = [] |
65 |
ul_ed_engs = [] |
66 |
ul_eu_engs = [] |
67 |
ul_flux = [] |
68 |
|
69 |
# Loop over rows of the file
|
70 |
nrows = table.nrows() |
71 |
for row in range(nrows): |
72 |
|
73 |
# Get Test Statistic, flux and flux error
|
74 |
ts = c_ts.real(row) |
75 |
flx = c_flux.real(row) |
76 |
e_flx = c_eflux.real(row) |
77 |
|
78 |
# If Test Statistic is larger than 9 and flux error is smaller than
|
79 |
# flux then append flux plots ...
|
80 |
if ts > 9.0 and e_flx < flx: |
81 |
energies.append(c_energy.real(row)) |
82 |
flux.append(c_flux.real(row)) |
83 |
ed_engs.append(c_ed.real(row)) |
84 |
eu_engs.append(c_eu.real(row)) |
85 |
e_flux.append(c_eflux.real(row)) |
86 |
|
87 |
# ... otherwise append upper limit
|
88 |
else:
|
89 |
ul_energies.append(c_energy.real(row)) |
90 |
ul_flux.append(c_upper.real(row)) |
91 |
ul_ed_engs.append(c_ed.real(row)) |
92 |
ul_eu_engs.append(c_eu.real(row)) |
93 |
|
94 |
# Set upper limit errors
|
95 |
yerr = [0.6 * x for x in ul_flux] |
96 |
|
97 |
# Plot the spectrum
|
98 |
plt.figure() |
99 |
plt.loglog() |
100 |
plt.grid() |
101 |
plt.errorbar(energies, flux, yerr=e_flux, xerr=[ed_engs, eu_engs], |
102 |
fmt='ro')
|
103 |
plt.errorbar(ul_energies, ul_flux, xerr=[ul_ed_engs, ul_eu_engs], |
104 |
yerr=yerr, uplims=True, fmt='ro') |
105 |
plt.xlabel('Energy (TeV)')
|
106 |
plt.ylabel(r'E$^2$ $\times$ dN/dE (erg cm$^{-2}$ s$^{-1}$)')
|
107 |
|
108 |
# Show figure
|
109 |
if len(plotfile) > 0: |
110 |
plt.savefig(plotfile) |
111 |
else:
|
112 |
plt.show() |
113 |
|
114 |
# Return
|
115 |
return
|
116 |
|
117 |
|
118 |
# ============= #
|
119 |
# Show spectrum #
|
120 |
# ============= #
|
121 |
def show_spectrum(): |
122 |
"""
|
123 |
Show spectrum
|
124 |
"""
|
125 |
# Set usage string
|
126 |
usage = 'show_spectrum.py [-p plotfile] [file]'
|
127 |
|
128 |
# Set default options
|
129 |
options = [{'option': '-p', 'value': ''}] |
130 |
|
131 |
# Get arguments and options from command line arguments
|
132 |
args, options = cscripts.ioutils.get_args_options(options, usage) |
133 |
|
134 |
# Extract script parameters from options
|
135 |
plotfile = options[0]['value'] |
136 |
|
137 |
# Show spectrum
|
138 |
plot_spectrum(args[0], plotfile)
|
139 |
|
140 |
# Return
|
141 |
return
|
142 |
|
143 |
|
144 |
# ======================== #
|
145 |
# Main routine entry point #
|
146 |
# ======================== #
|
147 |
if __name__ == '__main__': |
148 |
|
149 |
# Show spectrum
|
150 |
show_spectrum() |