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