Commit 4b2b52fee940d5e4de4831aa0523cef81d1a10cd

Authored by Jürgen Knödlseder
1 parent 5947039a

Add unit test of make_pointings.py script

examples/make_pointings.py
1 1 #! /usr/bin/env python
2 2 # ==========================================================================
3   -# This script generates pointing patterns for CTA observations.
  3 +# Generates pointing patterns for CTA observations
4 4 #
5   -# Usage:
6   -# ./make_pointings.py
7   -#
8   -# ==========================================================================
9   -#
10   -# Copyright (C) 2015 Juergen Knoedlseder
  5 +# Copyright (C) 2015-2016 Juergen Knoedlseder
11 6 #
12 7 # This program is free software: you can redistribute it and/or modify
13 8 # it under the terms of the GNU General Public License as published by
... ... @@ -33,10 +28,30 @@ import gammalib
33 28 # ============================================================ #
34 29 def get_positions(xmin, xmax, ymin, ymax, step):
35 30 """
  31 + Get pointing positions for an interleaved pattern
  32 +
36 33 Get positions for a patch of interleaved pointings on the sky.
37 34 The function does respect the latitude dependence of the
38 35 pointing distance. The step parameter is for a latitude of
39 36 zero.
  37 +
  38 + Parameters
  39 + ----------
  40 + xmin : float
  41 + Minimum longitude (deg)
  42 + xmax : float
  43 + Maximum longitude (deg)
  44 + ymin : float
  45 + Minimum latitude (deg)
  46 + ymax : float
  47 + Maximum latitude (deg)
  48 + step : float
  49 + Pointing step size (deg)
  50 +
  51 + Returns
  52 + -------
  53 + positions : list of dict
  54 + List of dictionaries with x and y positions
40 55 """
41 56 # Initialise positions
42 57 positions = []
... ... @@ -46,7 +61,7 @@ def get_positions(xmin, xmax, ymin, ymax, step):
46 61 ny = int((ymax-ymin)/ystep+1.5)
47 62 rescale = (ymax-ymin)/(ystep*(ny-1))
48 63 ystep *= rescale
49   - print("Number of rows ....: %d" % (ny))
  64 + print('Number of rows ....: %d' % (ny))
