show_sensitivity.py
1 |
#! /usr/bin/env python
|
---|---|
2 |
# ==========================================================================
|
3 |
# This script displays a differential sensitivity plot generated by cssens.
|
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 sys |
22 |
import math |
23 |
import gammalib |
24 |
try:
|
25 |
import matplotlib.pyplot as plt |
26 |
except:
|
27 |
sys.exit("This script needs matplotlib")
|
28 |
|
29 |
|
30 |
# ===================== #
|
31 |
# Read sensitivity data #
|
32 |
# ===================== #
|
33 |
def read_sensitivity(filename): |
34 |
"""
|
35 |
Read sensitivity information from CSV file.
|
36 |
"""
|
37 |
# Read filename
|
38 |
csv = gammalib.GCsv(filename,',')
|
39 |
|
40 |
# Create dictionnary
|
41 |
sensitivity = {} |
42 |
for column in range(csv.ncols()): |
43 |
name = csv[0,column]
|
44 |
values = [] |
45 |
for row in range(csv.nrows()-1): |
46 |
values.append(float(csv[row+1,column])) |
47 |
sensitivity[name] = values |
48 |
|
49 |
# Add linear energy values
|
50 |
if sensitivity.has_key("loge"): |
51 |
name = 'energy'
|
52 |
values = [] |
53 |
for value in sensitivity["loge"]: |
54 |
values.append(math.pow(10.0, value))
|
55 |
sensitivity[name] = values |
56 |
|
57 |
# Return
|
58 |
return sensitivity
|
59 |
|
60 |
|
61 |
# ===================== #
|
62 |
# Show sensitivity data #
|
63 |
# ===================== #
|
64 |
def show_sensitivity(sensitivity, title="Sensitivity"): |
65 |
"""
|
66 |
Plot sensitivity.
|
67 |
"""
|
68 |
# Create figure
|
69 |
plt.figure() |
70 |
|
71 |
# Set plot attributes
|
72 |
plt.title(title) |
73 |
plt.loglog() |
74 |
plt.grid() |
75 |
|
76 |
# Show differential sensitivity
|
77 |
plt.plot(sensitivity['energy'], sensitivity['eflux'], 'ro-') |
78 |
|
79 |
# Set labels
|
80 |
plt.xlabel("Energy (TeV)")
|
81 |
plt.ylabel(r"Sensitivity (erg cm$^{-2}$ s$^{-1}$)")
|
82 |
|
83 |
# Show plot
|
84 |
plt.show() |
85 |
|
86 |
# Return
|
87 |
return
|
88 |
|
89 |
|
90 |
# ======================== #
|
91 |
# Main routine entry point #
|
92 |
# ======================== #
|
93 |
if __name__ == '__main__': |
94 |
"""
|
95 |
"""
|
96 |
# Print usage information
|
97 |
usage = "Usage: show_sensitivity [filename]"
|
98 |
if len(sys.argv) < 1: |
99 |
sys.stdout.write(usage+"\n")
|
100 |
sys.exit() |
101 |
|
102 |
# Extract parameters
|
103 |
if len(sys.argv) == 1: |
104 |
filename = 'sensitivity.dat'
|
105 |
else:
|
106 |
filename = sys.argv[1]
|
107 |
|
108 |
# Set title
|
109 |
title = 'Differential sensitivity ('+filename+')' |
110 |
|
111 |
# Read sensitivity data
|
112 |
sensitivity = read_sensitivity(filename) |
113 |
|
114 |
# Show sensitivity data
|
115 |
show_sensitivity(sensitivity, title=title) |