Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""Rapid sky localization with :mod:`BAYESTAR <ligo.skymap.bayestar>`.""" 

2import io 

3import logging 

4import urllib.parse 

5 

6from celery.exceptions import Ignore 

7from glue.ligolw.utils import load_fileobj 

8from glue.ligolw.utils.ligolw_add import merge_ligolws 

9from ligo.skymap import bayestar as _bayestar 

10from ligo.skymap.io import events 

11from ligo.skymap.io import fits 

12 

13from .. import app 

14from . import gracedb 

15 

16log = logging.getLogger('BAYESTAR') 

17 

18 

19@app.task(queue='openmp', shared=False) 

20def localize(coinc_psd, graceid, filename='bayestar.fits.gz', 

21 disabled_detectors=None): 

22 """Generate a rapid sky localization using 

23 :mod:`BAYESTAR <ligo.skymap.bayestar>`. 

24 

25 Parameters 

26 ---------- 

27 coinc_psd : tuple 

28 Tuple consisting of the byte contents of the input event's 

29 ``coinc.xml`` and ``psd.xml.gz`` files. 

30 graceid : str 

31 The GraceDB ID, used for FITS metadata and recording log messages 

32 to GraceDB. 

33 filename : str, optional 

34 The name of the FITS file. 

35 disabled_detectors : list, optional 

36 List of detectors to disable. 

37 

38 Returns 

39 ------- 

40 bytes 

41 The byte contents of the finished FITS file. 

42 

43 Notes 

44 ----- 

45 This task is adapted from the command-line tool 

46 :doc:`bayestar-localize-lvalert 

47 <ligo/skymap/tool/bayestar_localize_lvalert>`. 

48 

49 It should execute in a special queue for computationally intensive, 

50 multithreaded, OpenMP tasks. 

51 

52 """ 

53 # Determine the base URL for event pages. 

54 scheme, netloc, *_ = urllib.parse.urlparse(gracedb.client.url) 

55 base_url = urllib.parse.urlunparse((scheme, netloc, 'events', '', '', '')) 

56 

57 try: 

58 # A little bit of Cylon humor 

59 log.info('by your command...') 

60 

61 # Combine coinc.xml and psd.xml.gz into one XML document 

62 doc = None 

63 for filecontents in coinc_psd: 

64 doc, _ = load_fileobj(io.BytesIO(filecontents), xmldoc=doc, 

65 contenthandler=events.ligolw.ContentHandler) 

66 merge_ligolws(doc) 

67 

68 # Parse event 

69 event_source = events.ligolw.open(doc, psd_file=doc, coinc_def=None) 

70 if disabled_detectors: 

71 event_source = events.detector_disabled.open( 

72 event_source, disabled_detectors) 

73 event, = event_source.values() 

74 

75 # Run BAYESTAR 

76 log.info('starting sky localization') 

77 # FIXME: the low frequency cutoff should not be hardcoded. 

78 # It should be provided in the coinc.xml file. 

79 skymap = _bayestar.localize(event, f_low=15.0) 

80 skymap.meta['objid'] = str(graceid) 

81 skymap.meta['url'] = '{}/{}'.format(base_url, graceid) 

82 log.info('sky localization complete') 

83 

84 with io.BytesIO() as f: 

85 fits.write_sky_map(f, skymap, moc=True) 

86 return f.getvalue() 

87 except events.DetectorDisabledError: 

88 raise Ignore()