Commit 4998d4480c66c5fcd39a4e15e84fc24336f2889b
1 parent
7f4cb37d
Refactor show_obs.py script
Showing
2 changed files
with
87 additions
and
35 deletions
examples/show_obs.py
| ... | ... | @@ -31,43 +31,66 @@ except: |
| 31 | 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 | 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 | 67 | info['ra'] = ra |
| 65 | 68 | info['dec'] = dec |
| 66 | 69 | info['offset'] = True |
| 67 | - info['inobs'] = inobs.url() | |
| 68 | - info['debug'] = True | |
| 70 | + else: | |
| 71 | + info['offset'] = False | |
| 72 | + | |
| 73 | + # Run csobsinfo | |
| 69 | 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 | 94 | # Retrieve observation info |
| 72 | 95 | zeniths = info.zeniths() |
| 73 | 96 | azimuths = info.azimuths() |
| ... | ... | @@ -88,8 +111,8 @@ if __name__ == '__main__': |
| 88 | 111 | zmin = min(zeniths) |
| 89 | 112 | zmax = max(zeniths) |
| 90 | 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 | 116 | plt.title('Zenith angle distribution') |
| 94 | 117 | |
| 95 | 118 | # Plot azimuth angle distribution |
| ... | ... | @@ -99,7 +122,7 @@ if __name__ == '__main__': |
| 99 | 122 | amax = max(azimuths) |
| 100 | 123 | plt.hist(azimuths, bins=30, range=(amin, amax), fc='blue') |
| 101 | 124 | plt.xlabel('Azimuth Angle (deg)') |
| 102 | - plt.ylabel('Abundance') | |
| 125 | + plt.ylabel('Frequency') | |
| 103 | 126 | plt.title('Azimuth distribution') |
| 104 | 127 | |
| 105 | 128 | # Plot offset if possible |
| ... | ... | @@ -110,7 +133,7 @@ if __name__ == '__main__': |
| 110 | 133 | omax = max(offsets) |
| 111 | 134 | plt.hist(offsets, bins=30, range=(omin, omax), fc='blue') |
| 112 | 135 | plt.xlabel('Offset from (RA, DEC)=('+str(ra)+','+str(dec)+') (deg)') |
| 113 | - plt.ylabel('Abundance') | |
| 136 | + plt.ylabel('Frequency') | |
| 114 | 137 | plt.title('Offset distribution') |
| 115 | 138 | |
| 116 | 139 | # Plot energy thresholds if possible |
| ... | ... | @@ -125,7 +148,7 @@ if __name__ == '__main__': |
| 125 | 148 | plt.hist(emin, bins=80, range=(-1.0, 2.0), fc='red', label='emin') |
| 126 | 149 | plt.hist(emax, bins=80, range=(-1.0, 2.0), fc='blue', label='emax') |
| 127 | 150 | plt.xlabel('Energy threshold (log10 (E/TeV))') |
| 128 | - plt.ylabel('Abundance') | |
| 151 | + plt.ylabel('Frequency') | |
| 129 | 152 | plt.legend(loc='upper left') |
| 130 | 153 | plt.title('Energy threshold') |
| 131 | 154 | |
| ... | ... | @@ -146,3 +169,32 @@ if __name__ == '__main__': |
| 146 | 169 | plt.savefig(plotfile) |
| 147 | 170 | else: |
| 148 | 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