gstlal  0.8.1
 All Classes Namespaces Files Functions Variables Pages
gstlal_ligo_data_find_check
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
20 # A program to check for gaps in frame cache files according to segment lists
21 
22 #
23 # =============================================================================
24 #
25 # Preamble
26 #
27 # =============================================================================
28 #
29 
30 
31 from optparse import OptionParser
32 import sqlite3
33 import sys
34 
35 
36 from glue import lal
37 from glue.ligolw import ligolw
38 from glue.ligolw import lsctables
39 from glue.ligolw import dbtables
40 from glue.ligolw import utils
41 from glue.ligolw.utils import segments as ligolw_segments
42 
43 
44 class XMLContentHandler(ligolw.LIGOLWContentHandler):
45  pass
46 lsctables.use_in(XMLContentHandler)
47 
48 
49 #
50 # =============================================================================
51 #
52 # Command Line
53 #
54 # =============================================================================
55 #
56 
57 
58 def parse_command_line():
59  parser = OptionParser(
60  )
61  parser.add_option("-s", "--segments-file", metavar = "filename", help = "Load segment lists from this .xml file (required).")
62  parser.add_option("-n", "--segments-name", metavar = "name", help = "Load this segment list from the file (required).")
63  parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose (optional).")
64 
65  options, filenames = parser.parse_args()
66 
67  required = ("segments_file", "segments_name")
68  missing = [opt for opt in required if getattr(options, opt) is None]
69  if missing:
70  raise ValueError("missing options: %s" % ", ".join("--%s" % opt.replace("_", "-") for opt in missing))
71 
72  return options, filenames or [None]
73 
74 
75 #
76 # =============================================================================
77 #
78 # Main
79 #
80 # =============================================================================
81 #
82 
83 
84 #
85 # parse command line
86 #
87 
88 
89 options, filenames = parse_command_line()
90 
91 
92 #
93 # load segments and subtract time spanned by cache files
94 #
95 
96 
97 if options.segments_file.lower().endswith(".sqlite"):
98  if options.verbose:
99  print >>sys.stderr, "reading %s ..." % options.segments_file
100  seglists = ligolw_segments.segmenttable_get_by_name(dbtables.get_xml(sqlite3.connect(options.segments_file)), options.segments_name).coalesce()
101 else:
102  seglists = ligolw_segments.segmenttable_get_by_name(utils.load_filename(options.segments_file, contenthandler = XMLContentHandler, verbose = options.verbose), options.segments_name).coalesce()
103 
104 
105 for filename in filenames:
106  if options.verbose:
107  print >>sys.stderr, "loading %s ..." % (filename or "stdin")
108  for line in open(filename) if filename else sys.stdin:
109  seglists -= lal.CacheEntry(line).segmentlistdict
110 
111 
112 #
113 # check for remainders
114 #
115 
116 
117 if any(seglists.values()):
118  print >>sys.stderr, "gaps found in cache. total missing time: %s" % str(abs(seglists))
119  sys.exit(1)
120 if options.verbose:
121  print >>sys.stderr, "cache is complete"