Commit 431bda7b8170e95621a8c8d0546b7bd5c7772ec2
1 parent
70b28cc1
Add show_pointing.py script
Showing
3 changed files
with
212 additions
and
5 deletions
examples/make_pointings.py
| ... | ... | @@ -72,8 +72,7 @@ def get_positions(xmin, xmax, ymin, ymax, step): |
| 72 | 72 | # is invariant of latitude. |
| 73 | 73 | xstep = step / math.cos(gammalib.deg2rad * y) |
| 74 | 74 | nx = int((xmax-xmin)/xstep+1.5) |
| 75 | - rescale = (xmax-xmin)/(xstep*(nx-1)) | |
| 76 | - xstep *= rescale | |
| 75 | + xstep = (xmax-xmin)/float(nx-1) | |
| 77 | 76 | |
| 78 | 77 | # Set x offset. For every second row the x position is displace by |
| 79 | 78 | # half a step size. |
| ... | ... | @@ -427,9 +426,9 @@ def set_extgal(separation=3.0, caldb='prod2', lst=True): |
| 427 | 426 | |
| 428 | 427 | # Set patch |
| 429 | 428 | obsdef.extend(set_patch(tmin, lmin=-90.0, lmax=90.0, bmin=+5.0, bmax=+88.0, |
| 430 | - separation=separation, hours=1000, | |
| 429 | + separation=separation, hours=500, run_duration=25, | |
| 431 | 430 | site='Automatic', caldb=caldb, lst=lst, |
| 432 | - autodec=10.0)) | |
| 431 | + autodec=-10.0)) | |
| 433 | 432 | |
| 434 | 433 | # Return observation definition |
| 435 | 434 | return obsdef |
| ... | ... | @@ -501,6 +500,9 @@ def set_lmc(hours=250.0, caldb='prod2', lst=True): |
| 501 | 500 | # Initialise observation definition |
| 502 | 501 | obsdef = [] |
| 503 | 502 | |
| 503 | + # Initialise start time (1-1-2021) | |
| 504 | + tmin = 7671.0 * 86400.0 | |
| 505 | + | |
| 504 | 506 | # Set LMC centre |
| 505 | 507 | centre = gammalib.GSkyDir() |
| 506 | 508 | centre.radec_deg(80.0, -69.0) |
| ... | ... | @@ -523,7 +525,7 @@ def set_lmc(hours=250.0, caldb='prod2', lst=True): |
| 523 | 525 | lat = pnt.b_deg() |
| 524 | 526 | |
| 525 | 527 | # Set positions and duration |
| 526 | - obs = {'lon': lon, 'lat': lat, 'duration': duration} | |
| 528 | + obs = {'lon': lon, 'lat': lat, 'tmin': tmin, 'duration': duration} | |
| 527 | 529 | |
| 528 | 530 | # Set IRF |
| 529 | 531 | irf = set_irf('South', obs, caldb, lst=lst) |
| ... | ... | @@ -535,6 +537,9 @@ def set_lmc(hours=250.0, caldb='prod2', lst=True): |
| 535 | 537 | # Append observation |
| 536 | 538 | obsdef.append(obs) |
| 537 | 539 | |
| 540 | + # Update start time for next pointing | |
| 541 | + tmin = set_tmin_for_next_pointing(tmin, duration) | |
| 542 | + | |
| 538 | 543 | # Dump statistics |
| 539 | 544 | print('Number of pointings: %d (%.2f s)' % (n_pnt,duration)) |
| 540 | 545 | ... | ... |
examples/show_pointings.py
0 → 100755
| 1 | +#! /usr/bin/env python | |
| 2 | +# ========================================================================== | |
| 3 | +# Display pointings in observation definition XML file | |
| 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 | +# Extract pointings from XML file # | |
| 35 | +# =============================== # | |
| 36 | +def get_pointings(filename): | |
| 37 | + """ | |
| 38 | + Extract pointings from XML file | |
| 39 | + | |
| 40 | + Parameters | |
| 41 | + ---------- | |
| 42 | + filename : str | |
| 43 | + File name of observation definition XML file | |
| 44 | + | |
| 45 | + Returns | |
| 46 | + ------- | |
| 47 | + pnt : list of dict | |
| 48 | + Pointings | |
| 49 | + """ | |
| 50 | + # Initialise pointings | |
| 51 | + pnt = [] | |
| 52 | + | |
| 53 | + # Open XML file | |
| 54 | + xml = gammalib.GXml(filename) | |
| 55 | + | |
| 56 | + # Get observation list | |
| 57 | + obs = xml.element('observation_list') | |
| 58 | + | |
| 59 | + # Get number of observations | |
| 60 | + nobs = obs.elements('observation') | |
| 61 | + | |
| 62 | + # Loop over observations | |
| 63 | + for i in range(nobs): | |
| 64 | + | |
| 65 | + # Get observation | |
| 66 | + run = obs.element('observation', i) | |
| 67 | + | |
| 68 | + # Get pointing parameter | |
| 69 | + npars = run.elements('parameter') | |
| 70 | + ra = None | |
| 71 | + dec = None | |
| 72 | + south = False | |
| 73 | + for k in range(npars): | |
| 74 | + par = run.element('parameter', k) | |
| 75 | + if par.attribute('name') == 'Pointing': | |
| 76 | + ra = float(par.attribute('ra')) | |
| 77 | + dec = float(par.attribute('dec')) | |
| 78 | + if par.attribute('name') == 'Calibration': | |
| 79 | + if 'South' in par.attribute('response'): | |
| 80 | + south = True | |
| 81 | + else: | |
| 82 | + south = False | |
| 83 | + if ra != None: | |
| 84 | + p = gammalib.GSkyDir() | |
| 85 | + p.radec_deg(ra, dec) | |
| 86 | + entry = {'l': p.l_deg(), 'b': p.b_deg(), 'ra': ra, 'dec': dec, 'south': south} | |
| 87 | + pnt.append(entry) | |
| 88 | + | |
| 89 | + # Return pointings | |
| 90 | + return pnt | |
| 91 | + | |
| 92 | + | |
| 93 | +# ================ # | |
| 94 | +# Plot information # | |
| 95 | +# ================ # | |
| 96 | +def plot_pointings(pnt, plotfile): | |
| 97 | + """ | |
| 98 | + Plot information | |
| 99 | + | |
| 100 | + Parameters | |
| 101 | + ---------- | |
| 102 | + pnt : list of dict | |
| 103 | + Pointings | |
| 104 | + plotfile : str | |
| 105 | + Plot filename | |
| 106 | + """ | |
| 107 | + # Create figure | |
| 108 | + plt.figure() | |
| 109 | + | |
| 110 | + # Setup figure | |
| 111 | + ax = plt.gca() | |
| 112 | + ax.cla() | |
| 113 | + ax.set_xlim((180, -180)) | |
| 114 | + ax.set_ylim((-90, 90)) | |
| 115 | + | |
| 116 | + # Loop over pointings | |
| 117 | + for p in pnt: | |
| 118 | + | |
| 119 | + # Get longitude | |
| 120 | + l = p['l'] | |
| 121 | + if l > 180.0: | |
| 122 | + l = l - 360.0 | |
| 123 | + | |
| 124 | + # Set color | |
| 125 | + if p['south']: | |
| 126 | + color='r' | |
| 127 | + else: | |
| 128 | + color='b' | |
| 129 | + | |
| 130 | + # Set circle | |
| 131 | + circle = plt.Circle((l, p['b']), 4.0, color=color, fill=False) | |
| 132 | + | |
| 133 | + # Add circle | |
| 134 | + ax.add_artist(circle) | |
| 135 | + | |
| 136 | + # Plot title and labels | |
| 137 | + plt.xlabel('Galactic longitude (deg)') | |
| 138 | + plt.ylabel('Galactic latitude (deg)') | |
| 139 | + plt.title('Pointings') | |
| 140 | + | |
| 141 | + # Show plots or save it into file | |
| 142 | + if len(plotfile) > 0: | |
| 143 | + plt.savefig(plotfile) | |
| 144 | + else: | |
| 145 | + plt.show() | |
| 146 | + | |
| 147 | + # Return | |
| 148 | + return | |
| 149 | + | |
| 150 | + | |
| 151 | +# ============== # | |
| 152 | +# Show pointings # | |
| 153 | +# ============== # | |
| 154 | +def show_pointings(): | |
| 155 | + """ | |
| 156 | + Show pointings | |
| 157 | + """ | |
| 158 | + # Set usage string | |
| 159 | + usage = 'show_pointings.py [-p plotfile] file' | |
| 160 | + | |
| 161 | + # Set default options | |
| 162 | + options = [{'option': '-p', 'value': ''}] | |
| 163 | + | |
| 164 | + # Get arguments and options from command line arguments | |
| 165 | + args, options = cscripts.ioutils.get_args_options(options, usage) | |
| 166 | + | |
| 167 | + # Extract script parameters from options | |
| 168 | + plotfile = options[0]['value'] | |
| 169 | + | |
| 170 | + # Get pointings | |
| 171 | + pnt = get_pointings(args[0]) | |
| 172 | + | |
| 173 | + # Plot pointings | |
| 174 | + plot_pointings(pnt, plotfile) | |
| 175 | + | |
| 176 | + # Return | |
| 177 | + return | |
| 178 | + | |
| 179 | + | |
| 180 | +# ======================== # | |
| 181 | +# Main routine entry point # | |
| 182 | +# ======================== # | |
| 183 | +if __name__ == '__main__': | |
| 184 | + | |
| 185 | + # Show pointings | |
| 186 | + show_pointings() | ... | ... |
test/test_examples.py.in
| ... | ... | @@ -134,6 +134,8 @@ class Test(gammalib.GPythonTestSuite): |
| 134 | 134 | 'Test ts_distribution.py') |
| 135 | 135 | self.append(self.test_show_obs, |
| 136 | 136 | 'Test show_obs.py') |
| 137 | + self.append(self.test_show_pointings, | |
| 138 | + 'Test show_pointings.py') | |
| 137 | 139 | |
| 138 | 140 | # Return |
| 139 | 141 | return |
| ... | ... | @@ -460,6 +462,20 @@ class Test(gammalib.GPythonTestSuite): |
| 460 | 462 | # Return |
| 461 | 463 | return |
| 462 | 464 | |
| 465 | + # Test show_pointings | |
| 466 | + def test_show_pointings(self): | |
| 467 | + """ | |
| 468 | + Test show_pointings | |
| 469 | + """ | |
| 470 | + # Set arguments | |
| 471 | + args = '-p example_show_pointings.eps %s/obs_unbinned.xml' % self._datadir | |
| 472 | + | |
| 473 | + # Execute script | |
| 474 | + self._execute_python('show_pointings', args=args) | |
| 475 | + | |
| 476 | + # Return | |
| 477 | + return | |
| 478 | + | |
| 463 | 479 | |
| 464 | 480 | # ======================== # |
| 465 | 481 | # Main routine entry point # | ... | ... |