50 65  
51 66 # Loop over rows
52 67 y = ymin
... ... @@ -63,7 +78,6 @@ def get_positions(xmin, xmax, ymin, ymax, step):
63 78 x = xmin
64 79 else:
65 80 x = xmin + 0.5 * xstep
66   - #print(row, y, nx)
67 81  
68 82 # Append pointings
69 83 for pnt in range(nx):
... ... @@ -86,7 +100,33 @@ def set_patch(lmin=-30.0, lmax=30.0, bmin=-1.5, bmax=+1.5,
86 100 separation=3.0, hours=100.0,
87 101 site=None, lst=True, autodec=0.0):
88 102 """
89   - Setup pointing patch on the sky.
  103 + Setup pointing patch on the sky
  104 +
  105 + Parameters
  106 + ----------
  107 + lmin : float, optional
  108 + Minimum Galactic Longitude (deg)
  109 + lmax : float, optional
  110 + Maximum Galactic Longitude (deg)
  111 + bmin : float, optional
  112 + Minimum Galactic Latitude (deg)
  113 + bmax : float, optional
  114 + Maximum Galactic Latitude (deg)
  115 + separation : float, optional
  116 + Pointing separation (deg)
  117 + hours : float, optional
  118 + Total observation duration (h)
  119 + site : str, optional
  120 + Array site (one of 'Automatic', 'South', 'North')
  121 + lst : bool, optional
  122 + Use LSTs
  123 + autodec : float, optional
  124 + Declination for switching automatically between South and North site
  125 +
  126 + Returns
  127 + -------
  128 + obsdef : list of dict
  129 + List of pointing definitions
90 130 """
91 131 # Initialise observation definition
92 132 obsdef = []
... ... @@ -112,30 +152,30 @@ def set_patch(lmin=-30.0, lmax=30.0, bmin=-1.5, bmax=+1.5,
112 152 if site != None:
113 153  
114 154 # Handle automatic site switching
115   - if site == "Automatic":
  155 + if site == 'Automatic':
116 156 pos = gammalib.GSkyDir()
117 157 pos.lb_deg(pnt['x'], pnt['y'])
118 158 dec = pos.dec_deg()
119 159 if (dec >= autodec):
120   - obs_site = "North"
  160 + obs_site = 'North'
121 161 else:
122   - obs_site = "South"
  162 + obs_site = 'South'
123 163 else:
124 164 obs_site = site
125 165  
126 166 # Set site IRF
127   - caldb = "prod2"
128   - if obs_site == "North":
  167 + caldb = 'prod2'
  168 + if obs_site == 'North':
129 169 if lst:
130   - irf = "North_50h"
  170 + irf = 'North_50h'
131 171 else:
132   - irf = "North_50h"
  172 + irf = 'North_50h'
133 173 exposure_north += duration
134 174 else:
135 175 if lst:
136   - irf = "South_50h"
  176 + irf = 'South_50h'
137 177 else:
138   - irf = "South_50h"
  178 + irf = 'South_50h'
139 179 exposure_south += duration
140 180  
141 181 # Set IRF information
... ... @@ -146,11 +186,11 @@ def set_patch(lmin=-30.0, lmax=30.0, bmin=-1.5, bmax=+1.5,
146 186 obsdef.append(obs)
147 187  
148 188 # Dump statistics
149   - print("Number of pointings: %d (%.2f sec)" % (n_pnt,duration))
150   - print("South array .......: %.2f hours (%.2f%%)" %
  189 + print('Number of pointings: %d (%.2f s)' % (n_pnt,duration))
  190 + print('South array .......: %.2f hours (%.2f%%)' %
151 191 (exposure_south/3600.0,
152 192 100.0*exposure_south/(exposure_south+exposure_north)))
153   - print("North array .......: %.2f hours (%.2f%%)" %
  193 + print('North array .......: %.2f hours (%.2f%%)' %
154 194 (exposure_north/3600.0,
155 195 100.0*exposure_north/(exposure_south+exposure_north)))
156 196  
... ... @@ -163,7 +203,23 @@ def set_patch(lmin=-30.0, lmax=30.0, bmin=-1.5, bmax=+1.5,
163 203 # =========================== #
164 204 def set_gps(separation=3.0, bmin=-1.3, bmax=1.3, lst=True):
165 205 """
166   - Setup Galactic plane survey.
  206 + Setup Galactic plane survey
  207 +
  208 + Parameters
  209 + ----------
  210 + separation : float, optional
  211 + Pointing separation (deg)
  212 + bmin : float, optional
  213 + Minimum Galactic Latitude (deg)
  214 + bmax : float, optional
  215 + Maximum Galactic Latitude (deg)
  216 + lst : bool, optional
  217 + Use LSTs
  218 +
  219 + Returns
  220 + -------
  221 + obsdef : list of dict
  222 + List of pointing definitions
167 223 """
168 224 # Initialise observation definition
169 225 obsdef = []
... ... @@ -171,27 +227,27 @@ def set_gps(separation=3.0, bmin=-1.3, bmax=1.3, lst=True):
171 227 # Add inner region South
172 228 obsdef.extend(set_patch(lmin=-60.0, lmax=60.0, bmin=bmin, bmax=bmax,
173 229 separation=separation, hours=780,
174   - site="South", lst=lst))
  230 + site='South', lst=lst))
175 231  
176 232 # Add Vela & Carina region
177 233 obsdef.extend(set_patch(lmin=240.0, lmax=300.0, bmin=bmin, bmax=bmax,
178 234 separation=separation, hours=180,
179   - site="South", lst=lst))
  235 + site='South', lst=lst))
180 236  
181 237 # Add 210-240 region
182 238 obsdef.extend(set_patch(lmin=210.0, lmax=240.0, bmin=bmin, bmax=bmax,
183 239 separation=separation, hours=60,
184   - site="South", lst=lst))
  240 + site='South', lst=lst))
