gstlal-inspiral  1.9.2
 All Classes Namespaces Files Functions
gstlal_inspiral_lvalert_omegascan
1 #!/usr/bin/env python3
2 #
3 # Copyright (C) 2015 Chad Hanna
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
20 # A program to request an omega scan from ligodv web based on gracedb submissions notified by lvalert
21 
22 import subprocess
23 import os
24 import re
25 import json
26 import sys
27 import tempfile
28 import urllib
29 import urllib.parse as urlparse
30 import logging
31 import time
32 from optparse import OptionParser
33 import lal
34 from lal import LIGOTimeGPS
35 from glue.ligolw import ligolw
36 from glue.ligolw import table
37 from glue.ligolw import lsctables
38 from glue.ligolw import utils as ligolw_utils
39 from gstlal import lvalert_helper
40 from ligo.gracedb import rest as gracedb
41 """
42 Usage: gstlal_inspiral_lvalert_omegascan <albert.einstein> <GPS TIME>
43 """
44 
45 # define a content handler
46 class LIGOLWContentHandler(ligolw.LIGOLWContentHandler):
47  pass
48 lsctables.use_in(LIGOLWContentHandler)
49 
50 
51 def now():
52  return LIGOTimeGPS(lal.UTCToGPS(time.gmtime()), 0)
53 
54 
55 def parse_command_line():
56  parser = OptionParser()
57  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)
58  parser.add_option("--channel-name", action = "append", default = ["H1:GDS-CALIB_STRAIN", "L1:GDS-CALIB_STRAIN"], help = 'Chanel names, e.g., H1:GDS-CALIB_STRAIN. Can be given more than once. Default to ["H1:GDS-CALIB_STRAIN", "L1:GDS-CALIB_STRAIN"]')
59  parser.add_option("--duration", type = int, default=10, help="Omega scan duration in seconds. Default=10")
60  parser.add_option("--albert-einstein")
61 
62  options, gid_list = parser.parse_args()
63 
64  if len(gid_list) > 1:
65  raise ValueError("%d graceids specified, no more than one allowed" % len(gid_list))
66 
67  if len(gid_list) == 0:
68  lvalert_data = json.loads(sys.stdin.read())
69  logging.info("%(alert_type)s-type alert for event %(uid)s" % lvalert_data)
70  logging.info("lvalert data: %s" % repr(lvalert_data))
71  if "filename" in lvalert_data["data"]:
72  filename = os.path.split(urlparse.urlparse(lvalert_data["file"]).path)[-1]
73  if filename not in (u"psd.xml.gz",):
74  logging.info("filename is not 'psd.xml.gz'. skipping")
75  sys.exit()
76  gid = str(lvalert_data["uid"])
77  else:
78  logging.info("json key filename not in lvalert data, skipping")
79  else:
80  gid = gid_list[0]
81 
82  return options, gid
83 
84 
85 options, graceid = parse_command_line()
86 
87 
88 def curl(url, outfile):
89  subprocess.check_call(["curl", "--insecure", "-c", "/tmp/${USER}_cookies", "-b", "/tmp/${USER}_cookies", "--negotiate", "--user", ":", "--location-trusted", "-o", outfile, url])
90 
91 
92 # Connect to gracedb and extract the coinc document
93 gracedb_client = gracedb.GraceDb(service_url = options.gracedb_service_url)
94 coinc_xmldoc = lvalert_helper.get_coinc_xmldoc(gracedb_client, graceid)
95 coinc_inspiral, = lsctables.CoincInspiralTable.get_table(coinc_xmldoc)
96 gps = coinc_inspiral.get_end()
97 
98 # Wait a little while in the hopes that NDS2 has caught up
99 if abs(now() - gps) < 180:
100  time.sleep(180)
101 
102 if not options.albert_einstein:
103  options.albert_einstein = os.environ["KERBEROS_USER"]
104 
105 # Kinit - if a keytab exists use it: User must set the KERBEROS_KEYTAB variable.
106 # FIXME is there a standard key tab env. variable?
107 if os.environ["KERBEROS_KEYTAB"]:
108  subprocess.check_call(["kinit", "%s@LIGO.ORG" % options.albert_einstein, "-k", "-t", os.environ["KERBEROS_KEYTAB"]])
109 else:
110  subprocess.check_call(["kinit", "%s@LIGO.ORG" % options.albert_einstein])
111 
112 
113 # FIXME don't hard code service URL, expose more of Joe's API?
114 url = "https://ldvw.ligo.caltech.edu/ldvw/view?act=doplot&chanName=%s&strtTime=%s&duration=%s&doWplot=1" % ("&chanName=".join(options.channel_name), str(gps), str(options.duration))
115 
116 
117 # Request that figures be made
118 outfile = tempfile.NamedTemporaryFile().name
119 curl(url, outfile)
120 
121 
122 # grep for the figures and download them too
123 patt = re.compile('.*imgId=(?P<id>[0-9]*).*')
124 for l in open(outfile):
125  m = patt.match(l)
126  if m:
127  print(l)
128  imgid = m.group("id")
129  url = "https://ldvw.ligo.caltech.edu/ldvw/view?act=getImg&imgId=%s" % imgid
130  outfile = "ldvw_%s.png" % imgid
131  curl(url, outfile)
132  lvalert_helper.upload_file(gracedb_client, graceid, outfile, log_message = "Omega Scan", tagname = "data_quality")
133  os.remove(outfile)