Coverage for gwcelery/tasks/bayestar.py: 100%
31 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-11-14 05:52 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-11-14 05:52 +0000
1"""Rapid sky localization with :mod:`BAYESTAR <ligo.skymap.bayestar>`."""
2import io
3import logging
4import urllib.parse
6from celery.exceptions import Ignore
7from ligo.lw.utils import load_fileobj
8from ligo.skymap import bayestar as _bayestar
9from ligo.skymap.io import events, fits
11from .. import app
12from . import gracedb
14log = logging.getLogger('BAYESTAR')
17@app.task(queue='openmp', shared=False)
18def localize(coinc_psd, graceid, disabled_detectors=None):
19 """Generate a rapid sky localization using
20 :mod:`BAYESTAR <ligo.skymap.bayestar>`.
22 Parameters
23 ----------
24 coinc_psd : byte
25 contents of the input event's ``coinc.xml`` file that includes PSD.
26 graceid : str
27 The GraceDB ID, used for FITS metadata and recording log messages
28 to GraceDB.
29 disabled_detectors : list, optional
30 List of detectors to disable.
32 Returns
33 -------
34 bytes
35 The byte contents of the finished FITS file.
37 Notes
38 -----
39 This task is adapted from the command-line tool
40 :doc:`bayestar-localize-lvalert
41 <ligo.skymap:tool/bayestar_localize_lvalert>`.
43 It should execute in a special queue for computationally intensive,
44 multithreaded, OpenMP tasks.
46 """
47 # Determine the base URL for event pages.
48 scheme, netloc, *_ = urllib.parse.urlparse(gracedb.client.url)
49 base_url = urllib.parse.urlunparse((scheme, netloc, 'events', '', '', ''))
51 try:
52 # A little bit of Cylon humor
53 log.info('by your command...')
55 # Read the coinc.xml into a document
56 doc = load_fileobj(io.BytesIO(coinc_psd),
57 contenthandler=events.ligolw.ContentHandler)
59 # Parse event
60 event_source = events.ligolw.open(doc, psd_file=doc, coinc_def=None)
61 if disabled_detectors:
62 event_source = events.detector_disabled.open(
63 event_source, disabled_detectors)
64 event, = event_source.values()
66 # Run BAYESTAR
67 log.info('starting sky localization')
68 # FIXME: the low frequency cutoff should not be hardcoded.
69 # It should be provided in the coinc.xml file.
70 skymap = _bayestar.localize(event, f_low=15.0)
71 skymap.meta['objid'] = str(graceid)
72 skymap.meta['url'] = '{}/{}'.format(base_url, graceid)
73 log.info('sky localization complete')
75 with io.BytesIO() as f:
76 fits.write_sky_map(f, skymap, moc=True)
77 return f.getvalue()
78 except events.DetectorDisabledError:
79 raise Ignore()