3 # Copyright (C) 2015 Chad Hanna
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 request an omega scan from ligodv web based on gracedb submissions notified by lvalert
29 import urllib.parse as urlparse
32 from optparse
import OptionParser
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
42 Usage: gstlal_inspiral_lvalert_omegascan <albert.einstein> <GPS TIME>
45 # define a content handler
46 class LIGOLWContentHandler(ligolw.LIGOLWContentHandler):
48 lsctables.use_in(LIGOLWContentHandler)
52 return LIGOTimeGPS(lal.UTCToGPS(time.gmtime()), 0)
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")
62 options, gid_list = parser.parse_args()
65 raise ValueError("%d graceids specified, no more than one allowed" % len(gid_list))
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")
76 gid = str(lvalert_data["uid"])
78 logging.info("json key filename not in lvalert data, skipping")
85 options, graceid = parse_command_line()
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])
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()
98 # Wait a little while in the hopes that NDS2 has caught up
99 if abs(now() - gps) < 180:
102 if not options.albert_einstein:
103 options.albert_einstein = os.environ["KERBEROS_USER"]
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"]])
110 subprocess.check_call(["kinit", "%s@LIGO.ORG" % options.albert_einstein])
113 # FIXME don't hard code service URL, expose more of Joe's API?
117 # Request that figures be made
118 outfile = tempfile.NamedTemporaryFile().name
122 # grep for the figures and download them too
123 patt = re.compile('.*imgId=(?P<id>[0-9]*).*')
124 for l in open(outfile):
128 imgid = m.group("id")
130 outfile = "ldvw_%s.png" % imgid
132 lvalert_helper.upload_file(gracedb_client, graceid, outfile, log_message = "Omega Scan", tagname = "data_quality")