3 # Copyright (C) 2013 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 check for gaps in frame cache files according to segment lists
23 # =============================================================================
27 # =============================================================================
31 from optparse
import OptionParser
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
44 class XMLContentHandler(ligolw.LIGOLWContentHandler):
46 lsctables.use_in(XMLContentHandler)
50 # =============================================================================
54 # =============================================================================
58 def parse_command_line():
59 parser = OptionParser(
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).")
65 options, filenames = parser.parse_args()
67 required = ("segments_file", "segments_name")
68 missing = [opt for opt in required if getattr(options, opt) is None]
70 raise ValueError("missing options: %s" % ", ".join("--%s" % opt.replace("_", "-") for opt in missing))
72 return options, filenames or [None]
76 # =============================================================================
80 # =============================================================================
89 options, filenames = parse_command_line()
93 # load segments and subtract time spanned by cache files
97 if options.segments_file.lower().endswith(".sqlite"):
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()
102 seglists = ligolw_segments.segmenttable_get_by_name(utils.load_filename(options.segments_file, contenthandler = XMLContentHandler, verbose = options.verbose), options.segments_name).coalesce()
105 for filename in filenames:
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
113 # check for remainders
117 if any(seglists.values()):
118 print >>sys.stderr, "gaps found in cache. total missing time: %s" % str(abs(seglists))
121 print >>sys.stderr, "cache is complete"