gstlal-inspiral  1.9.3
 All Classes Namespaces Files Functions
lvalert_helper.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2013 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 ## @file gstlal_inspiral_lvalert_psd_plotter
20 # A program to listen to lvalerts, download the psd from gstlal gracedb events, plot it, and upload the results
21 #
22 # ### Command line interface
23 #
24 # + `--no-upload`: Write plots to disk.
25 # + `--skip-404`: Skip events that give 404 (file not found) errors (default is to abort).
26 # + `--verbose`: Be verbose.
27 #
28 # =============================================================================
29 #
30 # Preamble
31 #
32 # =============================================================================
33 #
34 
35 
36 import http.client
37 import logging
38 import os.path
39 import time
40 import io
41 
42 from glue.ligolw import ligolw
43 from glue.ligolw import array as ligolw_array
44 from glue.ligolw import param as ligolw_param
45 from glue.ligolw import lsctables
46 from glue.ligolw import utils as ligolw_utils
47 
48 
49 class LIGOLWContentHandler(ligolw.LIGOLWContentHandler):
50  pass
51 ligolw_array.use_in(LIGOLWContentHandler)
52 ligolw_param.use_in(LIGOLWContentHandler)
53 lsctables.use_in(LIGOLWContentHandler)
54 
55 
56 def get_filename(gracedb_client, graceid, filename, retries = 3, retry_delay = 10.0, ignore_404 = False):
57  for i in range(retries):
58  logging.info("retrieving \"%s\" for %s" % (filename, graceid))
59  response = gracedb_client.files(graceid, filename)
60  if response.status == http.client.OK:
61  return response
62  if response.status == http.client.NOT_FOUND and ignore_404:
63  logging.warning("retrieving \"%s\" for %s: (%d) %s. skipping ..." % (filename, graceid, response.status, response.reason))
64  return None
65  logging.warning("retrieving \"%s\" for %s: (%d) %s. pausing ..." % (filename, graceid, response.status, response.reason))
66  time.sleep(retry_delay)
67  raise Exception("retrieving \"%s\" for %s: (%d) %s" % (filename, graceid, response.status, response.reason))
68 
69 
70 def get_coinc_xmldoc(gracedb_client, graceid, filename = "coinc.xml"):
71  return ligolw_utils.load_fileobj(get_filename(gracedb_client, graceid, filename = filename), contenthandler = LIGOLWContentHandler)[0]
72 
73 
74 def upload_fig(fig, gracedb_client, graceid, filename, log_message, tagname = "psd"):
75  plotfile = io.StringIO()
76  fig.savefig(plotfile, format = os.path.splitext(filename)[-1][1:])
77  logging.info("uploading \"%s\" for %s" % (filename, graceid))
78  response = gracedb_client.writeLog(graceid, log_message, filename = filename, filecontents = plotfile.getvalue(), tagname = tagname)
79  if response.status != http.client.CREATED:
80  raise Exception("upload of \"%s\" for %s failed: %s" % (filename, graceid, response["error"]))
81 
82 
83 def upload_file(gracedb_client, graceid, filename, log_message = "A file", tagname = None):
84  logging.info("uploading \"%s\" for %s" % (filename, graceid))
85  response = gracedb_client.writeLog(graceid, log_message, filename = filename, filecontents = io.FileIO(filename).readall(), tagname = tagname)
86  if response.status != http.client.CREATED:
87  raise Exception("upload of \"%s\" for %s failed: %s" % (filename, graceid, response["error"]))
88 
89 
90 def upload_xmldoc(gracedb_client, graceid, filename, xmldoc, log_message = "A file", tagname = None):
91  logging.info("uploading \"%s\" for %s" % (filename, graceid))
92  output = io.StringIO()
93  ligolw_utils.write_fileobj(xmldoc, output)
94  response = gracedb_client.writeLog(graceid, log_message, filename = filename, filecontents = output.getvalue(), tagname = tagname)
95  output.close()
96  if response.status != http.client.CREATED:
97  raise Exception("upload of \"%s\" for %s failed: %s" % (filename, graceid, response["error"]))