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
36 from lal.utils import CacheEntry
39 from glue.ligolw import dbtables
40 from glue.ligolw import utils as ligolw_utils
41 from glue.ligolw.utils import segments as ligolw_segments
45 # =============================================================================
49 # =============================================================================
53 def parse_command_line():
54 parser = OptionParser(
56 parser.add_option("-s", "--segments-file", metavar = "filename", help = "Load segment lists from this .xml file (required).")
57 parser.add_option("-n", "--segments-name", metavar = "name", help = "Load this segment list from the file (required).")
58 parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose (optional).")
60 options, filenames = parser.parse_args()
62 required = ("segments_file", "segments_name")
63 missing = [opt for opt in required if getattr(options, opt) is None]
65 raise ValueError("missing options: %s" % ", ".join("--%s" % opt.replace("_", "-") for opt in missing))
67 return options, filenames or [None]
71 # =============================================================================
75 # =============================================================================
84 options, filenames = parse_command_line()
88 # load segments and subtract time spanned by cache files
92 if options.segments_file.lower().endswith(".sqlite"):
94 print >>sys.stderr, "reading %s ..." % options.segments_file
95 seglists = ligolw_segments.segmenttable_get_by_name(dbtables.get_xml(sqlite3.connect(options.segments_file)), options.segments_name).coalesce()
97 seglists = ligolw_segments.segmenttable_get_by_name(ligolw_utils.load_filename(options.segments_file, contenthandler = ligolw_segments.LIGOLWContentHandler, verbose = options.verbose), options.segments_name).coalesce()
100 for filename in filenames:
102 print >>sys.stderr, "loading %s ..." % (filename or "stdin")
103 for line in open(filename) if filename else sys.stdin:
104 seglists -= CacheEntry(line).segmentlistdict
108 # check for remainders
112 if any(seglists.values()):
113 print >>sys.stderr, "gaps found in cache. total missing time: %s" % str(abs(seglists))
116 print >>sys.stderr, "cache is complete"