show_sensitivity.py

Knödlseder Jürgen, 09/24/2015 02:49 PM

Download (8.16 KB)

 
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
# Overplot south sensitivities #
32
# ============================ #
33
def south_sensitivities():
34
    """
35
    """
36
    # Set sensitivities
37
    energies_50h = [0.02522, 0.03965, 0.06359, 0.10000, 0.15725, 0.24726,
38
                    0.39654, 0.63594, 1.00000, 1.60372, 2.47265, 3.96543,
39
                    6.23551, 10.0000, 16.0372, 25.2180, 39.6543, 62.3551,
40
                    98.0512, 157.247]
41
    sens_50h =     [-10.84000, -11.41818, -11.73455, -12.07273, -12.29091, -12.47636,
42
                    -12.67273, -12.82545, -12.97818, -13.09818, -13.24000, -13.29455,
43
                    -13.29455, -13.21818, -13.07636, -12.91273, -12.70545, -12.52000,
44
                    -12.32364, -12.07273]
45
    energies_5h  = [0.02522, 0.04044, 0.06359, 0.10000, 0.15725, 0.25218,
46
                    0.39654, 0.63594, 1.00000, 1.60372, 2.47265, 3.96543,
47
                    6.35944, 10.1988, 16.0372, 24.7265, 39.6543, 63.5944,
48
                    100.000, 160.372]
49
    sens_5h  =     [-10.54545, -11.04727, -11.23273, -11.56000, -11.76727, -11.95273,
50
                    -12.10545, -12.23636, -12.36727, -12.45455, -12.49818, -12.47636,
51
                    -12.43273, -12.24727, -12.10545, -11.90909, -11.70182, -11.52727,
52
                    -11.30909, -11.08000]
53
    energies_05h = [0.02522, 0.04044, 0.06486, 0.10199, 0.15725, 0.25218,
54
                    0.40442, 0.63594, 1.00000, 1.60372, 2.57191, 4.04425,
55
                    6.35944, 10.0000, 16.0372, 25.2179, 39.6543, 63.5944,
56
                    100.000, 160.372]
57
    sens_05h =     [-10.02182, -10.53455, -10.69818, -11.01455, -11.21091, -11.35273,
58
                    -11.49454, -11.57091, -11.64727, -11.69091, -11.65818, -11.57091,
59
                    -11.44000, -11.28727, -11.10182, -10.91636, -10.70909, -10.51273,
60
                    -10.32727, -10.08727]
61
    
62
    # Convert sensitivity
63
    flux_50h = [math.pow(10.0,x) for x in sens_50h]
64
    flux_5h  = [math.pow(10.0,x) for x in sens_5h]
65
    flux_05h = [math.pow(10.0,x) for x in sens_05h]
66
    
67
    # Overplot sensitivities
68
    plt.loglog(energies_50h, flux_50h, 'b-.')
69
    plt.loglog(energies_5h,  flux_5h,  'g-.')
70
    plt.loglog(energies_05h, flux_05h, 'r-.')
71
    
72
    # Return
73
    return
74

    
75
# ============================ #
76
# Overplot north sensitivities #
77
# ============================ #
78
def north_sensitivities():
79
    """
80
    """
81
    # Set sensitivities
82
    energies_50h = [0.02517, 0.04032, 0.06460, 0.10148, 0.15943, 0.25542,
83
                    0.40125, 0.64284, 1.02990, 1.68272, 2.49223, 3.91517,
84
                    6.15053, 10.0492, 15.7868, 25.2921, 39.7326, 63.6558,
85
                    100.000, 160.210]
86
    sens_50h =     [-10.64364, -10.99273, -11.40727, -11.80000, -12.00727, -12.29091,
87
                    -12.47636, -12.62909, -12.78182, -12.91273, -12.92364, -12.92364,
88
                    -12.81455, -12.67273, -12.49818, -12.33455, -12.12727, -11.88727,
89
                    -11.59273, -11.32000]
90
    energies_5h =  [0.02567, 0.03954, 0.06334, 0.09951, 0.15943, 0.25045,
91
                    0.39344, 0.64284, 1.00987, 1.58645, 2.54166, 3.91517,
92
                    6.15053, 9.85379, 16.0998, 24.8002, 39.7326, 62.4179,
93
                    100.000, 157.094]
94
    sens_5h =      [-10.14182, -10.66545, -10.86182, -11.25455, -11.46182, -11.72364,
95
                    -11.89818, -11.98545, -12.09455, -12.16000, -12.12727, -12.06182,
96
                    -11.89818, -11.70182, -11.49454, -11.30909, -11.09091, -10.84000,
97
                    -10.54545, -10.28364]
