Commit 593f8844a39831756cee9424e87747c0eb091a99
1 parent
96d2ba22
Add show_phases.py example script and unit test
Showing
5 changed files
with
158 additions
and
11 deletions
examples/Makefile.am
examples/README.md
| ... | ... | @@ -4,7 +4,7 @@ ctools examples |
| 4 | 4 | This folder contains Python example scripts that illustrate the usage of |
| 5 | 5 | the ctools. |
| 6 | 6 | |
| 7 | -`generate_prod3_irfs/py` | |
| 7 | +`generate_prod3_irfs.py` | |
| 8 | 8 | > Generates Prod3 Instrument Response Functions from FITS files. |
| 9 | 9 | |
| 10 | 10 | `make_pointings.py` |
| ... | ... | @@ -48,12 +48,27 @@ the ctools. |
| 48 | 48 | `show_butterfly.py` |
| 49 | 49 | > Displays a butterfly generated using the ctbutterfly tool. |
| 50 | 50 | |
| 51 | +`show_irf.py` | |
| 52 | +> Displays an Instrument Response Function. | |
| 53 | + | |
| 54 | +`show_lightcurve.py` | |
| 55 | +> Displays lightcurve generated by the cslightcrv script. | |
| 56 | + | |
| 51 | 57 | `show_model.py` |
| 52 | 58 | > Displays a spectral model. |
| 53 | 59 | |
| 60 | +`show_obs.py` | |
| 61 | +> Displays an observation definition XML file. | |
| 62 | + | |
| 54 | 63 | `show_pha.py` |
| 55 | 64 | > Displays a spectrum in PHA format. |
| 56 | 65 | |
| 66 | +`show_phases.py` | |
| 67 | +> Displays event phases in an event list of observation definition XML file. | |
| 68 | + | |
| 69 | +`show_pointings.py` | |
| 70 | +> Displays pointings in an observation definition XML file. | |
| 71 | + | |
| 57 | 72 | `show_pull_evolution.py` |
| 58 | 73 | > Displays the evolution of the mean and rms pull as function of number of |
| 59 | 74 | > trials. |
| ... | ... | @@ -70,8 +85,5 @@ the ctools. |
| 70 | 85 | `show_spectrum.py` |
| 71 | 86 | > Displays spectrum generated by the csspec script. |
| 72 | 87 | |
| 73 | -`show_lightcurve.py` | |
| 74 | -> Displays lightcurve generated by the cslightcrv script. | |
| 75 | - | |
| 76 | 88 | `show_ts_distribution.py` |
| 77 | 89 | > Displays Test Statistic histograms generated using the cstsdist script. | ... | ... |
examples/show_phases.py
0 → 100755
| 1 | +#! /usr/bin/env python | |
| 2 | +# ========================================================================== | |
| 3 | +# Display phase information from event list | |
| 4 | +# | |
| 5 | +# Copyright (C) 2017 Juergen Knoedlseder | |
| 6 | +# | |
| 7 | +# This program is free software: you can redistribute it and/or modify | |
| 8 | +# it under the terms of the GNU General Public License as published by | |
| 9 | +# the Free Software Foundation, either version 3 of the License, or | |
| 10 | +# (at your option) any later version. | |
| 11 | +# | |
| 12 | +# This program is distributed in the hope that it will be useful, | |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | +# GNU General Public License for more details. | |
| 16 | +# | |
| 17 | +# You should have received a copy of the GNU General Public License | |
| 18 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 19 | +# | |
| 20 | +# ========================================================================== | |
| 21 | +import sys | |
| 22 | +import gammalib | |
| 23 | +import cscripts | |
| 24 | +try: | |
| 25 | + import matplotlib.pyplot as plt | |
| 26 | + plt.figure() | |
| 27 | + plt.close() | |
| 28 | +except (ImportError, RuntimeError): | |
| 29 | + print('This script needs the "matplotlib" module') | |
| 30 | + sys.exit() | |
| 31 | + | |
| 32 | + | |
| 33 | +# =========== # | |
| 34 | +# Plot phases # | |
| 35 | +# =========== # | |
| 36 | +def plot_phases(filename, plotfile, nphases=40): | |
| 37 | + """ | |
| 38 | + Plot phases | |
| 39 | + | |
| 40 | + Parameters | |
| 41 | + ---------- | |
| 42 | + filename : str | |
| 43 | + Name of lightcurve FITS file | |
| 44 | + plotfile : str | |
| 45 | + Plot file name | |
| 46 | + """ | |
| 47 | + # Read observation container. If an exception occurs then try loading the | |
| 48 | + # file as an event list and build an observation container. | |
| 49 | + try: | |
| 50 | + obs = gammalib.GObservations(filename) | |
| 51 | + except: | |
| 52 | + obs = gammalib.GObservations() | |
| 53 | + obs.append(gammalib.GCTAObservation(filename)) | |
| 54 | + | |
| 55 | + # Initialise phases | |
| 56 | + phases = [] | |
| 57 | + | |
| 58 | + # Loop over all observations | |
| 59 | + for run in obs: | |
| 60 | + | |
| 61 | + # Get events | |
| 62 | + events = run.events() | |
| 63 | + | |
| 64 | + # Loop over all events | |
| 65 | + for event in events: | |
| 66 | + | |
| 67 | + # Get phase | |
| 68 | + phase = event.phase() | |
| 69 | + | |
| 70 | + # Collect phase | |
| 71 | + phases.append(phase) | |
| 72 | + phases.append(phase+1.0) | |
| 73 | + | |
| 74 | + # Plot phase | |
| 75 | + plt.figure() | |
| 76 | + plt.hist(phases,bins=nphases,range=(0.0,2.0),facecolor='r') | |
| 77 | + plt.xlabel('Phase') | |
| 78 | + plt.ylabel('Events') | |
| 79 | + | |
| 80 | + # Show figure | |
| 81 | + if len(plotfile) > 0: | |
| 82 | + plt.savefig(plotfile) | |
| 83 | + else: | |
| 84 | + plt.show() | |
| 85 | + | |
| 86 | + # Return | |
| 87 | + return | |
| 88 | + | |
| 89 | + | |
| 90 | +# =========== # | |
| 91 | +# Show phases # | |
| 92 | +# =========== # | |
| 93 | +def show_phases(): | |
| 94 | + """ | |
| 95 | + Show phases | |
| 96 | + """ | |
| 97 | + # Set usage string | |
| 98 | + usage = 'show_phases.py [-p plotfile] [file]' | |
| 99 | + | |
| 100 | + # Set default options | |
| 101 | + options = [{'option': '-n', 'value': '20'}, | |
| 102 | + {'option': '-p', 'value': ''}] | |
| 103 | + | |
| 104 | + # Get arguments and options from command line arguments | |
| 105 | + args, options = cscripts.ioutils.get_args_options(options, usage) | |
| 106 | + | |
| 107 | + # Extract script parameters from options | |
| 108 | + nphases = int(options[0]['value'])*2 | |
| 109 | + plotfile = options[1]['value'] | |
| 110 | + | |
| 111 | + # Plot phases | |
| 112 | + plot_phases(args[0], plotfile, nphases=nphases) | |
| 113 | + | |
| 114 | + # Return | |
| 115 | + return | |
| 116 | + | |
| 117 | + | |
| 118 | +# ======================== # | |
| 119 | +# Main routine entry point # | |
| 120 | +# ======================== # | |
| 121 | +if __name__ == '__main__': | |
| 122 | + | |
| 123 | + # Show phases | |
| 124 | + show_phases() | ... | ... |
test/data/obs_unbinned.xml
| 1 | 1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 2 | 2 | <observation_list title="observation list"> |
| 3 | 3 | <observation name="" id="00001" instrument="CTA"> |
| 4 | - <parameter name="EventList" file="crab_events.fits" /> | |
| 4 | + <parameter name="EventList" file="$(TEST_DATA)/crab_events.fits" /> | |
| 5 | 5 | <parameter name="EffectiveArea" file="$(TEST_DATA)/irf_file.fits" /> |
| 6 | 6 | <parameter name="PointSpreadFunction" file="$(TEST_DATA)/irf_file.fits" /> |
| 7 | 7 | <parameter name="EnergyDispersion" file="$(TEST_DATA)/irf_file.fits" /> | ... | ... |
test/test_examples.py.in
| ... | ... | @@ -120,6 +120,8 @@ class Test(gammalib.GPythonTestSuite): |
| 120 | 120 | 'Test show_model.py') |
| 121 | 121 | self.append(self.test_show_pha, |
| 122 | 122 | 'Test show_pha.py') |
| 123 | + self.append(self.test_show_phases, | |
| 124 | + 'Test show_phases.py') | |
| 123 | 125 | self.append(self.test_show_pull_evolution, |
| 124 | 126 | 'Test show_pull_evolution.py') |
| 125 | 127 | self.append(self.test_show_pull_histogram, |
| ... | ... | @@ -334,6 +336,20 @@ class Test(gammalib.GPythonTestSuite): |
| 334 | 336 | # Return |
| 335 | 337 | return |
| 336 | 338 | |
| 339 | + # Test show_phases | |
| 340 | + def test_show_phases(self): | |
| 341 | + """ | |
| 342 | + Test show_phases | |
| 343 | + """ | |
| 344 | + # Set arguments | |
| 345 | + args = '-p example_show_phases.eps %s/phased_events.fits' % self._datadir | |
| 346 | + | |
| 347 | + # Execute script | |
| 348 | + self._execute_python('show_phases', args=args) | |
| 349 | + | |
| 350 | + # Return | |
| 351 | + return | |
| 352 | + | |
| 337 | 353 | # Test show_pull_evolution |
| 338 | 354 | def test_show_pull_evolution(self): |
| 339 | 355 | """ |
| ... | ... | @@ -526,12 +542,6 @@ if __name__ == '__main__': |
| 526 | 542 | # Save test results |
| 527 | 543 | suites.save('reports/examples.xml') |
| 528 | 544 | |
| 529 | - # Set return code | |
| 530 | - if success: | |
| 531 | - rc = 0 | |
| 532 | - else: | |
| 533 | - rc = 1 | |
| 534 | - | |
| 535 | 545 | # Raise an exception in case of failure |
| 536 | 546 | if not success: |
| 537 | 547 | raise RuntimeError('At least one error occured during the test.') | ... | ... |