185 241  
186 242 # Add Cygnus, Perseus
187 243 obsdef.extend(set_patch(lmin=60.0, lmax=150.0, bmin=bmin, bmax=bmax,
188 244 separation=separation, hours=450,
189   - site="North", lst=lst))
  245 + site='North', lst=lst))
190 246  
191 247 # Add Anticentre
192 248 obsdef.extend(set_patch(lmin=150.0, lmax=210.0, bmin=bmin, bmax=bmax,
193 249 separation=separation, hours=150,
194   - site="North", lst=lst))
  250 + site='North', lst=lst))
195 251  
196 252 # Return observation definition
197 253 return obsdef
... ... @@ -203,6 +259,18 @@ def set_gps(separation=3.0, bmin=-1.3, bmax=1.3, lst=True):
203 259 def set_extgal(separation=3.0, lst=True):
204 260 """
205 261 Setup Extragalactic survey.
  262 +
  263 + Parameters
  264 + ----------
  265 + separation : float, optional
  266 + Pointing separation (deg)
  267 + lst : bool, optional
  268 + Use LSTs
  269 +
  270 + Returns
  271 + -------
  272 + obsdef : list of dict
  273 + List of pointing definitions
206 274 """
207 275 # Initialise observation definition
208 276 obsdef = []
... ... @@ -210,7 +278,7 @@ def set_extgal(separation=3.0, lst=True):
210 278 # Set patch
211 279 obsdef.extend(set_patch(lmin=-90.0, lmax=90.0, bmin=+5.0, bmax=+88.0,
212 280 separation=separation, hours=1000,
213   - site="Automatic", lst=lst, autodec=10.0))
  281 + site='Automatic', lst=lst, autodec=10.0))
214 282  
215 283 # Return observation definition
216 284 return obsdef
... ... @@ -222,6 +290,16 @@ def set_extgal(separation=3.0, lst=True):
222 290 def set_gc(lst=True):
223 291 """
224 292 Setup Galactic centre KSP.
  293 +
  294 + Parameters
  295 + ----------
  296 + lst : bool, optional
  297 + Use LSTs
  298 +
  299 + Returns
  300 + -------
  301 + obsdef : list of dict
  302 + List of pointing definitions
225 303 """
226 304 # Initialise observation definition
227 305 obsdef = []
... ... @@ -229,12 +307,12 @@ def set_gc(lst=True):
229 307 # Central wobble
230 308 obsdef.extend(set_patch(lmin=-0.5, lmax=0.5, bmin=-0.5, bmax=0.5,
231 309 separation=0.1, hours=525,
232   - site="South", lst=lst))
  310 + site='South', lst=lst))
233 311  
234 312 # Extended region
235 313 obsdef.extend(set_patch(lmin=-10.0, lmax=10.0, bmin=-10.0, bmax=10.0,
236 314 separation=1.5, hours=300,
237   - site="South", lst=lst))
  315 + site='South', lst=lst))
238 316  
239 317 # Return observation definition
240 318 return obsdef
... ... @@ -246,6 +324,18 @@ def set_gc(lst=True):
246 324 def set_lmc(hours=250.0, lst=True):
247 325 """
248 326 Setup LMC KSP.
  327 +
  328 + Parameters
  329 + ----------
  330 + hours : float, optional
  331 + Total observation duration (h)
  332 + lst : bool, optional
  333 + Use LSTs
  334 +
  335 + Returns
  336 + -------
  337 + obsdef : list of dict
  338 + List of pointing definitions
249 339 """
250 340 # Initialise observation definition
251 341 obsdef = []
... ... @@ -275,11 +365,11 @@ def set_lmc(hours=250.0, lst=True):
275 365 obs = {'lon': lon, 'lat': lat, 'duration': duration}
276 366  
277 367 # Add IRF
278   - caldb = "prod2"
  368 + caldb = 'prod2'
279 369 if lst:
280   - irf = "South_50h"
  370 + irf = 'South_50h'
