Commit 3cc46b2652ddb7d07e48f32b00cfcdde324842d1
1 parent
479bb6d0
Use setdefault() to set dictionary default values in obsutils
Showing
1 changed file
with
23 additions
and
58 deletions
cscripts/obsutils.py
| ... | ... | @@ -198,7 +198,7 @@ def sim(obs, log=False, debug=False, chatter=2, edisp=False, seed=0, |
| 198 | 198 | # ======================= # |
| 199 | 199 | def set_obs(pntdir, tstart=0.0, duration=1800.0, deadc=0.95, \ |
| 200 | 200 | emin=0.1, emax=100.0, rad=5.0, \ |
| 201 | - irf='South_50h', caldb='prod2', id='000000'): | |
| 201 | + irf='South_50h', caldb='prod2', obsid='000000'): | |
| 202 | 202 | """ |
| 203 | 203 | Set a single CTA observation |
| 204 | 204 | |
| ... | ... | @@ -211,9 +211,9 @@ def set_obs(pntdir, tstart=0.0, duration=1800.0, deadc=0.95, \ |
| 211 | 211 | pntdir : `~gammalib.GSkyDir` |
| 212 | 212 | Pointing direction |
| 213 | 213 | tstart : float, optional |
| 214 | - Start time (seconds) | |
| 214 | + Start time (s) | |
| 215 | 215 | duration : float, optional |
| 216 | - Duration of observation (seconds) | |
| 216 | + Duration of observation (s) | |
| 217 | 217 | deadc : float, optional |
| 218 | 218 | Deadtime correction factor |
| 219 | 219 | emin : float, optional |
| ... | ... | @@ -226,7 +226,7 @@ def set_obs(pntdir, tstart=0.0, duration=1800.0, deadc=0.95, \ |
| 226 | 226 | Instrument response function |
| 227 | 227 | caldb : str, optional |
| 228 | 228 | Calibration database path |
| 229 | - id : str, optional | |
| 229 | + obsid : str, optional | |
| 230 | 230 | Observation identifier |
| 231 | 231 | |
| 232 | 232 | Returns |
| ... | ... | @@ -278,7 +278,7 @@ def set_obs(pntdir, tstart=0.0, duration=1800.0, deadc=0.95, \ |
| 278 | 278 | obs_cta.ontime(duration) |
| 279 | 279 | obs_cta.livetime(duration*deadc) |
| 280 | 280 | obs_cta.deadc(deadc) |
| 281 | - obs_cta.id(id) | |
| 281 | + obs_cta.id(obsid) | |
| 282 | 282 | |
| 283 | 283 | # Return CTA observation |
| 284 | 284 | return obs_cta |
| ... | ... | @@ -305,9 +305,9 @@ def set_obs_list(obsdeflist, tstart=0.0, duration=1800.0, deadc=0.95, \ |
| 305 | 305 | obsdeflist : list of dict |
| 306 | 306 | Observation definition list |
| 307 | 307 | tstart : float, optional |
| 308 | - Start time (seconds) | |
| 308 | + Start time (s) | |
| 309 | 309 | duration : float, optional |
| 310 | - Duration of observation (seconds) | |
| 310 | + Duration of observation (s) | |
| 311 | 311 | deadc : float, optional |
| 312 | 312 | Deadtime correction factor |
| 313 | 313 | emin : float, optional |
| ... | ... | @@ -340,61 +340,26 @@ def set_obs_list(obsdeflist, tstart=0.0, duration=1800.0, deadc=0.95, \ |
| 340 | 340 | pntdir = gammalib.GSkyDir() |
| 341 | 341 | pntdir.radec_deg(obsdef['ra'], obsdef['dec']) |
| 342 | 342 | |
| 343 | - # Set duration (use default if not found in definition list) | |
| 344 | - if 'duration' in obsdef: | |
| 345 | - obs_duration = obsdef['duration'] | |
| 346 | - else: | |
| 347 | - obs_duration = duration | |
| 348 | - | |
| 349 | - # Set emin (use default if not found in definition list) | |
| 350 | - if 'emin' in obsdef: | |
| 351 | - obs_emin = obsdef['emin'] | |
| 352 | - else: | |
| 353 | - obs_emin = emin | |
| 354 | - | |
| 355 | - # Set emax (use default if not found in definition list) | |
| 356 | - if 'emax' in obsdef: | |
| 357 | - obs_emax = obsdef['emax'] | |
| 358 | - else: | |
| 359 | - obs_emax = emax | |
| 360 | - | |
| 361 | - # Set radius (use default if not found in definition list) | |
| 362 | - if 'rad' in obsdef: | |
| 363 | - obs_rad = obsdef['rad'] | |
| 364 | - else: | |
| 365 | - obs_rad = rad | |
| 366 | - | |
| 367 | - # Set deadc (use default if not found in definition list) | |
| 368 | - if 'deadc' in obsdef: | |
| 369 | - obs_deadc = obsdef['deadc'] | |
| 370 | - else: | |
| 371 | - obs_deadc = deadc | |
| 372 | - | |
| 373 | - # Set caldb (use default if not found in definition list) | |
| 374 | - if 'caldb' in obsdef: | |
| 375 | - obs_caldb = obsdef['caldb'] | |
| 376 | - else: | |
| 377 | - obs_caldb = caldb | |
| 378 | - | |
| 379 | - # Set irf (use default if not found in definition list) | |
| 380 | - if 'irf' in obsdef: | |
| 381 | - obs_irf = obsdef['irf'] | |
| 382 | - else: | |
| 383 | - obs_irf = irf | |
| 384 | - | |
| 385 | 343 | # Generate identifier string |
| 386 | - id = '%6.6d' % obs_id | |
| 387 | - | |
| 388 | - # Set CTA observation | |
| 389 | - obs_cta = set_obs(pntdir, tstart=obs_start, duration=obs_duration, \ | |
| 390 | - deadc=obs_deadc, emin=obs_emin, emax=obs_emax, \ | |
| 391 | - rad=obs_rad, irf=obs_irf, caldb=obs_caldb, id=id) | |
| 344 | + obsid = '%6.6d' % obs_id | |
| 345 | + | |
| 346 | + # Set one CTA observation | |
| 347 | + obs_cta = set_obs(pntdir, | |
| 348 | + tstart=obs_start, | |
| 349 | + duration=obsdef.setdefault('duration', duration), | |
| 350 | + deadc=obsdef.setdefault('deadc', deadc), | |
| 351 | + emin=obsdef.setdefault('emin', emin), | |
| 352 | + emax=obsdef.setdefault('emax', emax), | |
| 353 | + rad=obsdef.setdefault('rad', rad), | |
| 354 | + irf=obsdef.setdefault('irf', irf), | |
| 355 | + caldb=obsdef.setdefault('caldb', caldb), | |
| 356 | + obsid=obsid) | |
| 392 | 357 | |
| 393 | 358 | # Append to container |
| 394 | 359 | obs.append(obs_cta) |
| 395 | 360 | |
| 396 | 361 | # Update start time and identifier |
| 397 | - obs_start += obs_duration | |
| 362 | + obs_start += duration | |
| 398 | 363 | obs_id += 1 |
| 399 | 364 | |
| 400 | 365 | # Return observation container |
| ... | ... | @@ -473,9 +438,9 @@ def set_observations(ra, dec, rad, tstart, duration, emin, emax, irf, caldb, |
| 473 | 438 | rad : float |
| 474 | 439 | ROI radius used for analysis (deg) |
| 475 | 440 | tstart : float |
| 476 | - Start time of observation (seconds) | |
| 441 | + Start time of observation (s) | |
| 477 | 442 | duration : float |
| 478 | - Duration of each observation (seconds) | |
| 443 | + Duration of each observation (s) | |
| 479 | 444 | emin : float, optional |
| 480 | 445 | Minimum event energy (TeV) |
| 481 | 446 | emax : float, optional | ... | ... |