98
    energies_05h = [0.02776, 0.04032, 0.06334, 0.10148, 0.15943, 0.25045,
99
                    0.40125, 0.64284, 0.99023, 1.58645, 2.54166, 3.99282,
100
                    6.27251, 10.0492, 15.7868, 25.2921, 38.9599, 62.4179,
101
                    77.4686]
102
    sens_05h =     [-9.71636, -10.15273, -10.33818, -10.68727, -10.88364, -11.13455,
103
                    -11.25455, -11.30909, -11.33091, -11.29818, -11.22182, -11.11273,
104
                    -10.90545, -10.70909, -10.48000, -10.29455, -10.08727, -9.84727,
105
                    -9.72727]
106

    
107
    # Convert sensitivity
108
    flux_50h = [math.pow(10.0,x) for x in sens_50h]
109
    flux_5h  = [math.pow(10.0,x) for x in sens_5h]
110
    flux_05h = [math.pow(10.0,x) for x in sens_05h]
111
    
112
    # Overplot sensitivities
113
    plt.loglog(energies_50h, flux_50h, 'b-.')
114
    plt.loglog(energies_5h,  flux_5h,  'g-.')
115
    plt.loglog(energies_05h, flux_05h, 'r-.')
116
    
117
    # Return
118
    return
119

    
120

    
121
# ===================== #
122
# Read sensitivity data #
123
# ===================== #
124
def read_sensitivity(filename):
125
    """
126
    Read sensitivity information from CSV file.
127
    """
128
    # Read filename
129
    csv = gammalib.GCsv(filename,',')
130

    
131
    # Create dictionnary
132
    sensitivity = {}
133
    for column in range(csv.ncols()):
134
        name   = csv[0,column]
135
        values = []
136
        for row in range(csv.nrows()-1):
137
            values.append(float(csv[row+1,column]))
138
        sensitivity[name] = values
139

    
140
    # Check where file contains differential or integral sensitivity
141
    mode = "Integral"
142
    if sensitivity.has_key("emax"):
143
        emax_ref = -1.0
144
        for value in sensitivity["emax"]:
145
            if emax_ref < 0.0:
146
                emax_ref = value
147
            elif emax_ref != value:
148
                mode = "Differential"
149
                break
150

    
151
    # Add mode
152
    sensitivity["mode"] = mode
153

    
154
    # Add linear energy values
155
    if mode == "Differential":
156
        if sensitivity.has_key("loge"):
157
            name   = 'energy'
158
            values = []
159
            for value in sensitivity["loge"]:
160
                values.append(math.pow(10.0, value))
161
            sensitivity[name] = values
162
    else:
163
        if sensitivity.has_key("emin"):
164
            name   = 'energy'
165
            values = []
166
            for value in sensitivity["emin"]:
167
                values.append(value)
168
            sensitivity[name] = values
169

    
170
    # Return
171
    return sensitivity
172

    
173

    
174
# ===================== #
175
# Show sensitivity data #
176
# ===================== #
177
def show_sensitivity(sensitivity, filename):
178
    """
179
    Plot sensitivity.
180
    """
181
    # Build title
182
    title = sensitivity['mode'] + ' sensitivity ('+filename+')'
183

    
184
    # Create figure
185
    plt.figure()
186

    
187
    # Set plot attributes
188
    plt.title(title)
189
    plt.loglog()
190
    plt.grid()
191

    
192
    # Show differential sensitivity
193
    plt.plot(sensitivity['energy'], sensitivity['sensitivity\r'], 'ro-')
194

    
195
    # Set labels
196
    plt.xlabel("Energy (TeV)")
197
    if sensitivity['mode'] == "Differential":
198
        plt.ylabel(r"E$\times$ F(E) (erg cm$^{-2}$ s$^{-1}$)")
199
    else:
200
        plt.ylabel(r"E$\times$ F($>$E) (erg cm$^{-2}$ s$^{-1}$)")
201

    
202
    # Overplot sensitivities
203
    south_sensitivities()
204

    
205
    # Show plot
206
    plt.show()
207

    
208
    # Return
209
    return
210

    
211

    
212
# ======================== #
213
# Main routine entry point #
214
# ======================== #
215
if __name__ == '__main__':
216
    """
217
    """
218
    # Print usage information
219
    usage = "Usage: show_sensitivity [filename]"
220
    if len(sys.argv) < 1:
221
        sys.stdout.write(usage+"\n")
222
        sys.exit()
223

    
224
    # Extract parameters
225
    if len(sys.argv) == 1:
226
        filename = 'sensitivity.dat'
227
    else:
228
        filename = sys.argv[1]
229

    
230
    # Read sensitivity data
231
    sensitivity = read_sensitivity(filename)
232

    
233
    # Show sensitivity data
234
    show_sensitivity(sensitivity, filename)