3 # Copyright (C) 2013--2015 Kipp Cannon
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.
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.
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.
20 ### A program to listen to lvalerts, download the psd from gstlal gracedb events, plot it, and upload the results
24 # =============================================================================
28 # =============================================================================
33 from optparse
import OptionParser
36 import urllib.parse as urlparse
44 from glue.ligolw
import utils as ligolw_utils
45 from gstlal
import lvalert_helper
46 from gstlal.plots
import psd as plotpsd
47 from ligo.gracedb
import rest as gracedb
50 matplotlib.rcParams.update({
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,
65 # =============================================================================
69 # =============================================================================
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)
77 return lal.series.read_psd_xmldoc(ligolw_utils.load_fileobj(response, contenthandler = lal.series.PSDContentHandler)[0])
81 # =============================================================================
85 # =============================================================================
88 PLOT_DEFAULTS = [
'plots/H1L1-GSTLAL_INSPIRAL_PLOT_SENSITIVITY_ALL_LLOID_COMBINED_RANGE_VS_FAR_BINNED_BY_SOURCE_TYPE-*.png',
'plots/H1L1-GSTLAL_INSPIRAL_PLOT_SENSITIVITY_ALL_LLOID_COMBINED_VOLUME_VS_FAR_BINNED_BY_SOURCE_TYPE-*.png',
'plots/H1L1V1-GSTLAL_INSPIRAL_PLOTSUMMARY_ALL_LLOID_COMBINED_03_chi2_vs_rho_*1_openbox-*.png',
'plots/H1L1V1-GSTLAL_INSPIRAL_PLOTSUMMARY_ALL_LLOID_COMBINED_02_mchirp_acc_frac_scatter_*_*1_closedbox-*.png']
90 def parse_command_line():
91 parser = OptionParser()
92 parser.add_option("--skip-404", action = "store_true", help = "Skip events that give 404 (file not found) errors (default is to abort).")
93 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)
94 parser.add_option("--plot-glob-pattern", default = PLOT_DEFAULTS, action = "append", help = "find plots matching these patterns on disk and upload them to an event")
95 parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
96 options, graceids = parser.parse_args()
99 # FIXME: lvalert_listen doesn't allow command-line
100 # options, enable logging for online analysis
101 options.verbose = True
103 # can only call basicConfig once (otherwise need to switch to more
104 # complex logging configuration)
106 logging.basicConfig(format =
"%(asctime)s:%(message)s", level = logging.INFO)
108 logging.basicConfig(format =
"%(asctime)s:%(message)s")
110 return options, graceids
114 # =============================================================================
118 # =============================================================================
122 options, graceids = parse_command_line()
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")
134 graceids = [str(lvalert_data["uid"])]
136 logging.info("json key filename not in lvalert data, skipping")
139 gracedb_client = gracedb.GraceDb(service_url = options.gracedb_service_url)
142 for graceid in graceids:
144 for pat in options.plot_glob_pattern:
145 for filename in glob.glob(pat):
146 tmpfilename = "plot%d.png" % number
147 shutil.copyfile(filename, tmpfilename)
149 lvalert_helper.upload_file(gracedb_client, graceid, filename = tmpfilename, log_message = "Additional analysis plot", tagname = "background")
150 if tmpfilename != filename:
151 os.remove(tmpfilename)
153 logging.info("finished processing %s" % graceid)