281 371 else:
282   - irf = "South_50h"
  372 + irf = 'South_50h'
283 373 obs['caldb'] = caldb
284 374 obs['irf'] = irf
285 375  
... ... @@ -287,7 +377,7 @@ def set_lmc(hours=250.0, lst=True):
287 377 obsdef.append(obs)
288 378  
289 379 # Dump statistics
290   - print("Number of pointings: %d (%.2f sec)" % (n_pnt,duration))
  380 + print('Number of pointings: %d (%.2f s)' % (n_pnt,duration))
291 381  
292 382 # Return observation definition
293 383 return obsdef
... ... @@ -298,31 +388,38 @@ def set_lmc(hours=250.0, lst=True):
298 388 # ======================================= #
299 389 def write_obsdef(filename, obsdef):
300 390 """
301   - Write observation definition file.
  391 + Write observation definition file
  392 +
  393 + Parameters
  394 + ----------
  395 + filename : str
  396 + Observation definition file name
  397 + obsdef : list of dict
  398 + List of pointing definitions
302 399 """
303 400 # Open file
304 401 file = open(filename, 'w')
305 402  
306 403 # Write header
307   - file.write("ra,dec,duration,caldb,irf\n")
  404 + file.write('ra,dec,duration,caldb,irf\n')
308 405  
309 406 # Loop over pointings
310 407 for obs in obsdef:
311 408  
312 409 # If we have lon,lat then convert into RA,Dec
313   - if "lon" in obs and "lat" in obs:
314   - lon = obs["lon"]
315   - lat = obs["lat"]
  410 + if 'lon' in obs and 'lat' in obs:
  411 + lon = obs['lon']
  412 + lat = obs['lat']
316 413 dir = gammalib.GSkyDir()
317 414 dir.lb_deg(lon,lat)
318 415 ra = dir.ra_deg()
319 416 dec = dir.dec_deg()
320 417 else:
321   - ra = obs["ra"]
322   - dec = obs["dec"]
  418 + ra = obs['ra']
  419 + dec = obs['dec']
