Commit 4998d4480c66c5fcd39a4e15e84fc24336f2889b

Authored by Jürgen Knödlseder
1 parent 7f4cb37d

Refactor show_obs.py script

examples/show_obs.py
@@ -31,43 +31,66 @@ except: @@ -31,43 +31,66 @@ except:
31 sys.exit() 31 sys.exit()
32 32
33 33
34 -# ======================== #  
35 -# Main routine entry point #  
36 -# ======================== #  
37 -if __name__ == '__main__': 34 +# ==================== #
  35 +# Run csobsinfo script #
  36 +# ==================== #
  37 +def run_csobsinfo(filename, ra=None, dec=None, debug=True):
  38 + """
  39 + Run csobsinfo script
38 40
39 - # Print help if wrong number of arguments provided  
40 - if len(sys.argv) < 2:  
41 - sys.exit('Usage: inspect_obs.py inobs.xml [ra] [dec] [file]') 41 + Parameters
  42 + ----------
  43 + filename : str
  44 + File name of observation definition XML file
  45 + ra : float, optional
  46 + Target Right Ascension (deg)
  47 + dec : float, optional
  48 + Target declination of pointing (deg)
  49 + debug : bool, optional
  50 + Switch on debugging in csobsinfo run
42 51
43 - # Check if plotting in file is requested  
44 - plotfile = ''  
45 - if len(sys.argv) == 3:  
46 - plotfile = sys.argv[2] 52 + Returns
  53 + -------
  54 + info : `~cscripts.csobsinfo`
  55 + csobsinfo instance
  56 + """
  57 + # Get observation definition XML filename
  58 + obsdef = gammalib.GFilename(filename)
47 59
48 - # Get input observation XML file  
49 - inobs = gammalib.GFilename(sys.argv[1])  
50 -  
51 - # Assert more than one observation (circumvents having a binned  
52 - # observation without e.g. zenith and azimuth)  
53 - if inobs.is_fits():  
54 - raise RuntimeError('Input observation container should be an XML file')  
55 -  
56 - # Run csobsinfo 60 + # Setup csobsinfo script
57 info = cscripts.csobsinfo() 61 info = cscripts.csobsinfo()
58 - info['offset'] = False  
59 -  
60 - # Add offset if coordinates are provided  
61 - if len(sys.argv) == 4:  
62 - ra = float(sys.argv[2])  
63 - dec = float(sys.argv[3]) 62 + info['inobs'] = obsdef.url()
  63 + info['debug'] = debug
  64 +
  65 + # Set offset
  66 + if ra != None and dec != None:
64 info['ra'] = ra 67 info['ra'] = ra
65 info['dec'] = dec 68 info['dec'] = dec
66 info['offset'] = True 69 info['offset'] = True
67 - info['inobs'] = inobs.url()  
68 - info['debug'] = True 70 + else:
  71 + info['offset'] = False
  72 +
  73 + # Run csobsinfo
69 info.run() 74 info.run()
70 - 75 +
  76 + # Return
  77 + return info
  78 +
  79 +
  80 +# ================ #
  81 +# Plot information #
  82 +# ================ #
  83 +def plot_information(info, plotfile=''):
  84 + """
  85 + Plot information
  86 +
  87 + Parameters
  88 + ----------
  89 + info : `~cscripts.csobsinfo`
  90 + csobsinfo instance
  91 + plotfile : str, optional
  92 + Plot filename
  93 + """
71 # Retrieve observation info 94 # Retrieve observation info
72 zeniths = info.zeniths() 95 zeniths = info.zeniths()
73 azimuths = info.azimuths() 96 azimuths = info.azimuths()
@@ -88,8 +111,8 @@ if __name__ == &#39;__main__&#39;: @@ -88,8 +111,8 @@ if __name__ == &#39;__main__&#39;:
88 zmin = min(zeniths) 111 zmin = min(zeniths)
89 zmax = max(zeniths) 112 zmax = max(zeniths)
90 plt.hist(zeniths, bins=30, range=(zmin, zmax), fc='blue') 113 plt.hist(zeniths, bins=30, range=(zmin, zmax), fc='blue')
91 - plt.xlabel('Zenith Angle [deg]')  
92 - plt.ylabel('Abundance') 114 + plt.xlabel('Zenith Angle (deg)')
  115 + plt.ylabel('Frequency')
