show_obs.py
6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#! /usr/bin/env python
# ==========================================================================
# Display summary of observation definition XML file
#
# Copyright (C) 2016 Michael Mayer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ==========================================================================
import sys
import gammalib
import cscripts
try:
import matplotlib.pyplot as plt
plt.figure()
plt.close()
except (ImportError, RuntimeError):
print('This script needs the "matplotlib" module')
sys.exit()
# ==================== #
# Run csobsinfo script #
# ==================== #
def run_csobsinfo(filename, ra, dec, debug=True):
"""
Run csobsinfo script
Parameters
----------
filename : str
File name of observation definition XML file
ra : str
Target Right Ascension (deg)
dec : str
Target declination of pointing (deg)
debug : bool, optional
Switch on debugging in csobsinfo run
Returns
-------
info : `~cscripts.csobsinfo`
csobsinfo instance
"""
# Get observation definition XML filename
obsdef = gammalib.GFilename(filename)
# Setup csobsinfo script
info = cscripts.csobsinfo()
info['inobs'] = obsdef.url()
info['debug'] = debug
# Set offset if the "ra" and "dec" arguments are not empty strings
if ra != '' and dec != '':
info['ra'] = float(ra)
info['dec'] = float(dec)
info['offset'] = True
else:
info['offset'] = False
# Run csobsinfo
info.run()
# Return
return info
# ================ #
# Plot information #
# ================ #
def plot_information(info, ra, dec, plotfile):
"""
Plot information
Parameters
----------
info : `~cscripts.csobsinfo`
csobsinfo instance
ra : float
Right Ascension (deg)
dec : float
Declination (deg)
plotfile : str
Plot filename
"""
# Retrieve observation information
zeniths = info.zeniths()
azimuths = info.azimuths()
offsets = info.offsets()
ebounds = info.ebounds()
gti = info.gti()
# Create figure with subplots. In case that offset and energy boundary
# information is available a column is added to the subplots
nrows = 2
ncols = 2
if info['offset'].boolean() and ebounds.size() > 0:
ncols += 1
iplot = 1
plt.figure()
plt.subplot(nrows, ncols, iplot)
# Plot zenith angle histogram
zmin = min(zeniths)
zmax = max(zeniths)
plt.hist(zeniths, bins=30, range=(zmin, zmax), fc='blue')
plt.xlabel('Zenith Angle (deg)')
plt.ylabel('Frequency')
plt.title('Zenith angle distribution')
# Plot azimuth angle histogram
iplot += 1
plt.subplot(nrows, ncols, iplot)
amin = min(azimuths)
amax = max(azimuths)
plt.hist(azimuths, bins=30, range=(amin, amax), fc='blue')
plt.xlabel('Azimuth Angle (deg)')
plt.ylabel('Frequency')
plt.title('Azimuth distribution')
# If available, plot offset from target histogram
if info['offset'].boolean():
iplot += 1
plt.subplot(nrows, ncols, iplot)
omin = min(offsets)
omax = max(offsets)
plt.hist(offsets, bins=30, range=(omin, omax), fc='blue')
plt.xlabel('Offset from (RA, DEC)=('+str(ra)+','+str(dec)+') (deg)')
plt.ylabel('Frequency')
plt.title('Offset distribution')
# If available, plot energy thresholds histogram
if ebounds.size() > 0:
emin = []
emax = []
for i in range(ebounds.size()):
emin.append(ebounds.emin(i).log10TeV())
emax.append(ebounds.emax(i).log10TeV())
iplot += 1
plt.subplot(nrows, ncols, iplot)
plt.hist(emin, bins=80, range=(-1.0, 2.0), fc='red', label='emin')
plt.hist(emax, bins=80, range=(-1.0, 2.0), fc='blue', label='emax')
plt.xlabel('Energy threshold (log10 (E/TeV))')
plt.ylabel('Frequency')
plt.legend(loc='upper left')
plt.title('Energy threshold')
# Plot zenith angle versus mean observing time for all observations
iplot += 1
plt.subplot(nrows, ncols, iplot)
times = []
for i in range(gti.size()):
tmean = gti.tstart(i) + 0.5*(gti.tstart(i)-gti.tstart(i))
times.append(tmean.mjd())
plt.plot(times, zeniths, 'o', lw=2.0, color='black')
plt.xlabel('Time (MJD)')
plt.ylabel('Zenith Angle (deg)')
plt.title('Observation time')
# Show plots or save it into file
if len(plotfile) > 0:
plt.savefig(plotfile)
else:
plt.show()
# Return
return
# ================ #
# Show observation #
# ================ #
def show_obs():
"""
Show observation
"""
# Set usage string
usage = 'show_obs.py [-ra ra] [-dec dec] [-p plotfile] file'
# Set default options
options = [{'option': '-ra', 'value': ''},
{'option': '-dec', 'value': ''},
{'option': '-p', 'value': ''}]
# Get arguments and options from command line arguments
args, options = cscripts.ioutils.get_args_options(options, usage)
# Extract script parameters from options
ra = options[0]['value']
dec = options[1]['value']
plotfile = options[2]['value']
# Run csobsinfo
info = run_csobsinfo(args[0], ra, dec)
# Plot information
plot_information(info, ra, dec, plotfile)
# Return
return
# ======================== #
# Main routine entry point #
# ======================== #
if __name__ == '__main__':
# Show observation
show_obs()