Commit 7f4cb37d3c9a5361446c7fe78f6f35284933e898

Authored by Jürgen Knödlseder
1 parent 02215253

Integrate show_obs.py example code

.gitignore
... ... @@ -82,6 +82,7 @@ test/*.reg
82 82 test/*.trs
83 83 test/*.txt
84 84 test/*.eps
  85 +test/*.jpg
85 86 test/test_cscripts.sh
86 87 test/test_examples.py
87 88 test/reports/*.xml
... ...
examples/show_obs.py
... ... @@ -2,9 +2,6 @@
2 2 # ==========================================================================
3 3 # Display summary of observation definition XML file
4 4 #
5   -# Required 3rd party modules:
6   -# - matplotlib
7   -#
8 5 # Copyright (C) 2016 Michael Mayer
9 6 #
10 7 # This program is free software: you can redistribute it and/or modify
... ... @@ -40,17 +37,14 @@ except:
40 37 if __name__ == '__main__':
41 38  
42 39 # Print help if wrong number of arguments provided
43   - if not len(sys.argv) > 1:
44   - msg = 'Usage: inspect_obs.py <inobs.xml> <ra> <dec>\n'
45   - msg += 'or\n'
46   - msg += 'inspect_obs.py <inobs.xml>'
47   - sys.exit(msg)
48   -
49   - # Initialise flag if plots should be shown or saved
50   - save = False
51   - if 'save' in sys.argv:
52   - save = True
53   -
  40 + if len(sys.argv) < 2:
  41 + sys.exit('Usage: inspect_obs.py inobs.xml [ra] [dec] [file]')
  42 +
  43 + # Check if plotting in file is requested
  44 + plotfile = ''
  45 + if len(sys.argv) == 3:
  46 + plotfile = sys.argv[2]
  47 +
54 48 # Get input observation XML file
55 49 inobs = gammalib.GFilename(sys.argv[1])
56 50  
... ... @@ -65,10 +59,10 @@ if __name__ == &#39;__main__&#39;:
65 59  
66 60 # Add offset if coordinates are provided
67 61 if len(sys.argv) == 4:
68   - ra = float(sys.argv[2])
  62 + ra = float(sys.argv[2])
69 63 dec = float(sys.argv[3])
70   - info['ra'] = ra
71   - info['dec'] = dec
  64 + info['ra'] = ra
  65 + info['dec'] = dec
72 66 info['offset'] = True
73 67 info['inobs'] = inobs.url()
74 68 info['debug'] = True
... ... @@ -80,61 +74,64 @@ if __name__ == &#39;__main__&#39;:
80 74 offsets = info.offsets()
81 75 ebounds = info.ebounds()
82 76 gti = info.gti()
83   -
84   - # Plot zenith angle distribution
  77 +
  78 + # Create figure with subplots
  79 + nrows = 2
  80 + ncols = 2
  81 + if info['offset'].boolean() and ebounds.size() > 0:
  82 + ncols += 1
  83 + iplot = 1
85 84 plt.figure()
  85 + plt.subplot(nrows, ncols, iplot)
  86 +
  87 + # Plot zenith angle distribution
86 88 zmin = min(zeniths)
87 89 zmax = max(zeniths)
88 90 plt.hist(zeniths, bins=30, range=(zmin, zmax), fc='blue')
89 91 plt.xlabel('Zenith Angle [deg]')
90 92 plt.ylabel('Abundance')
91 93 plt.title('Zenith angle distribution')
92   - if save:
93   - plt.savefig('Zenith angle distribution.eps')
94   -
  94 +
95 95 # Plot azimuth angle distribution
96   - plt.figure()
  96 + iplot += 1
  97 + plt.subplot(nrows, ncols, iplot)
97 98 amin = min(azimuths)
98 99 amax = max(azimuths)
99 100 plt.hist(azimuths, bins=30, range=(amin, amax), fc='blue')
100 101 plt.xlabel('Azimuth Angle (deg)')
101 102 plt.ylabel('Abundance')
102 103 plt.title('Azimuth distribution')
103   - if save:
104   - plt.savefig('Azimuth distribution.eps')
105   -
  104 +
106 105 # Plot offset if possible
107 106 if info['offset'].boolean():
108   - plt.figure()
  107 + iplot += 1
  108 + plt.subplot(nrows, ncols, iplot)
109 109 omin = min(offsets)
110 110 omax = max(offsets)
111 111 plt.hist(offsets, bins=30, range=(omin, omax), fc='blue')
112 112 plt.xlabel('Offset from (RA, DEC)=('+str(ra)+','+str(dec)+') (deg)')
113 113 plt.ylabel('Abundance')
114 114 plt.title('Offset distribution')
115   - if save:
116   - plt.savefig('Offset distribution.eps')
117   -
  115 +
118 116 # Plot energy thresholds if possible
119   - if ebounds.size():
120   -
  117 + if ebounds.size() > 0:
121 118 emin = []
122 119 emax = []
123 120 for i in range(ebounds.size()):
124 121 emin.append(ebounds.emin(i).log10TeV())
125 122 emax.append(ebounds.emax(i).log10TeV())
126   - plt.figure()
  123 + iplot += 1
  124 + plt.subplot(nrows, ncols, iplot)
127 125 plt.hist(emin, bins=80, range=(-1.0, 2.0), fc='red', label='emin')
128 126 plt.hist(emax, bins=80, range=(-1.0, 2.0), fc='blue', label='emax')
129 127 plt.xlabel('Energy threshold (log10 (E/TeV))')
130 128 plt.ylabel('Abundance')
131 129 plt.legend(loc='upper left')
132 130 plt.title('Energy threshold')
133   - if save:
134   - plt.savefig('Energy threshold.eps')
135   -
  131 +
136 132 # Plot observation point in time wrt zenith angle
137   - plt.figure()
  133 + iplot += 1
  134 + plt.subplot(nrows, ncols, iplot)
138 135 times = []
139 136 for i in range(gti.size()):
140 137 tmean = gti.tstart(i) + 0.5*(gti.tstart(i)-gti.tstart(i))
... ... @@ -143,12 +140,9 @@ if __name__ == &#39;__main__&#39;:
143 140 plt.xlabel('Time (MJD)')
144 141 plt.ylabel('Zenith Angle (deg)')
145 142 plt.title('Observation time')
146   - if save:
147   - plt.savefig('Observation time.eps')
148 143  
149   - # Display plots if possible
150   - if not save:
151   -
152   - # Show plots
  144 + # Show plots or save it into file
  145 + if len(plotfile) > 0:
  146 + plt.savefig(plotfile)
  147 + else:
153 148 plt.show()
154   -
155 149 \ No newline at end of file
... ...
test/test_examples.py.in
... ... @@ -129,8 +129,6 @@ class Test(gammalib.GPythonTestSuite):
129 129 self._execute_python(script, args='gc', log='make_pointings_gc')
130 130 self._execute_python(script, args='lmc', log='make_pointings_lmc')
131 131  
132   - #TODO: Do any testing
133   -
134 132 # Return
135 133 return
136 134  
... ... @@ -142,8 +140,6 @@ class Test(gammalib.GPythonTestSuite):
142 140 # Execute script
143 141 self._execute_python('generate_prod3_irfs')
144 142  
145   - #TODO: Do any testing
146   -
147 143 # Return
148 144 return
149 145  
... ... @@ -155,8 +151,6 @@ class Test(gammalib.GPythonTestSuite):
155 151 # Execute script
156 152 self._execute_python('make_spectrum', args='eps')
157 153  
158   - #TODO: Do any testing
159   -
160 154 # Return
161 155 return
162 156  
... ... @@ -168,8 +162,6 @@ class Test(gammalib.GPythonTestSuite):
168 162 # Execute script
169 163 self._execute_python('make_ts_distributions', args='-n 2 -e 0 -d 1800')
170 164  
171   - #TODO: Do any testing
172   -
173 165 # Return
174 166 return
175 167  
... ... @@ -181,8 +173,6 @@ class Test(gammalib.GPythonTestSuite):
181 173 # Execute script
182 174 self._execute_python('pipeline_binned_disk')
183 175  
184   - #TODO: Do any testing
185   -
186 176 # Return
187 177 return
188 178  
... ... @@ -194,8 +184,6 @@ class Test(gammalib.GPythonTestSuite):
194 184 # Execute script
195 185 self._execute_python('pipeline_binned_mem')
196 186  
197   - #TODO: Do any testing
198   -
199 187 # Return
200 188 return
201 189  
... ... @@ -207,8 +195,6 @@ class Test(gammalib.GPythonTestSuite):
207 195 # Execute script
208 196 self._execute_python('pipeline_stacked_disk')
209 197  
210   - #TODO: Do any testing
211   -
212 198 # Return
213 199 return
214 200  
... ... @@ -220,8 +206,6 @@ class Test(gammalib.GPythonTestSuite):
220 206 # Execute script
221 207 self._execute_python('pipeline_stacked_mem')
222 208  
223   - #TODO: Do any testing
224   -
225 209 # Return
226 210 return
227 211  
... ... @@ -233,8 +217,6 @@ class Test(gammalib.GPythonTestSuite):
233 217 # Execute script
234 218 self._execute_python('pipeline_unbinned_disk')
235 219  
236   - #TODO: Do any testing
237   -
238 220 # Return
239 221 return
240 222  
... ... @@ -246,8 +228,6 @@ class Test(gammalib.GPythonTestSuite):
246 228 # Execute script
247 229 self._execute_python('pipeline_unbinned_mem')
248 230  
249   - #TODO: Do any testing
250   -
251 231 # Return
252 232 return
253 233  
... ... @@ -261,8 +241,6 @@ class Test(gammalib.GPythonTestSuite):
261 241 args='data/butterfly.txt '
262 242 'example_show_butterfly.eps')
263 243  
264   - #TODO: Do any testing
265   -
266 244 # Return
267 245 return
268 246  
... ... @@ -275,8 +253,6 @@ class Test(gammalib.GPythonTestSuite):
275 253 self._execute_python('show_model',
276 254 args='data/crab.xml Crab example_show_model.eps')
277 255  
278   - #TODO: Do any testing
279   -
280 256 # Return
281 257 return
282 258  
... ... @@ -289,8 +265,6 @@ class Test(gammalib.GPythonTestSuite):
289 265 self._execute_python('show_pha',
290 266 args='data/pha.fits example_show_pha.eps')
291 267  
292   - #TODO: Do any testing
293   -
294 268 # Return
295 269 return
296 270  
... ... @@ -304,8 +278,6 @@ class Test(gammalib.GPythonTestSuite):
304 278 args='data/pull.dat Pull_Crab_Prefactor '
305 279 'example_show_pull_evolution.eps')
306 280  
307   - #TODO: Do any testing
308   -
309 281 # Return
310 282 return
311 283  
... ... @@ -319,8 +291,6 @@ class Test(gammalib.GPythonTestSuite):
319 291 args='data/pull.dat Pull_Crab_Prefactor 50 '
320 292 'example_show_pull_histogram.eps')
321 293  
322   - #TODO: Do any testing
323   -
324 294 # Return
325 295 return
326 296  
... ... @@ -332,8 +302,6 @@ class Test(gammalib.GPythonTestSuite):
332 302 # Execute script
333 303 self._execute_python('show_response', args='example_show_response.eps')
334 304  
335   - #TODO: Do any testing
336   -
337 305 # Return
338 306 return
339 307  
... ... @@ -347,8 +315,6 @@ class Test(gammalib.GPythonTestSuite):
347 315 args='data/sensitivity.dat '
348 316 'example_show_sensitivity.eps')
349 317  
350   - #TODO: Do any testing
351   -
352 318 # Return
353 319 return
354 320  
... ... @@ -362,8 +328,6 @@ class Test(gammalib.GPythonTestSuite):
362 328 args='data/spectrum.fits '
363 329 'example_show_spectrum.eps')
364 330  
365   - #TODO: Do any testing
366   -
367 331 # Return
368 332 return
369 333  
... ... @@ -377,8 +341,6 @@ class Test(gammalib.GPythonTestSuite):
377 341 args='data/ts.dat -n 100 '
378 342 '-f example_show_ts_distribution.eps')
379 343  
380   - #TODO: Do any testing
381   -
382 344 # Return
383 345 return
384 346  
... ... @@ -389,13 +351,11 @@ class Test(gammalib.GPythonTestSuite):
389 351 """
390 352 # Execute script
391 353 self._execute_python('show_obs',
392   - args='obs_selected.xml save')
393   -
394   - #TODO: Do any testing
  354 + args='obs_selected.xml example_show_one.jpg')
395 355  
396 356 # Return
397 357 return
398   -
  358 +
399 359  
400 360 # ======================== #
401 361 # Main routine entry point #
... ...