93 plt.title('Zenith angle distribution') 116 plt.title('Zenith angle distribution')
94 117
95 # Plot azimuth angle distribution 118 # Plot azimuth angle distribution
@@ -99,7 +122,7 @@ if __name__ == &#39;__main__&#39;: @@ -99,7 +122,7 @@ if __name__ == &#39;__main__&#39;:
99 amax = max(azimuths) 122 amax = max(azimuths)
100 plt.hist(azimuths, bins=30, range=(amin, amax), fc='blue') 123 plt.hist(azimuths, bins=30, range=(amin, amax), fc='blue')
101 plt.xlabel('Azimuth Angle (deg)') 124 plt.xlabel('Azimuth Angle (deg)')
102 - plt.ylabel('Abundance') 125 + plt.ylabel('Frequency')
103 plt.title('Azimuth distribution') 126 plt.title('Azimuth distribution')
104 127
105 # Plot offset if possible 128 # Plot offset if possible
@@ -110,7 +133,7 @@ if __name__ == &#39;__main__&#39;: @@ -110,7 +133,7 @@ if __name__ == &#39;__main__&#39;:
110 omax = max(offsets) 133 omax = max(offsets)
111 plt.hist(offsets, bins=30, range=(omin, omax), fc='blue') 134 plt.hist(offsets, bins=30, range=(omin, omax), fc='blue')
112 plt.xlabel('Offset from (RA, DEC)=('+str(ra)+','+str(dec)+') (deg)') 135 plt.xlabel('Offset from (RA, DEC)=('+str(ra)+','+str(dec)+') (deg)')
113 - plt.ylabel('Abundance') 136 + plt.ylabel('Frequency')
114 plt.title('Offset distribution') 137 plt.title('Offset distribution')
115 138
116 # Plot energy thresholds if possible 139 # Plot energy thresholds if possible
@@ -125,7 +148,7 @@ if __name__ == &#39;__main__&#39;: @@ -125,7 +148,7 @@ if __name__ == &#39;__main__&#39;:
125 plt.hist(emin, bins=80, range=(-1.0, 2.0), fc='red', label='emin') 148 plt.hist(emin, bins=80, range=(-1.0, 2.0), fc='red', label='emin')
126 plt.hist(emax, bins=80, range=(-1.0, 2.0), fc='blue', label='emax') 149 plt.hist(emax, bins=80, range=(-1.0, 2.0), fc='blue', label='emax')
127 plt.xlabel('Energy threshold (log10 (E/TeV))') 150 plt.xlabel('Energy threshold (log10 (E/TeV))')
128 - plt.ylabel('Abundance') 151 + plt.ylabel('Frequency')
129 plt.legend(loc='upper left') 152 plt.legend(loc='upper left')
130 plt.title('Energy threshold') 153 plt.title('Energy threshold')
131 154
@@ -146,3 +169,32 @@ if __name__ == &#39;__main__&#39;: @@ -146,3 +169,32 @@ if __name__ == &#39;__main__&#39;:
146 plt.savefig(plotfile) 169 plt.savefig(plotfile)
147 else: 170 else:
148 plt.show() 171 plt.show()
  172 +
  173 + # Return
  174 + return
  175 +
  176 +
  177 +# ======================== #
  178 +# Main routine entry point #
  179 +# ======================== #
  180 +if __name__ == '__main__':
  181 +
  182 + # Print usage message if wrong number of arguments is provided
  183 + if len(sys.argv) < 2:
  184 + sys.exit('Usage: inspect_obs.py inobs.xml [ra] [dec] [file]')
  185 +
  186 + # Extract optional parameters
  187 + plotfile = ''
  188 + ra = None
  189 + dec = None
  190 + if len(sys.argv) == 3:
  191 + plotfile = sys.argv[2]
  192 + elif len(sys.argv) == 4:
  193 + ra = float(sys.argv[2])
  194 + dec = float(sys.argv[3])
  195 +
  196 + # Run csobsinfo
  197 + info = run_csobsinfo(sys.argv[1], ra=ra, dec=dec)
  198 +
  199 + # Plot information
  200 + plot_information(info, plotfile=plotfile)
test/test_examples.py.in
@@ -351,7 +351,7 @@ class Test(gammalib.GPythonTestSuite): @@ -351,7 +351,7 @@ class Test(gammalib.GPythonTestSuite):
351 """ 351 """
352 # Execute script 352 # Execute script
353 self._execute_python('show_obs', 353 self._execute_python('show_obs',
354 - args='obs_selected.xml example_show_one.jpg') 354 + args='obs_selected.xml example_show_obs.jpg')
355 355
356 # Return 356 # Return
357 return 357 return