323 420  
324 421 # Write information
325   - file.write("%8.4f,%8.4f,%.4f,%s,%s\n" %
  422 + file.write('%8.4f,%8.4f,%.4f,%s,%s\n' %
326 423 (ra, dec, obs['duration'], obs['caldb'], obs['irf']))
327 424  
328 425 # Close file
... ... @@ -342,7 +439,7 @@ if __name__ == '__main__':
342 439  
343 440 # Test for command line arguments
344 441 if (len(sys.argv) > 1):
345   - if sys.argv[1] == "-h":
  442 + if sys.argv[1] == '-h':
346 443 need_help = True
347 444 else:
348 445 obsname = sys.argv[1]
... ... @@ -351,37 +448,37 @@ if __name__ == '__main__':
351 448  
352 449 # Print help if needed and exit
353 450 if need_help:
354   - print("Usage: make_pointing.py [OPTIONS]")
355   - print(" -h Display this usage message")
356   - print(" gps Galactic plane survey (2 row scheme)")
357   - print(" gps3 Galactic plane survey (3 row scheme)")
358   - print(" extgal Extragalactic survey")
359   - print(" gc Galactic centre survey")
360   - print(" lmc LMC survey")
  451 + print('Usage: make_pointing.py [OPTIONS]')
  452 + print(' -h Display this usage message')
  453 + print(' gps Galactic plane survey (2 row scheme)')
  454 + print(' gps3 Galactic plane survey (3 row scheme)')
  455 + print(' extgal Extragalactic survey')
  456 + print(' gc Galactic centre survey')
  457 + print(' lmc LMC survey')
361 458 sys.exit()
362 459  
363 460 # Galactic plane survey
364   - if obsname == "gps":
  461 + if obsname == 'gps':
365 462 obsdef = set_gps(lst=True)
366   - write_obsdef("gps.dat", obsdef)
367   - elif obsname == "gps3":
  463 + write_obsdef('gps.dat', obsdef)
  464 + elif obsname == 'gps3':
368 465 obsdef = set_gps(separation=1.5, lst=True)
369   - write_obsdef("gps3.dat", obsdef)
  466 + write_obsdef('gps3.dat', obsdef)
370 467  
371 468 # Extragalactic survey
372   - elif obsname == "extgal":
  469 + elif obsname == 'extgal':
373 470 obsdef = set_extgal(lst=True)
374   - write_obsdef("extgal.dat", obsdef)
  471 + write_obsdef('extgal.dat', obsdef)
375 472  
376 473 # Galactic centre
377   - elif obsname == "gc":
  474 + elif obsname == 'gc':
378 475 obsdef = set_gc(lst=True)
379   - write_obsdef("gc.dat", obsdef)
  476 + write_obsdef('gc.dat', obsdef)
380 477  
381 478 # LMC
382   - elif obsname == "lmc":
  479 + elif obsname == 'lmc':
383 480 obsdef = set_lmc(lst=True)
384   - write_obsdef("lmc.dat", obsdef)
  481 + write_obsdef('lmc.dat', obsdef)
385 482  
386 483 # Invalid pattern
387 484 else:
... ...
test/test_examples.py
... ... @@ -44,21 +44,33 @@ class Test(gammalib.GPythonTestSuite):
44 44 return
45 45  
46 46 # Execute Python script
47   - def _execute_python(self, name):
  47 + def _execute_python(self, name, args='', log=''):
48 48 """
49 49 Execute Python script
50 50  
51 51 Parameters
52 52 ----------
53 53 name : str
54   - Python script name with .py extension.
  54 + Python script name with .py extension
  55 + args : str, optional
  56 + String with arguments to be passed to script
  57 + log : str, optional
  58 + String with name to be used for logging
55 59 """
56 60 # Setup command
57 61 cmd = '../examples/' + name + '.py'
  62 + if len(args) > 0:
  63 + cmd += ' ' + args
  64 +
  65 + # Set logname
  66 + if len(log) > 0:
  67 + logname = log
  68 + else:
  69 + logname = name
58 70  
59 71 # Execute Python script, make sure we catch any exception
60 72 try:
61   - rc = os.system(cmd+' > example_'+name+'.log 2>&1')
  73 + rc = os.system(cmd+' > example_'+logname+'.log 2>&1')
62 74 except:
63 75 pass
64 76  
... ... @@ -78,6 +90,7 @@ class Test(gammalib.GPythonTestSuite):
78 90  
79 91 # Append tests
80 92 self.append(self.test_generate_prod3_irfs, 'Test generate_prod3_irfs')
  93 + self.append(self.test_make_pointings, 'Test make_pointings')
81 94 self.append(self.test_pipeline_binned_disk, 'Test pipeline_binned_disk')
82 95 self.append(self.test_pipeline_binned_mem, 'Test pipeline_binned_mem')
83 96 self.append(self.test_pipeline_stacked_disk, 'Test pipeline_stacked_disk')
... ... @@ -88,6 +101,27 @@ class Test(gammalib.GPythonTestSuite):
88 101 # Return
89 102 return
90 103  
  104 + # Test make_pointings
  105 + def test_make_pointings(self):
  106 + """
  107 + Test make_pointings
  108 + """
  109 + # Set script name
  110 + script = 'make_pointings'
  111 +
  112 + # Execute script
  113 + self._execute_python(script, args='-h', log='make_pointings_help')
  114 + self._execute_python(script, args='gps', log='make_pointings_gps')
  115 + self._execute_python(script, args='gps3', log='make_pointings_gps3')
  116 + self._execute_python(script, args='extgal', log='make_pointings_extgal')
  117 + self._execute_python(script, args='gc', log='make_pointings_gc')
  118 + self._execute_python(script, args='lmc', log='make_pointings_lmc')
  119 +
  120 + #TODO: Do any testing
  121 +
  122 + # Return
  123 + return
  124 +
91 125 # Test generate_prod3_irfs
92 126 def test_generate_prod3_irfs(self):
93 127 """
... ...