gstlal-inspiral  1.9.3
 All Classes Namespaces Files Functions
gstlal_inspiral_lvalert_psd_plotter
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 #
3 # Copyright (C) 2013--2015 Kipp Cannon
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2 of the License, or (at your
8 # option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19 
20 ### A program to listen to lvalerts, download the psd from gstlal gracedb events, plot it, and upload the results
21 
22 
23 #
24 # =============================================================================
25 #
26 # Preamble
27 #
28 # =============================================================================
29 #
30 
31 
32 import logging
33 from optparse import OptionParser
34 import os.path
35 import sys
36 import urllib.parse as urlparse
37 import json
38 
39 
40 import lal
41 
42 
43 from glue.ligolw import utils as ligolw_utils
44 from gstlal import lvalert_helper
45 from gstlal.plots import psd as plotpsd
46 from ligo.gracedb import rest as gracedb
47 
48 
49 import matplotlib
50 matplotlib.rcParams.update({
51  "font.size": 10.0,
52  "axes.titlesize": 10.0,
53  "axes.labelsize": 10.0,
54  "xtick.labelsize": 8.0,
55  "ytick.labelsize": 8.0,
56  "legend.fontsize": 8.0,
57  "figure.dpi": 100,
58  "savefig.dpi": 100,
59  "text.usetex": True,
60  "path.simplify": True
61 })
62 
63 
64 #
65 # =============================================================================
66 #
67 # Library
68 #
69 # =============================================================================
70 #
71 
72 
73 def get_psds(gracedb_client, graceid, filename = "psd.xml.gz", ignore_404 = False):
74  response = lvalert_helper.get_filename(gracedb_client, graceid, filename = filename, ignore_404 = ignore_404)
75  if response is None:
76  return None
77  return lal.series.read_psd_xmldoc(ligolw_utils.load_fileobj(response, contenthandler = lal.series.PSDContentHandler)[0])
78 
79 
80 #
81 # =============================================================================
82 #
83 # Command Line
84 #
85 # =============================================================================
86 #
87 
88 
89 def parse_command_line():
90  parser = OptionParser()
91  parser.add_option("--format", default = "png", help = "Set file format by selecting the extention (default = \"png\").")
92  parser.add_option("--no-upload", action = "store_true", help = "Write plots to disk.")
93  parser.add_option("--skip-404", action = "store_true", help = "Skip events that give 404 (file not found) errors (default is to abort).")
94  parser.add_option("--gracedb-service-url", default="%s" % gracedb.DEFAULT_SERVICE_URL, help = "GraceDb service url to upload to (default: %s)" % gracedb.DEFAULT_SERVICE_URL)
95  parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
96  options, graceids = parser.parse_args()
97 
98  if not graceids:
99  # FIXME: lvalert_listen doesn't allow command-line
100  # options, enable logging for online analysis
101  options.verbose = True
102 
103  # can only call basicConfig once (otherwise need to switch to more
104  # complex logging configuration)
105  if options.verbose:
106  logging.basicConfig(format = "%(asctime)s:%(message)s", level = logging.INFO)
107  else:
108  logging.basicConfig(format = "%(asctime)s:%(message)s")
109 
110  return options, graceids
111 
112 
113 #
114 # =============================================================================
115 #
116 # Main
117 #
118 # =============================================================================
119 #
120 
121 
122 options, graceids = parse_command_line()
123 
124 
125 if not graceids:
126  lvalert_data = json.loads(sys.stdin.read())
127  logging.info("%(alert_type)s-type alert for event %(uid)s" % lvalert_data)
128  logging.info("lvalert data: %s" % repr(lvalert_data))
129  if "filename" in lvalert_data["data"]:
130  filename = os.path.split(urlparse.urlparse(lvalert_data["data"]["filename"]).path)[-1]
131  if filename not in (u"psd.xml.gz",):
132  logging.info("filename is not 'psd.xml.gz'. skipping")
133  sys.exit()
134  graceids = [str(lvalert_data["uid"])]
135  else:
136  logging.info("json key filename not in lvalert data, skipping")
137 
138 
139 gracedb_client = gracedb.GraceDb(service_url = options.gracedb_service_url)
140 
141 
142 for graceid in graceids:
143  psds = get_psds(gracedb_client, graceid, ignore_404 = options.skip_404)
144  if psds is None:
145  continue
146 
147  coinc_xmldoc = lvalert_helper.get_coinc_xmldoc(gracedb_client, graceid)
148 
149  #
150  # PSD plot
151  #
152 
153  fig = plotpsd.plot_psds(psds, coinc_xmldoc, plot_width = 800)
154  fig.tight_layout()
155 
156  filename = "%s_psd.%s" % (graceid, options.format)
157  if options.no_upload:
158  logging.info("writing %s ..." % filename)
159  fig.savefig(filename)
160  else:
161  lvalert_helper.upload_fig(fig, gracedb_client, graceid, filename = filename, log_message = "strain spectral density plot", tagname = "psd")
162 
163  #
164  # Cumulative SNRs plot
165  #
166 
167  fig = plotpsd.plot_cumulative_snrs(psds, coinc_xmldoc, plot_width = 800)
168  fig.tight_layout()
169 
170  filename = "%s_cumulative_snrs.%s" % (graceid, options.format)
171  if options.no_upload:
172  logging.info("writing %s ..." % filename)
173  fig.savefig(filename)
174  else:
175  lvalert_helper.upload_fig(fig, gracedb_client, graceid, filename = filename, log_message = "cumulative SNRs plot", tagname = "psd")
176 
177  logging.info("finished processing %s" % graceid)